1 | <?php |
||
15 | class Container implements ContainerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var \League\Container\Definition\DefinitionFactoryInterface |
||
19 | */ |
||
20 | protected $definitionFactory; |
||
21 | |||
22 | /** |
||
23 | * @var \League\Container\Definition\DefinitionInterface[] |
||
24 | */ |
||
25 | protected $definitions = []; |
||
26 | |||
27 | /** |
||
28 | * @var \League\Container\Definition\DefinitionInterface[] |
||
29 | */ |
||
30 | protected $sharedDefinitions = []; |
||
31 | |||
32 | /** |
||
33 | * @var \League\Container\Inflector\InflectorAggregateInterface |
||
34 | */ |
||
35 | protected $inflectors; |
||
36 | |||
37 | /** |
||
38 | * @var \League\Container\ServiceProvider\ServiceProviderAggregateInterface |
||
39 | */ |
||
40 | protected $providers; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $shared = []; |
||
46 | |||
47 | /** |
||
48 | * @var \Interop\Container\ContainerInterface[] |
||
49 | */ |
||
50 | protected $delegates = []; |
||
51 | |||
52 | /** |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param \League\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers |
||
56 | * @param \League\Container\Inflector\InflectorAggregateInterface|null $inflectors |
||
57 | * @param \League\Container\Definition\DefinitionFactoryInterface|null $definitionFactory |
||
58 | */ |
||
59 | 48 | public function __construct( |
|
60 | ServiceProviderAggregateInterface $providers = null, |
||
61 | InflectorAggregateInterface $inflectors = null, |
||
62 | DefinitionFactoryInterface $definitionFactory = null |
||
63 | ) { |
||
64 | // set required dependencies |
||
65 | 48 | $this->providers = (is_null($providers)) |
|
66 | 48 | ? (new ServiceProviderAggregate)->setContainer($this) |
|
67 | 48 | : $providers->setContainer($this); |
|
68 | |||
69 | 48 | $this->inflectors = (is_null($inflectors)) |
|
70 | 48 | ? (new InflectorAggregate)->setContainer($this) |
|
71 | 48 | : $inflectors->setContainer($this); |
|
72 | |||
73 | 48 | $this->definitionFactory = (is_null($definitionFactory)) |
|
74 | 48 | ? (new DefinitionFactory)->setContainer($this) |
|
75 | 48 | : $definitionFactory->setContainer($this); |
|
76 | 48 | } |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 33 | public function get($alias, array $args = []) |
|
82 | { |
||
83 | 33 | $service = $this->getFromThisContainer($alias, $args); |
|
84 | |||
85 | 33 | if ($service === false && $this->providers->provides($alias)) { |
|
86 | 12 | $this->providers->register($alias); |
|
87 | 12 | $service = $this->getFromThisContainer($alias, $args); |
|
88 | 12 | } |
|
89 | |||
90 | 33 | if ($service !== false) { |
|
91 | 27 | return $service; |
|
92 | } |
||
93 | |||
94 | 12 | if ($resolved = $this->getFromDelegate($alias, $args)) { |
|
95 | 6 | return $this->inflectors->inflect($resolved); |
|
96 | } |
||
97 | |||
98 | 6 | throw new NotFoundException( |
|
99 | 6 | sprintf('Alias (%s) is not being managed by the container', $alias) |
|
100 | 6 | ); |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 18 | public function has($alias) |
|
118 | |||
119 | /** |
||
120 | * Returns a boolean to determine if the container has a shared instance of an alias. |
||
121 | * |
||
122 | * @param string $alias |
||
123 | * @param boolean $resolved |
||
124 | * @return boolean |
||
125 | */ |
||
126 | 39 | public function hasShared($alias, $resolved = false) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 36 | public function add($alias, $concrete = null, $share = false) |
|
137 | { |
||
138 | 36 | if (is_null($concrete)) { |
|
139 | 3 | $concrete = $alias; |
|
140 | 3 | } |
|
141 | |||
142 | 36 | $definition = $this->definitionFactory->getDefinition($alias, $concrete); |
|
143 | |||
144 | 36 | if ($definition instanceof DefinitionInterface) { |
|
145 | 36 | if ($share === false) { |
|
146 | 18 | $this->definitions[$alias] = $definition; |
|
147 | 18 | } else { |
|
148 | 21 | $this->sharedDefinitions[$alias] = $definition; |
|
149 | } |
||
150 | |||
151 | 36 | return $definition; |
|
152 | } |
||
153 | |||
154 | // dealing with a value that cannot build a definition |
||
155 | $this->shared[$alias] = $concrete; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 21 | public function share($alias, $concrete = null) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 12 | public function addServiceProvider($provider) |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 6 | public function extend($alias) |
|
180 | { |
||
181 | 6 | if ($this->providers->provides($alias)) { |
|
182 | 3 | $this->providers->register($alias); |
|
183 | 3 | } |
|
184 | |||
185 | 6 | if (array_key_exists($alias, $this->definitions)) { |
|
186 | 3 | return $this->definitions[$alias]; |
|
187 | } |
||
188 | |||
189 | 6 | if (array_key_exists($alias, $this->sharedDefinitions)) { |
|
190 | 3 | return $this->sharedDefinitions[$alias]; |
|
191 | } |
||
192 | |||
193 | 3 | throw new NotFoundException( |
|
194 | 3 | sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $alias) |
|
195 | 3 | ); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function inflector($type, callable $callback = null) |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function call(callable $callable, array $args = []) |
||
210 | { |
||
211 | return (new ReflectionContainer)->setContainer($this)->call($callable, $args); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Delegate a backup container to be checked for services if it |
||
216 | * cannot be resolved via this container. |
||
217 | * |
||
218 | * @param \Interop\Container\ContainerInterface $container |
||
219 | * @return $this |
||
220 | */ |
||
221 | 9 | public function delegate(InteropContainerInterface $container) |
|
231 | |||
232 | /** |
||
233 | * Returns true if service is registered in one of the delegated backup containers. |
||
234 | * |
||
235 | * @param string $alias |
||
236 | * @return boolean |
||
237 | */ |
||
238 | 9 | public function hasInDelegate($alias) |
|
248 | |||
249 | /** |
||
250 | * Attempt to get a service from the stack of delegated backup containers. |
||
251 | * |
||
252 | * @param string $alias |
||
253 | * @param array $args |
||
254 | * @return mixed |
||
255 | */ |
||
256 | 12 | protected function getFromDelegate($alias, array $args = []) |
|
268 | |||
269 | /** |
||
270 | * Get a service that has been registered in this container. |
||
271 | * |
||
272 | * @param string $alias |
||
273 | * @param array $args |
||
274 | * @return mixed |
||
275 | */ |
||
276 | 33 | protected function getFromThisContainer($alias, array $args = []) |
|
296 | } |
||
297 |