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