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