1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Yiisoft\Di\Contracts\DeferredServiceProviderInterface; |
10
|
|
|
use Yiisoft\Di\Contracts\ServiceProviderInterface; |
11
|
|
|
use Yiisoft\Factory\Definition\ArrayDefinition; |
12
|
|
|
use Yiisoft\Factory\Definition\DefinitionValidator; |
13
|
|
|
use Yiisoft\Factory\Exception\CircularReferenceException; |
14
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
15
|
|
|
use Yiisoft\Factory\Exception\NotFoundException; |
16
|
|
|
use Yiisoft\Factory\Exception\NotInstantiableException; |
17
|
|
|
use Yiisoft\Injector\Injector; |
18
|
|
|
|
19
|
|
|
use function array_key_exists; |
20
|
|
|
use function array_keys; |
21
|
|
|
use function assert; |
22
|
|
|
use function class_exists; |
23
|
|
|
use function get_class; |
24
|
|
|
use function implode; |
25
|
|
|
use function in_array; |
26
|
|
|
use function is_array; |
27
|
|
|
use function is_object; |
28
|
|
|
use function is_string; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Container implements a [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) container. |
32
|
|
|
*/ |
33
|
|
|
final class Container extends AbstractContainerConfigurator implements ContainerInterface |
34
|
|
|
{ |
35
|
|
|
private const META_TAGS = 'tags'; |
36
|
|
|
private const META_RESET = 'reset'; |
37
|
|
|
private const ALLOWED_META = [self::META_TAGS, self::META_RESET]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array object definitions indexed by their types |
41
|
|
|
*/ |
42
|
|
|
private array $definitions = []; |
43
|
|
|
/** |
44
|
|
|
* @var array used to collect ids instantiated during build |
45
|
|
|
* to detect circular references |
46
|
|
|
*/ |
47
|
|
|
private array $building = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool $validate Validate definitions when set |
51
|
|
|
*/ |
52
|
|
|
private bool $validate; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var object[] |
56
|
|
|
*/ |
57
|
|
|
private array $instances = []; |
58
|
|
|
|
59
|
|
|
private array $tags; |
60
|
|
|
|
61
|
|
|
private array $resetters = []; |
62
|
|
|
|
63
|
|
|
private ?CompositeContainer $rootContainer = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Container constructor. |
67
|
|
|
* |
68
|
|
|
* @param array $definitions Definitions to put into container. |
69
|
|
|
* @param ServiceProviderInterface[]|string[] $providers Service providers |
70
|
|
|
* to get definitions from. |
71
|
|
|
* @param ContainerInterface|null $rootContainer Root container to delegate |
72
|
|
|
* lookup to when resolving dependencies. If provided the current container |
73
|
|
|
* is no longer queried for dependencies. |
74
|
|
|
* |
75
|
|
|
* @throws InvalidConfigException |
76
|
|
|
*/ |
77
|
99 |
|
public function __construct( |
78
|
|
|
array $definitions = [], |
79
|
|
|
array $providers = [], |
80
|
|
|
array $tags = [], |
81
|
|
|
ContainerInterface $rootContainer = null, |
82
|
|
|
bool $validate = true |
83
|
|
|
) { |
84
|
99 |
|
$this->tags = $tags; |
85
|
99 |
|
$this->validate = $validate; |
86
|
99 |
|
$this->delegateLookup($rootContainer); |
87
|
99 |
|
$this->setDefaultDefinitions(); |
88
|
99 |
|
$this->setMultiple($definitions); |
89
|
93 |
|
$this->addProviders($providers); |
90
|
|
|
|
91
|
|
|
// Prevent circular reference to ContainerInterface |
92
|
91 |
|
$this->get(ContainerInterface::class); |
93
|
91 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
97
|
|
|
* |
98
|
|
|
* @param string $id class name, interface name or alias name |
99
|
|
|
* |
100
|
|
|
* @return bool whether the container is able to provide instance of class specified. |
101
|
|
|
* |
102
|
|
|
* @see set() |
103
|
|
|
*/ |
104
|
32 |
|
public function has($id): bool |
105
|
|
|
{ |
106
|
32 |
|
if ($this->isTagAlias($id)) { |
107
|
2 |
|
$tag = substr($id, 4); |
108
|
2 |
|
return isset($this->tags[$tag]); |
109
|
|
|
} |
110
|
|
|
|
111
|
30 |
|
return isset($this->definitions[$id]) || class_exists($id); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns an instance by either interface name or alias. |
116
|
|
|
* |
117
|
|
|
* Same instance of the class will be returned each time this method is called. |
118
|
|
|
* |
119
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
120
|
|
|
* |
121
|
|
|
* @throws CircularReferenceException |
122
|
|
|
* @throws InvalidConfigException |
123
|
|
|
* @throws NotFoundException |
124
|
|
|
* @throws NotInstantiableException |
125
|
|
|
* |
126
|
|
|
* @return mixed|object An instance of the requested interface. |
127
|
|
|
* |
128
|
|
|
* @psalm-template T |
129
|
|
|
* @psalm-param string|class-string<T> $id |
130
|
|
|
* @psalm-return ($id is class-string ? T : mixed) |
131
|
|
|
*/ |
132
|
92 |
|
public function get($id) |
133
|
|
|
{ |
134
|
92 |
|
if ($id === StateResetter::class && !isset($this->definitions[$id])) { |
135
|
4 |
|
$resetters = []; |
136
|
4 |
|
foreach ($this->resetters as $serviceId => $callback) { |
137
|
4 |
|
if (isset($this->instances[$serviceId])) { |
138
|
4 |
|
$resetters[] = $callback->bindTo($this->instances[$serviceId], get_class($this->instances[$serviceId])); |
139
|
|
|
} |
140
|
|
|
} |
141
|
4 |
|
return new StateResetter($resetters, $this); |
142
|
|
|
} |
143
|
|
|
|
144
|
92 |
|
if (!array_key_exists($id, $this->instances)) { |
145
|
92 |
|
$this->instances[$id] = $this->build($id); |
146
|
|
|
} |
147
|
|
|
|
148
|
92 |
|
return $this->instances[$id]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Delegate service lookup to another container. |
153
|
|
|
* |
154
|
|
|
* @param ContainerInterface $container |
155
|
|
|
*/ |
156
|
99 |
|
protected function delegateLookup(?ContainerInterface $container): void |
157
|
|
|
{ |
158
|
99 |
|
if ($container === null) { |
159
|
99 |
|
return; |
160
|
|
|
} |
161
|
8 |
|
if ($this->rootContainer === null) { |
162
|
8 |
|
$this->rootContainer = new CompositeContainer(); |
163
|
8 |
|
$this->setDefaultDefinitions(); |
164
|
|
|
} |
165
|
|
|
|
166
|
8 |
|
$this->rootContainer->attach($container); |
|
|
|
|
167
|
8 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sets a definition to the container. Definition may be defined multiple ways. |
171
|
|
|
* |
172
|
|
|
* @param string $id |
173
|
|
|
* @param mixed $definition |
174
|
|
|
* |
175
|
|
|
* @throws InvalidConfigException |
176
|
|
|
* |
177
|
|
|
* @see `DefinitionNormalizer::normalize()` |
178
|
|
|
*/ |
179
|
99 |
|
protected function set(string $id, $definition): void |
180
|
|
|
{ |
181
|
99 |
|
[$definition, $meta] = DefinitionParser::parse($definition); |
182
|
99 |
|
if ($this->validate) { |
183
|
99 |
|
$this->validateDefinition($definition, $id); |
184
|
99 |
|
$this->validateMeta($meta); |
185
|
|
|
} |
186
|
|
|
|
187
|
99 |
|
if (isset($meta[self::META_TAGS])) { |
188
|
8 |
|
if ($this->validate) { |
189
|
8 |
|
$this->validateTags($meta[self::META_TAGS]); |
190
|
|
|
} |
191
|
8 |
|
$this->setTags($id, $meta[self::META_TAGS]); |
192
|
|
|
} |
193
|
99 |
|
if (isset($meta[self::META_RESET])) { |
194
|
5 |
|
$this->setResetter($id, $meta[self::META_RESET]); |
195
|
|
|
} |
196
|
|
|
|
197
|
99 |
|
unset($this->instances[$id]); |
198
|
99 |
|
$this->definitions[$id] = $definition; |
199
|
99 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Sets multiple definitions at once. |
203
|
|
|
* |
204
|
|
|
* @param array $config definitions indexed by their ids |
205
|
|
|
* |
206
|
|
|
* @throws InvalidConfigException |
207
|
|
|
*/ |
208
|
99 |
|
protected function setMultiple(array $config): void |
209
|
|
|
{ |
210
|
99 |
|
foreach ($config as $id => $definition) { |
211
|
99 |
|
if ($this->validate && !is_string($id)) { |
212
|
1 |
|
throw new InvalidConfigException(sprintf('Key must be a string. %s given.', $this->getVariableType($id))); |
213
|
|
|
} |
214
|
99 |
|
$this->set($id, $definition); |
215
|
|
|
} |
216
|
99 |
|
} |
217
|
|
|
|
218
|
99 |
|
private function setDefaultDefinitions(): void |
219
|
|
|
{ |
220
|
99 |
|
$container = $this->rootContainer ?? $this; |
221
|
99 |
|
$this->setMultiple([ |
222
|
99 |
|
ContainerInterface::class => $container, |
223
|
99 |
|
Injector::class => new Injector($container), |
224
|
|
|
]); |
225
|
99 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param mixed $definition |
229
|
|
|
* |
230
|
|
|
* @throws InvalidConfigException |
231
|
|
|
*/ |
232
|
99 |
|
private function validateDefinition($definition, ?string $id = null): void |
233
|
|
|
{ |
234
|
99 |
|
if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) { |
235
|
37 |
|
[$class, $constructorArguments, $methodsAndProperties] = $definition; |
236
|
37 |
|
$definition = array_merge( |
237
|
37 |
|
$class === null ? [] : [ArrayDefinition::CLASS_NAME => $class], |
238
|
37 |
|
[ArrayDefinition::CONSTRUCTOR => $constructorArguments], |
239
|
|
|
$methodsAndProperties, |
240
|
|
|
); |
241
|
|
|
} |
242
|
99 |
|
DefinitionValidator::validate($definition, $id); |
243
|
99 |
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @throws InvalidConfigException |
247
|
|
|
*/ |
248
|
99 |
|
private function validateMeta(array $meta): void |
249
|
|
|
{ |
250
|
99 |
|
foreach ($meta as $key => $_value) { |
251
|
16 |
|
if (!in_array($key, self::ALLOWED_META, true)) { |
252
|
3 |
|
throw new InvalidConfigException( |
253
|
3 |
|
sprintf( |
254
|
3 |
|
'Invalid definition: metadata "%s" is not allowed. Did you mean "%s()" or "$%s"?', |
255
|
|
|
$key, |
256
|
|
|
$key, |
257
|
|
|
$key, |
258
|
|
|
) |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
} |
262
|
99 |
|
} |
263
|
|
|
|
264
|
8 |
|
private function validateTags(array $tags): void |
265
|
|
|
{ |
266
|
8 |
|
foreach ($tags as $tag) { |
267
|
8 |
|
if (!is_string($tag)) { |
268
|
|
|
throw new InvalidConfigException('Invalid tag. Expected a string, got ' . var_export($tag, true) . '.'); |
269
|
|
|
} |
270
|
|
|
} |
271
|
8 |
|
} |
272
|
|
|
|
273
|
8 |
|
private function setTags(string $id, array $tags): void |
274
|
|
|
{ |
275
|
8 |
|
foreach ($tags as $tag) { |
276
|
8 |
|
if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag], true)) { |
277
|
8 |
|
$this->tags[$tag][] = $id; |
278
|
|
|
} |
279
|
|
|
} |
280
|
8 |
|
} |
281
|
|
|
|
282
|
4 |
|
private function setResetter(string $id, Closure $resetter): void |
283
|
|
|
{ |
284
|
4 |
|
$this->resetters[$id] = $resetter; |
285
|
4 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Creates new instance by either interface name or alias. |
289
|
|
|
* |
290
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
291
|
|
|
* |
292
|
|
|
* @throws CircularReferenceException |
293
|
|
|
* @throws InvalidConfigException |
294
|
|
|
* @throws NotFoundException |
295
|
|
|
* |
296
|
|
|
* @return mixed|object New built instance of the specified class. |
297
|
|
|
* |
298
|
|
|
* @internal |
299
|
|
|
*/ |
300
|
92 |
|
private function build(string $id) |
301
|
|
|
{ |
302
|
92 |
|
if ($this->isTagAlias($id)) { |
303
|
9 |
|
return $this->getTaggedServices($id); |
304
|
|
|
} |
305
|
|
|
|
306
|
92 |
|
if (isset($this->building[$id])) { |
307
|
9 |
|
if ($id === ContainerInterface::class) { |
308
|
2 |
|
return $this; |
309
|
|
|
} |
310
|
7 |
|
throw new CircularReferenceException(sprintf( |
311
|
7 |
|
'Circular reference to "%s" detected while building: %s.', |
312
|
|
|
$id, |
313
|
7 |
|
implode(',', array_keys($this->building)) |
314
|
|
|
)); |
315
|
|
|
} |
316
|
|
|
|
317
|
92 |
|
$this->building[$id] = 1; |
318
|
|
|
try { |
319
|
92 |
|
$object = $this->buildInternal($id); |
320
|
92 |
|
} finally { |
321
|
92 |
|
unset($this->building[$id]); |
322
|
|
|
} |
323
|
|
|
|
324
|
92 |
|
return $object; |
325
|
|
|
} |
326
|
|
|
|
327
|
92 |
|
private function isTagAlias(string $id): bool |
328
|
|
|
{ |
329
|
92 |
|
return strpos($id, 'tag@') === 0; |
330
|
|
|
} |
331
|
|
|
|
332
|
9 |
|
private function getTaggedServices(string $tagAlias): array |
333
|
|
|
{ |
334
|
9 |
|
$tag = substr($tagAlias, 4); |
335
|
9 |
|
$services = []; |
336
|
9 |
|
if (isset($this->tags[$tag])) { |
337
|
8 |
|
foreach ($this->tags[$tag] as $service) { |
338
|
8 |
|
$services[] = $this->get($service); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
9 |
|
return $services; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param mixed $definition |
347
|
|
|
*/ |
348
|
92 |
|
private function processDefinition($definition): void |
349
|
|
|
{ |
350
|
92 |
|
if ($definition instanceof DeferredServiceProviderInterface) { |
351
|
1 |
|
$definition->register($this); |
352
|
|
|
} |
353
|
92 |
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param string $id |
357
|
|
|
* |
358
|
|
|
* @throws InvalidConfigException |
359
|
|
|
* @throws NotFoundException |
360
|
|
|
* |
361
|
|
|
* @return mixed|object |
362
|
|
|
*/ |
363
|
92 |
|
private function buildInternal(string $id) |
364
|
|
|
{ |
365
|
92 |
|
if (!isset($this->definitions[$id])) { |
366
|
50 |
|
return $this->buildPrimitive($id); |
367
|
|
|
} |
368
|
92 |
|
$this->processDefinition($this->definitions[$id]); |
369
|
92 |
|
$definition = DefinitionNormalizer::normalize($this->definitions[$id], $id); |
370
|
|
|
|
371
|
92 |
|
return $definition->resolve($this->rootContainer ?? $this); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param string $class |
376
|
|
|
* |
377
|
|
|
* @throws InvalidConfigException |
378
|
|
|
* @throws NotFoundException |
379
|
|
|
* |
380
|
|
|
* @return mixed|object |
381
|
|
|
*/ |
382
|
50 |
|
private function buildPrimitive(string $class) |
383
|
|
|
{ |
384
|
50 |
|
if (class_exists($class)) { |
385
|
48 |
|
$definition = ArrayDefinition::fromPreparedData($class); |
386
|
|
|
|
387
|
48 |
|
return $definition->resolve($this->rootContainer ?? $this); |
388
|
|
|
} |
389
|
|
|
|
390
|
4 |
|
throw new NotFoundException($class); |
391
|
|
|
} |
392
|
|
|
|
393
|
93 |
|
private function addProviders(array $providers): void |
394
|
|
|
{ |
395
|
93 |
|
foreach ($providers as $provider) { |
396
|
6 |
|
$this->addProvider($provider); |
397
|
|
|
} |
398
|
91 |
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Adds service provider to the container. Unless service provider is deferred |
402
|
|
|
* it would be immediately registered. |
403
|
|
|
* |
404
|
|
|
* @param mixed $providerDefinition |
405
|
|
|
* |
406
|
|
|
* @throws InvalidConfigException |
407
|
|
|
* @throws NotInstantiableException |
408
|
|
|
* |
409
|
|
|
* @see ServiceProviderInterface |
410
|
|
|
* @see DeferredServiceProviderInterface |
411
|
|
|
*/ |
412
|
6 |
|
private function addProvider($providerDefinition): void |
413
|
|
|
{ |
414
|
6 |
|
$provider = $this->buildProvider($providerDefinition); |
415
|
|
|
|
416
|
5 |
|
if ($provider instanceof DeferredServiceProviderInterface) { |
417
|
1 |
|
foreach ($provider->provides() as $id) { |
418
|
1 |
|
$this->definitions[$id] = $provider; |
419
|
|
|
} |
420
|
|
|
} else { |
421
|
4 |
|
$provider->register($this); |
422
|
|
|
} |
423
|
4 |
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Builds service provider by definition. |
427
|
|
|
* |
428
|
|
|
* @param mixed $providerDefinition class name or definition of provider. |
429
|
|
|
* |
430
|
|
|
* @throws InvalidConfigException |
431
|
|
|
* |
432
|
|
|
* @return ServiceProviderInterface instance of service provider; |
433
|
|
|
*/ |
434
|
6 |
|
private function buildProvider($providerDefinition): ServiceProviderInterface |
435
|
|
|
{ |
436
|
6 |
|
if ($this->validate) { |
437
|
6 |
|
$this->validateDefinition($providerDefinition); |
438
|
|
|
} |
439
|
5 |
|
$provider = DefinitionNormalizer::normalize($providerDefinition)->resolve($this); |
440
|
5 |
|
assert($provider instanceof ServiceProviderInterface, new InvalidConfigException( |
441
|
5 |
|
sprintf( |
442
|
5 |
|
'Service provider should be an instance of %s. %s given.', |
443
|
5 |
|
ServiceProviderInterface::class, |
444
|
5 |
|
$this->getVariableType($provider) |
445
|
|
|
) |
446
|
|
|
)); |
447
|
|
|
|
448
|
5 |
|
return $provider; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* @param mixed $variable |
453
|
|
|
*/ |
454
|
6 |
|
private function getVariableType($variable): string |
455
|
|
|
{ |
456
|
6 |
|
if (is_object($variable)) { |
457
|
5 |
|
return get_class($variable); |
458
|
|
|
} |
459
|
|
|
|
460
|
1 |
|
return gettype($variable); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.