PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / vendor / php-di / php-di / src / CompiledContainer.php

CompiledContainer.php in The Innovative Form Builder – IvyForms 0.3, at backend/vendor/php-di/php-di/src/CompiledContainer.php

130 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace DI;
6
7 use DI\Compiler\RequestedEntryHolder;
8 use DI\Definition\Definition;
9 use DI\Definition\Exception\InvalidDefinition;
10 use DI\Invoker\FactoryParameterResolver;
11 use Invoker\Exception\NotCallableException;
12 use Invoker\Exception\NotEnoughParametersException;
13 use Invoker\Invoker;
14 use Invoker\InvokerInterface;
15 use Invoker\ParameterResolver\AssociativeArrayResolver;
16 use Invoker\ParameterResolver\DefaultValueResolver;
17 use Invoker\ParameterResolver\NumericArrayResolver;
18 use Invoker\ParameterResolver\ResolverChain;
19
20 /**
21 * Compiled version of the dependency injection container.
22 *
23 * @author Matthieu Napoli <[email protected]>
24 */
25 abstract class CompiledContainer extends Container
26 {
27 /**
28 * This const is overridden in child classes (compiled containers).
29 * @var array
30 */
31 protected const METHOD_MAPPING = [];
32
33 /**
34 * @var InvokerInterface
35 */
36 private $factoryInvoker;
37
38 /**
39 * {@inheritdoc}
40 */
41 public function get($name)
42 {
43 // Try to find the entry in the singleton map
44 if (isset($this->resolvedEntries[$name]) || array_key_exists($name, $this->resolvedEntries)) {
45 return $this->resolvedEntries[$name];
46 }
47
48 $method = static::METHOD_MAPPING[$name] ?? null;
49
50 // If it's a compiled entry, then there is a method in this class
51 if ($method !== null) {
52 // Check if we are already getting this entry -> circular dependency
53 if (isset($this->entriesBeingResolved[$name])) {
54 throw new DependencyException("Circular dependency detected while trying to resolve entry '$name'");
55 }
56 $this->entriesBeingResolved[$name] = true;
57
58 try {
59 $value = $this->$method();
60 } finally {
61 unset($this->entriesBeingResolved[$name]);
62 }
63
64 // Store the entry to always return it without recomputing it
65 $this->resolvedEntries[$name] = $value;
66
67 return $value;
68 }
69
70 return parent::get($name);
71 }
72
73 /**
74 * {@inheritdoc}
75 */
76 public function has($name)
77 {
78 if (! is_string($name)) {
79 throw new \InvalidArgumentException(sprintf(
80 'The name parameter must be of type string, %s given',
81 is_object($name) ? get_class($name) : gettype($name)
82 ));
83 }
84
85 // The parent method is overridden to check in our array, it avoids resolving definitions
86 if (isset(static::METHOD_MAPPING[$name])) {
87 return true;
88 }
89
90 return parent::has($name);
91 }
92
93 protected function setDefinition(string $name, Definition $definition)
94 {
95 // It needs to be forbidden because that would mean get() must go through the definitions
96 // every time, which kinds of defeats the performance gains of the compiled container
97 throw new \LogicException('You cannot set a definition at runtime on a compiled container. You can either put your definitions in a file, disable compilation or ->set() a raw value directly (PHP object, string, int, ...) instead of a PHP-DI definition.');
98 }
99
100 /**
101 * Invoke the given callable.
102 */
103 protected function resolveFactory($callable, $entryName, array $extraParameters = [])
104 {
105 // Initialize the factory resolver
106 if (! $this->factoryInvoker) {
107 $parameterResolver = new ResolverChain([
108 new AssociativeArrayResolver,
109 new FactoryParameterResolver($this->delegateContainer),
110 new NumericArrayResolver,
111 new DefaultValueResolver,
112 ]);
113
114 $this->factoryInvoker = new Invoker($parameterResolver, $this->delegateContainer);
115 }
116
117 $parameters = [$this->delegateContainer, new RequestedEntryHolder($entryName)];
118
119 $parameters = array_merge($parameters, $extraParameters);
120
121 try {
122 return $this->factoryInvoker->call($callable, $parameters);
123 } catch (NotCallableException $e) {
124 throw new InvalidDefinition("Entry \"$entryName\" cannot be resolved: factory " . $e->getMessage());
125 } catch (NotEnoughParametersException $e) {
126 throw new InvalidDefinition("Entry \"$entryName\" cannot be resolved: " . $e->getMessage());
127 }
128 }
129 }
130