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 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 DependencyResolver $dependencyResolver; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Container constructor. |
67
|
|
|
* |
68
|
|
|
* @param array $definitions Definitions to put into container. |
69
|
|
|
* @param array $providers Service providers to get definitions from. |
70
|
|
|
* @param ContainerInterface|null $rootContainer Root container to delegate |
71
|
|
|
* lookup to when resolving dependencies. If provided the current container |
72
|
|
|
* is no longer queried for dependencies. |
73
|
|
|
* |
74
|
|
|
* @throws InvalidConfigException |
75
|
|
|
*/ |
76
|
88 |
|
public function __construct( |
77
|
|
|
array $definitions = [], |
78
|
|
|
array $providers = [], |
79
|
|
|
array $tags = [], |
80
|
|
|
bool $validate = true |
81
|
|
|
) { |
82
|
88 |
|
$this->tags = $tags; |
83
|
88 |
|
$this->validate = $validate; |
84
|
88 |
|
$this->setDefaultDefinitions(); |
85
|
88 |
|
$this->setMultiple($definitions); |
86
|
82 |
|
$this->addProviders($providers); |
87
|
82 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
91
|
|
|
* |
92
|
|
|
* @param string $id class name, interface name or alias name |
93
|
|
|
* |
94
|
|
|
* @return bool whether the container is able to provide instance of class specified. |
95
|
|
|
* |
96
|
|
|
* @see set() |
97
|
|
|
*/ |
98
|
22 |
|
public function has($id): bool |
99
|
|
|
{ |
100
|
22 |
|
if ($this->isTagAlias($id)) { |
101
|
2 |
|
$tag = substr($id, 4); |
102
|
2 |
|
return isset($this->tags[$tag]); |
103
|
|
|
} |
104
|
|
|
|
105
|
20 |
|
return isset($this->definitions[$id]) || class_exists($id); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns an instance by either interface name or alias. |
110
|
|
|
* |
111
|
|
|
* Same instance of the class will be returned each time this method is called. |
112
|
|
|
* |
113
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
114
|
|
|
* |
115
|
|
|
* @throws CircularReferenceException |
116
|
|
|
* @throws InvalidConfigException |
117
|
|
|
* @throws NotFoundException |
118
|
|
|
* @throws NotInstantiableException |
119
|
|
|
* |
120
|
|
|
* @return mixed|object An instance of the requested interface. |
121
|
|
|
* |
122
|
|
|
* @psalm-template T |
123
|
|
|
* @psalm-param string|class-string<T> $id |
124
|
|
|
* @psalm-return ($id is class-string ? T : mixed) |
125
|
|
|
*/ |
126
|
78 |
|
public function get($id) |
127
|
|
|
{ |
128
|
78 |
|
if ($id === StateResetter::class && !isset($this->definitions[$id])) { |
129
|
4 |
|
$resetters = []; |
130
|
4 |
|
foreach ($this->resetters as $serviceId => $callback) { |
131
|
4 |
|
if (isset($this->instances[$serviceId])) { |
132
|
4 |
|
$resetters[] = $callback->bindTo($this->instances[$serviceId], get_class($this->instances[$serviceId])); |
133
|
|
|
} |
134
|
|
|
} |
135
|
4 |
|
return new StateResetter($resetters, $this); |
136
|
|
|
} |
137
|
|
|
|
138
|
78 |
|
if (!array_key_exists($id, $this->instances)) { |
139
|
78 |
|
$this->instances[$id] = $this->build($id); |
140
|
|
|
} |
141
|
|
|
|
142
|
76 |
|
return $this->instances[$id]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Sets a definition to the container. Definition may be defined multiple ways. |
147
|
|
|
* |
148
|
|
|
* @param string $id |
149
|
|
|
* @param mixed $definition |
150
|
|
|
* |
151
|
|
|
* @throws InvalidConfigException |
152
|
|
|
* |
153
|
|
|
* @see `DefinitionNormalizer::normalize()` |
154
|
|
|
*/ |
155
|
88 |
|
protected function set(string $id, $definition): void |
156
|
|
|
{ |
157
|
88 |
|
[$definition, $meta] = DefinitionParser::parse($definition); |
158
|
88 |
|
if ($this->validate) { |
159
|
88 |
|
$this->validateDefinition($definition, $id); |
160
|
88 |
|
$this->validateMeta($meta); |
161
|
|
|
} |
162
|
|
|
|
163
|
88 |
|
if (isset($meta[self::META_TAGS])) { |
164
|
8 |
|
if ($this->validate) { |
165
|
8 |
|
$this->validateTags($meta[self::META_TAGS]); |
166
|
|
|
} |
167
|
8 |
|
$this->setTags($id, $meta[self::META_TAGS]); |
168
|
|
|
} |
169
|
88 |
|
if (isset($meta[self::META_RESET])) { |
170
|
5 |
|
$this->setResetter($id, $meta[self::META_RESET]); |
171
|
|
|
} |
172
|
|
|
|
173
|
88 |
|
unset($this->instances[$id]); |
174
|
88 |
|
$this->definitions[$id] = $definition; |
175
|
88 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sets multiple definitions at once. |
179
|
|
|
* |
180
|
|
|
* @param array $config definitions indexed by their ids |
181
|
|
|
* |
182
|
|
|
* @throws InvalidConfigException |
183
|
|
|
*/ |
184
|
88 |
|
protected function setMultiple(array $config): void |
185
|
|
|
{ |
186
|
88 |
|
foreach ($config as $id => $definition) { |
187
|
88 |
|
if ($this->validate && !is_string($id)) { |
188
|
1 |
|
throw new InvalidConfigException(sprintf('Key must be a string. %s given.', $this->getVariableType($id))); |
189
|
|
|
} |
190
|
88 |
|
$this->set($id, $definition); |
191
|
|
|
} |
192
|
88 |
|
} |
193
|
|
|
|
194
|
88 |
|
private function setDefaultDefinitions(): void |
195
|
|
|
{ |
196
|
88 |
|
$this->setMultiple([ |
197
|
88 |
|
ContainerInterface::class => $this, |
198
|
88 |
|
Injector::class => new Injector($this), |
199
|
|
|
]); |
200
|
88 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param mixed $definition |
204
|
|
|
* |
205
|
|
|
* @throws InvalidConfigException |
206
|
|
|
*/ |
207
|
88 |
|
private function validateDefinition($definition, ?string $id = null): void |
208
|
|
|
{ |
209
|
88 |
|
if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) { |
210
|
35 |
|
[$class, $constructorArguments, $methodsAndProperties] = $definition; |
211
|
35 |
|
$definition = array_merge( |
212
|
35 |
|
$class === null ? [] : [ArrayDefinition::CLASS_NAME => $class], |
213
|
35 |
|
[ArrayDefinition::CONSTRUCTOR => $constructorArguments], |
214
|
|
|
$methodsAndProperties, |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
88 |
|
if ($definition instanceof ExtensibleService) { |
219
|
|
|
throw new InvalidConfigException('Invalid definition. ExtensibleService is only allowed in provider extensions.'); |
220
|
|
|
} |
221
|
|
|
|
222
|
88 |
|
DefinitionValidator::validate($definition, $id); |
223
|
88 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @throws InvalidConfigException |
227
|
|
|
*/ |
228
|
88 |
|
private function validateMeta(array $meta): void |
229
|
|
|
{ |
230
|
88 |
|
foreach ($meta as $key => $_value) { |
231
|
16 |
|
if (!in_array($key, self::ALLOWED_META, true)) { |
232
|
3 |
|
throw new InvalidConfigException( |
233
|
3 |
|
sprintf( |
234
|
3 |
|
'Invalid definition: metadata "%s" is not allowed. Did you mean "%s()" or "$%s"?', |
235
|
|
|
$key, |
236
|
|
|
$key, |
237
|
|
|
$key, |
238
|
|
|
) |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
} |
242
|
88 |
|
} |
243
|
|
|
|
244
|
8 |
|
private function validateTags(array $tags): void |
245
|
|
|
{ |
246
|
8 |
|
foreach ($tags as $tag) { |
247
|
8 |
|
if (!is_string($tag)) { |
248
|
|
|
throw new InvalidConfigException('Invalid tag. Expected a string, got ' . var_export($tag, true) . '.'); |
249
|
|
|
} |
250
|
|
|
} |
251
|
8 |
|
} |
252
|
|
|
|
253
|
8 |
|
private function setTags(string $id, array $tags): void |
254
|
|
|
{ |
255
|
8 |
|
foreach ($tags as $tag) { |
256
|
8 |
|
if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag], true)) { |
257
|
8 |
|
$this->tags[$tag][] = $id; |
258
|
|
|
} |
259
|
|
|
} |
260
|
8 |
|
} |
261
|
|
|
|
262
|
4 |
|
private function setResetter(string $id, Closure $resetter): void |
263
|
|
|
{ |
264
|
4 |
|
$this->resetters[$id] = $resetter; |
265
|
4 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Creates new instance by either interface name or alias. |
269
|
|
|
* |
270
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
271
|
|
|
* |
272
|
|
|
* @throws CircularReferenceException |
273
|
|
|
* @throws InvalidConfigException |
274
|
|
|
* @throws NotFoundException |
275
|
|
|
* |
276
|
|
|
* @return mixed|object New built instance of the specified class. |
277
|
|
|
* |
278
|
|
|
* @internal |
279
|
|
|
*/ |
280
|
78 |
|
private function build(string $id) |
281
|
|
|
{ |
282
|
78 |
|
if ($this->isTagAlias($id)) { |
283
|
9 |
|
return $this->getTaggedServices($id); |
284
|
|
|
} |
285
|
|
|
|
286
|
77 |
|
if (isset($this->building[$id])) { |
287
|
75 |
|
if ($id === ContainerInterface::class) { |
288
|
75 |
|
return $this; |
289
|
|
|
} |
290
|
7 |
|
throw new CircularReferenceException(sprintf( |
291
|
7 |
|
'Circular reference to "%s" detected while building: %s.', |
292
|
|
|
$id, |
293
|
7 |
|
implode(',', array_keys($this->building)) |
294
|
|
|
)); |
295
|
|
|
} |
296
|
|
|
|
297
|
77 |
|
$this->building[$id] = 1; |
298
|
|
|
try { |
299
|
77 |
|
$object = $this->buildInternal($id); |
300
|
75 |
|
} finally { |
301
|
77 |
|
unset($this->building[$id]); |
302
|
|
|
} |
303
|
|
|
|
304
|
75 |
|
return $object; |
305
|
|
|
} |
306
|
|
|
|
307
|
82 |
|
private function isTagAlias(string $id): bool |
308
|
|
|
{ |
309
|
82 |
|
return strpos($id, 'tag@') === 0; |
310
|
|
|
} |
311
|
|
|
|
312
|
9 |
|
private function getTaggedServices(string $tagAlias): array |
313
|
|
|
{ |
314
|
9 |
|
$tag = substr($tagAlias, 4); |
315
|
9 |
|
$services = []; |
316
|
9 |
|
if (isset($this->tags[$tag])) { |
317
|
8 |
|
foreach ($this->tags[$tag] as $service) { |
318
|
8 |
|
$services[] = $this->get($service); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
9 |
|
return $services; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string $id |
327
|
|
|
* |
328
|
|
|
* @throws InvalidConfigException |
329
|
|
|
* @throws NotFoundException |
330
|
|
|
* |
331
|
|
|
* @return mixed|object |
332
|
|
|
*/ |
333
|
77 |
|
private function buildInternal(string $id) |
334
|
|
|
{ |
335
|
77 |
|
if (!isset($this->definitions[$id])) { |
336
|
44 |
|
return $this->buildPrimitive($id); |
337
|
|
|
} |
338
|
75 |
|
$definition = DefinitionNormalizer::normalize($this->definitions[$id], $id); |
339
|
75 |
|
$this->dependencyResolver = $this->dependencyResolver ?? new DependencyResolver($this->get(ContainerInterface::class)); |
340
|
|
|
|
341
|
75 |
|
return $definition->resolve($this->dependencyResolver); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param string $class |
346
|
|
|
* |
347
|
|
|
* @throws InvalidConfigException |
348
|
|
|
* @throws NotFoundException |
349
|
|
|
* |
350
|
|
|
* @return mixed|object |
351
|
|
|
*/ |
352
|
44 |
|
private function buildPrimitive(string $class) |
353
|
|
|
{ |
354
|
44 |
|
if (class_exists($class)) { |
355
|
42 |
|
$definition = ArrayDefinition::fromPreparedData($class); |
356
|
42 |
|
$this->dependencyResolver = $this->dependencyResolver ?? new DependencyResolver($this->get(ContainerInterface::class)); |
357
|
|
|
|
358
|
42 |
|
return $definition->resolve($this->dependencyResolver); |
359
|
|
|
} |
360
|
|
|
|
361
|
3 |
|
throw new NotFoundException($class); |
362
|
|
|
} |
363
|
|
|
|
364
|
82 |
|
private function addProviders(array $providers): void |
365
|
|
|
{ |
366
|
82 |
|
$extensions = []; |
367
|
82 |
|
foreach ($providers as $provider) { |
368
|
3 |
|
$providerInstance = $this->buildProvider($provider); |
369
|
3 |
|
$extensions[] = $providerInstance->getExtensions(); |
370
|
3 |
|
$this->addProviderDefinitions($providerInstance); |
371
|
|
|
} |
372
|
|
|
|
373
|
82 |
|
foreach ($extensions as $providerExtensions) { |
374
|
3 |
|
foreach ($providerExtensions as $id => $extension) { |
375
|
2 |
|
if (!isset($this->definitions[$id])) { |
376
|
|
|
throw new InvalidConfigException("Extended service \"$id\" doesn't exist."); |
377
|
|
|
} |
378
|
|
|
|
379
|
2 |
|
if (!$this->definitions[$id] instanceof ExtensibleService) { |
380
|
2 |
|
$this->definitions[$id] = new ExtensibleService($this->definitions[$id]); |
381
|
|
|
} |
382
|
|
|
|
383
|
2 |
|
$this->definitions[$id]->addExtension($extension); |
384
|
|
|
} |
385
|
|
|
} |
386
|
82 |
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Adds service provider definitions to the container. |
390
|
|
|
* |
391
|
|
|
* @param mixed $providerDefinition |
392
|
|
|
* |
393
|
|
|
* @throws InvalidConfigException |
394
|
|
|
* @throws NotInstantiableException |
395
|
|
|
* |
396
|
|
|
* @see ServiceProviderInterface |
397
|
|
|
*/ |
398
|
3 |
|
private function addProviderDefinitions($provider): void |
399
|
|
|
{ |
400
|
3 |
|
$definitions = $provider->getDefinitions(); |
401
|
3 |
|
$this->setMultiple($definitions); |
402
|
3 |
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Builds service provider by definition. |
406
|
|
|
* |
407
|
|
|
* @param mixed $providerDefinition class name or definition of provider. |
408
|
|
|
* |
409
|
|
|
* @throws InvalidConfigException |
410
|
|
|
* |
411
|
|
|
* @return ServiceProviderInterface instance of service provider; |
412
|
|
|
*/ |
413
|
3 |
|
private function buildProvider($provider): ServiceProviderInterface |
414
|
|
|
{ |
415
|
3 |
|
if ($this->validate) { |
416
|
3 |
|
if (!is_string($provider)) { |
417
|
2 |
|
new InvalidConfigException( |
418
|
2 |
|
sprintf( |
419
|
2 |
|
'Service provider should be a class name or an instance of %s. %s given.', |
420
|
2 |
|
ServiceProviderInterface::class, |
421
|
2 |
|
$this->getVariableType($provider) |
422
|
|
|
) |
423
|
|
|
); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
3 |
|
$providerInstance = is_object($provider) ? $provider : new $provider(); |
428
|
3 |
|
if (!$providerInstance instanceof ServiceProviderInterface) { |
429
|
|
|
new InvalidConfigException( |
430
|
|
|
sprintf( |
431
|
|
|
'Service provider should be an instance of %s. %s given.', |
432
|
|
|
ServiceProviderInterface::class, |
433
|
|
|
$this->getVariableType($providerInstance) |
434
|
|
|
) |
435
|
|
|
); |
436
|
|
|
} |
437
|
|
|
|
438
|
3 |
|
return $providerInstance; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @param mixed $variable |
443
|
|
|
*/ |
444
|
3 |
|
private function getVariableType($variable): string |
445
|
|
|
{ |
446
|
3 |
|
if (is_object($variable)) { |
447
|
2 |
|
return get_class($variable); |
448
|
|
|
} |
449
|
|
|
|
450
|
1 |
|
return gettype($variable); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths