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