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 | 54 | public function __construct( |
|
60 | ServiceProviderAggregateInterface $providers = null, |
||
61 | InflectorAggregateInterface $inflectors = null, |
||
62 | DefinitionFactoryInterface $definitionFactory = null |
||
63 | ) { |
||
64 | // set required dependencies |
||
65 | 54 | $this->providers = (is_null($providers)) |
|
66 | 54 | ? (new ServiceProviderAggregate)->setContainer($this) |
|
67 | 54 | : $providers->setContainer($this); |
|
68 | |||
69 | 54 | $this->inflectors = (is_null($inflectors)) |
|
70 | 54 | ? (new InflectorAggregate)->setContainer($this) |
|
71 | 54 | : $inflectors->setContainer($this); |
|
72 | |||
73 | 54 | $this->definitionFactory = (is_null($definitionFactory)) |
|
74 | 54 | ? (new DefinitionFactory)->setContainer($this) |
|
75 | 54 | : $definitionFactory->setContainer($this); |
|
76 | 54 | } |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 42 | public function get($alias, array $args = []) |
|
82 | { |
||
83 | 42 | $service = $this->getFromThisContainer($alias, $args); |
|
84 | |||
85 | 42 | if ($service === false && $this->providers->provides($alias)) { |
|
86 | 12 | $this->providers->register($alias); |
|
87 | 12 | $service = $this->getFromThisContainer($alias, $args); |
|
88 | 12 | } |
|
89 | |||
90 | 42 | if ($service !== false) { |
|
91 | 36 | 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 | 48 | public function hasShared($alias, $resolved = false) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 42 | public function add($alias, $concrete = null, $share = false) |
|
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 33 | public function share($alias, $concrete = null) |
|
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 12 | public function addServiceProvider($provider) |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 6 | public function extend($alias) |
|
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function inflector($type, callable $callback = null) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function call(callable $callable, array $args = []) |
||
217 | |||
218 | /** |
||
219 | * Delegate a backup container to be checked for services if it |
||
220 | * cannot be resolved via this container. |
||
221 | * |
||
222 | * @param \Interop\Container\ContainerInterface $container |
||
223 | * @return $this |
||
224 | */ |
||
225 | 9 | public function delegate(InteropContainerInterface $container) |
|
235 | |||
236 | /** |
||
237 | * Returns true if service is registered in one of the delegated backup containers. |
||
238 | * |
||
239 | * @param string $alias |
||
240 | * @return boolean |
||
241 | */ |
||
242 | 9 | public function hasInDelegate($alias) |
|
252 | |||
253 | /** |
||
254 | * Attempt to get a service from the stack of delegated backup containers. |
||
255 | * |
||
256 | * @param string $alias |
||
257 | * @param array $args |
||
258 | * @return mixed |
||
259 | */ |
||
260 | 12 | protected function getFromDelegate($alias, array $args = []) |
|
272 | |||
273 | /** |
||
274 | * Get a service that has been registered in this container. |
||
275 | * |
||
276 | * @param string $alias |
||
277 | * @param array $args |
||
278 | * @return mixed |
||
279 | */ |
||
280 | 42 | protected function getFromThisContainer($alias, array $args = []) |
|
300 | } |
||
301 |