1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Container; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface as InteropContainerInterface; |
6
|
|
|
use League\Container\Definition\DefinitionFactory; |
7
|
|
|
use League\Container\Definition\DefinitionFactoryInterface; |
8
|
|
|
use League\Container\Definition\DefinitionInterface; |
9
|
|
|
use League\Container\Exception\LockedException; |
10
|
|
|
use League\Container\Exception\NotFoundException; |
11
|
|
|
use League\Container\Inflector\InflectorAggregate; |
12
|
|
|
use League\Container\Inflector\InflectorAggregateInterface; |
13
|
|
|
use League\Container\ServiceProvider\ServiceProviderAggregate; |
14
|
|
|
use League\Container\ServiceProvider\ServiceProviderAggregateInterface; |
15
|
|
|
|
16
|
|
|
class Container implements ContainerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \League\Container\Definition\DefinitionFactoryInterface |
20
|
|
|
*/ |
21
|
|
|
protected $definitionFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \League\Container\Definition\DefinitionInterface[] |
25
|
|
|
*/ |
26
|
|
|
protected $definitions = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \League\Container\Definition\DefinitionInterface[] |
30
|
|
|
*/ |
31
|
|
|
protected $sharedDefinitions = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \League\Container\Inflector\InflectorAggregateInterface |
35
|
|
|
*/ |
36
|
|
|
protected $inflectors; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \League\Container\ServiceProvider\ServiceProviderAggregateInterface |
40
|
|
|
*/ |
41
|
|
|
protected $providers; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $shared = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Interop\Container\ContainerInterface[] |
50
|
|
|
*/ |
51
|
|
|
protected $delegates = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
protected $isLocked = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
42 |
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param \League\Container\ServiceProvider\ServiceProviderAggregateInterface|null $providers |
62
|
|
|
* @param \League\Container\Inflector\InflectorAggregateInterface|null $inflectors |
63
|
|
|
* @param \League\Container\Definition\DefinitionFactoryInterface|null $definitionFactory |
64
|
|
|
*/ |
65
|
42 |
|
public function __construct( |
66
|
42 |
|
ServiceProviderAggregateInterface $providers = null, |
67
|
42 |
|
InflectorAggregateInterface $inflectors = null, |
68
|
|
|
DefinitionFactoryInterface $definitionFactory = null |
69
|
42 |
|
) { |
70
|
42 |
|
// set required dependencies |
71
|
42 |
|
$this->providers = (is_null($providers)) |
72
|
|
|
? (new ServiceProviderAggregate)->setContainer($this) |
73
|
42 |
|
: $providers->setContainer($this); |
74
|
42 |
|
|
75
|
42 |
|
$this->inflectors = (is_null($inflectors)) |
76
|
42 |
|
? (new InflectorAggregate)->setContainer($this) |
77
|
|
|
: $inflectors->setContainer($this); |
78
|
|
|
|
79
|
|
|
$this->definitionFactory = (is_null($definitionFactory)) |
80
|
|
|
? (new DefinitionFactory)->setContainer($this) |
81
|
30 |
|
: $definitionFactory->setContainer($this); |
82
|
|
|
} |
83
|
30 |
|
|
84
|
|
|
/** |
85
|
30 |
|
* {@inheritdoc} |
86
|
12 |
|
*/ |
87
|
12 |
|
public function get($alias, array $args = []) |
88
|
12 |
|
{ |
89
|
|
|
$service = $this->getFromThisContainer($alias, $args); |
90
|
30 |
|
|
91
|
24 |
|
if ($service === false && $this->providers->provides($alias)) { |
92
|
|
|
$this->providers->register($alias); |
93
|
|
|
$service = $this->getFromThisContainer($alias, $args); |
94
|
12 |
|
} |
95
|
6 |
|
|
96
|
|
|
if ($service !== false) { |
97
|
|
|
return $service; |
98
|
6 |
|
} |
99
|
6 |
|
|
100
|
6 |
|
if ($resolved = $this->getFromDelegate($alias, $args)) { |
101
|
|
|
return $this->inflectors->inflect($resolved); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw new NotFoundException( |
105
|
|
|
sprintf('Alias (%s) is not being managed by the container', $alias) |
106
|
18 |
|
); |
107
|
|
|
} |
108
|
18 |
|
|
109
|
12 |
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
9 |
|
public function has($alias) |
113
|
3 |
|
{ |
114
|
|
|
if (array_key_exists($alias, $this->definitions) || $this->hasShared($alias)) { |
115
|
|
|
return true; |
116
|
9 |
|
} |
117
|
|
|
|
118
|
|
|
if ($this->providers->provides($alias)) { |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->hasInDelegate($alias); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
36 |
|
* Returns a boolean to determine if the container has a shared instance of an alias. |
127
|
|
|
* |
128
|
36 |
|
* @param string $alias |
129
|
|
|
* @param boolean $resolved |
130
|
36 |
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
public function hasShared($alias, $resolved = false) |
133
|
|
|
{ |
134
|
|
|
$shared = ($resolved === false) ? array_merge($this->shared, $this->sharedDefinitions) : $this->shared; |
135
|
|
|
|
136
|
30 |
|
return (array_key_exists($alias, $shared)); |
137
|
|
|
} |
138
|
30 |
|
|
139
|
3 |
|
/** |
140
|
3 |
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
30 |
|
public function add($alias, $concrete = null, $share = false) |
143
|
|
|
{ |
144
|
30 |
|
if (is_null($concrete)) { |
145
|
27 |
|
$concrete = $alias; |
146
|
9 |
|
} |
147
|
9 |
|
|
148
|
21 |
|
if ($this->isLocked() && $this->has($alias)) { |
149
|
|
|
throw new LockedException('Container has been locked, overriding services is not allowed.'); |
150
|
|
|
} |
151
|
27 |
|
|
152
|
|
|
$definition = $this->definitionFactory->getDefinition($alias, $concrete); |
153
|
|
|
|
154
|
|
|
if ($definition instanceof DefinitionInterface) { |
155
|
3 |
|
if ($share === false) { |
156
|
3 |
|
$this->definitions[$alias] = $definition; |
157
|
|
|
} else { |
158
|
|
|
$this->sharedDefinitions[$alias] = $definition; |
159
|
|
|
} |
160
|
|
|
|
161
|
21 |
|
return $definition; |
162
|
|
|
} |
163
|
21 |
|
|
164
|
|
|
// dealing with a value that cannot build a definition |
165
|
|
|
$this->shared[$alias] = $concrete; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
12 |
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
12 |
|
public function share($alias, $concrete = null) |
172
|
|
|
{ |
173
|
12 |
|
return $this->add($alias, $concrete, true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* {@inheritdoc} |
178
|
|
|
*/ |
179
|
6 |
|
public function addServiceProvider($provider) |
180
|
|
|
{ |
181
|
6 |
|
$this->providers->add($provider); |
182
|
3 |
|
|
183
|
3 |
|
return $this; |
184
|
|
|
} |
185
|
6 |
|
|
186
|
3 |
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
6 |
|
public function extend($alias) |
190
|
3 |
|
{ |
191
|
|
|
if ($this->providers->provides($alias)) { |
192
|
|
|
$this->providers->register($alias); |
193
|
3 |
|
} |
194
|
3 |
|
|
195
|
3 |
|
if (array_key_exists($alias, $this->definitions)) { |
196
|
|
|
return $this->definitions[$alias]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (array_key_exists($alias, $this->sharedDefinitions)) { |
200
|
|
|
return $this->sharedDefinitions[$alias]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
throw new NotFoundException( |
204
|
|
|
sprintf('Unable to extend alias (%s) as it is not being managed as a definition', $alias) |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function inflector($type, callable $callback = null) |
212
|
|
|
{ |
213
|
|
|
return $this->inflectors->add($type, $callback); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritdoc} |
218
|
|
|
*/ |
219
|
|
|
public function call(callable $callable, array $args = []) |
220
|
|
|
{ |
221
|
9 |
|
return (new ReflectionContainer)->setContainer($this)->call($callable, $args); |
222
|
|
|
} |
223
|
9 |
|
|
224
|
|
|
public function lock() |
225
|
9 |
|
{ |
226
|
3 |
|
$this->isLocked = true; |
227
|
3 |
|
} |
228
|
|
|
|
229
|
9 |
|
/** |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function isLocked() |
233
|
|
|
{ |
234
|
|
|
return $this->isLocked; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
9 |
|
* Delegate a backup container to be checked for services if it |
239
|
|
|
* cannot be resolved via this container. |
240
|
9 |
|
* |
241
|
3 |
|
* @param \Interop\Container\ContainerInterface $container |
242
|
3 |
|
* @return $this |
243
|
|
|
*/ |
244
|
9 |
|
public function delegate(InteropContainerInterface $container) |
245
|
|
|
{ |
246
|
6 |
|
$this->delegates[] = $container; |
247
|
|
|
|
248
|
|
|
if ($container instanceof ImmutableContainerAwareInterface) { |
249
|
|
|
$container->setContainer($this); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
12 |
|
* Returns true if service is registered in one of the delegated backup containers. |
257
|
|
|
* |
258
|
12 |
|
* @param string $alias |
259
|
6 |
|
* @return boolean |
260
|
6 |
|
*/ |
261
|
|
|
public function hasInDelegate($alias) |
262
|
|
|
{ |
263
|
3 |
|
foreach ($this->delegates as $container) { |
264
|
6 |
|
if ($container->has($alias)) { |
265
|
|
|
return true; |
266
|
6 |
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return false; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Attempt to get a service from the stack of delegated backup containers. |
274
|
|
|
* |
275
|
|
|
* @param string $alias |
276
|
30 |
|
* @param array $args |
277
|
|
|
* @return mixed |
278
|
30 |
|
*/ |
279
|
9 |
|
protected function getFromDelegate($alias, array $args = []) |
280
|
|
|
{ |
281
|
|
|
foreach ($this->delegates as $container) { |
282
|
27 |
|
if ($container->has($alias)) { |
283
|
15 |
|
return $container->get($alias, $args); |
284
|
15 |
|
} |
285
|
15 |
|
|
286
|
|
|
continue; |
287
|
|
|
} |
288
|
24 |
|
|
289
|
6 |
|
return false; |
290
|
6 |
|
} |
291
|
6 |
|
|
292
|
|
|
/** |
293
|
|
|
* Get a service that has been registered in this container. |
294
|
21 |
|
* |
295
|
|
|
* @param string $alias |
296
|
|
|
* @param array $args |
297
|
|
|
* @return mixed |
298
|
|
|
*/ |
299
|
|
|
protected function getFromThisContainer($alias, array $args = []) |
300
|
|
|
{ |
301
|
|
|
if ($this->hasShared($alias, true)) { |
302
|
|
|
return $this->inflectors->inflect($this->shared[$alias]); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if (array_key_exists($alias, $this->sharedDefinitions)) { |
306
|
|
|
$shared = $this->inflectors->inflect($this->sharedDefinitions[$alias]->build()); |
307
|
|
|
$this->shared[$alias] = $shared; |
308
|
|
|
return $shared; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if (array_key_exists($alias, $this->definitions)) { |
312
|
|
|
return $this->inflectors->inflect( |
313
|
|
|
$this->definitions[$alias]->build($args) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|