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\Normalizer; |
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 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 extends AbstractContainerConfigurator implements ContainerInterface |
32
|
|
|
{ |
33
|
|
|
private const META_TAGS = 'tags'; |
34
|
|
|
private const META_RESET = 'reset'; |
35
|
|
|
private const ALLOWED_META = ['tags', '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
|
|
|
* @var object[] |
48
|
|
|
*/ |
49
|
|
|
private array $instances = []; |
50
|
|
|
|
51
|
|
|
private array $tags; |
52
|
|
|
|
53
|
|
|
private array $resetters = []; |
54
|
|
|
|
55
|
|
|
private ?CompositeContainer $rootContainer = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Container constructor. |
59
|
|
|
* |
60
|
|
|
* @param array $definitions Definitions to put into container. |
61
|
|
|
* @param ServiceProviderInterface[]|string[] $providers Service providers |
62
|
|
|
* to get definitions from. |
63
|
|
|
* @param ContainerInterface|null $rootContainer Root container to delegate |
64
|
|
|
* lookup to when resolving dependencies. If provided the current container |
65
|
|
|
* is no longer queried for dependencies. |
66
|
|
|
* |
67
|
|
|
* @throws InvalidConfigException |
68
|
|
|
*/ |
69
|
93 |
|
public function __construct( |
70
|
|
|
array $definitions = [], |
71
|
|
|
array $providers = [], |
72
|
|
|
array $tags = [], |
73
|
|
|
ContainerInterface $rootContainer = null |
74
|
|
|
) { |
75
|
93 |
|
$this->tags = $tags; |
76
|
93 |
|
$this->delegateLookup($rootContainer); |
77
|
93 |
|
$this->setDefaultDefinitions(); |
78
|
93 |
|
$this->setMultiple($definitions); |
79
|
90 |
|
$this->addProviders($providers); |
80
|
|
|
|
81
|
|
|
// Prevent circular reference to ContainerInterface |
82
|
88 |
|
$this->get(ContainerInterface::class); |
83
|
88 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
87
|
|
|
* |
88
|
|
|
* @param string $id class name, interface name or alias name |
89
|
|
|
* |
90
|
|
|
* @return bool whether the container is able to provide instance of class specified. |
91
|
|
|
* |
92
|
|
|
* @see set() |
93
|
|
|
*/ |
94
|
32 |
|
public function has($id): bool |
95
|
|
|
{ |
96
|
32 |
|
if ($this->isTagAlias($id)) { |
97
|
2 |
|
$tag = substr($id, 4); |
98
|
2 |
|
return isset($this->tags[$tag]); |
99
|
|
|
} |
100
|
|
|
|
101
|
30 |
|
return isset($this->definitions[$id]) || class_exists($id); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns an instance by either interface name or alias. |
106
|
|
|
* |
107
|
|
|
* Same instance of the class will be returned each time this method is called. |
108
|
|
|
* |
109
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
110
|
|
|
* |
111
|
|
|
* @throws CircularReferenceException |
112
|
|
|
* @throws InvalidConfigException |
113
|
|
|
* @throws NotFoundException |
114
|
|
|
* @throws NotInstantiableException |
115
|
|
|
* |
116
|
|
|
* @return mixed|object An instance of the requested interface. |
117
|
|
|
* |
118
|
|
|
* @psalm-template T |
119
|
|
|
* @psalm-param string|class-string<T> $id |
120
|
|
|
* @psalm-return ($id is class-string ? T : mixed) |
121
|
|
|
*/ |
122
|
89 |
|
public function get($id) |
123
|
|
|
{ |
124
|
89 |
|
if ($id === StateResetter::class && !isset($this->definitions[$id])) { |
125
|
4 |
|
$resetters = []; |
126
|
4 |
|
foreach ($this->resetters as $serviceId => $callback) { |
127
|
4 |
|
if (isset($this->instances[$serviceId])) { |
128
|
4 |
|
$resetters[] = $callback->bindTo($this->instances[$serviceId], get_class($this->instances[$serviceId])); |
129
|
|
|
} |
130
|
|
|
} |
131
|
4 |
|
return new StateResetter($resetters, $this); |
132
|
|
|
} |
133
|
|
|
|
134
|
89 |
|
if (!array_key_exists($id, $this->instances)) { |
135
|
89 |
|
$this->instances[$id] = $this->build($id); |
136
|
|
|
} |
137
|
|
|
|
138
|
89 |
|
return $this->instances[$id]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Delegate service lookup to another container. |
143
|
|
|
* |
144
|
|
|
* @param ContainerInterface $container |
145
|
|
|
*/ |
146
|
93 |
|
protected function delegateLookup(?ContainerInterface $container): void |
147
|
|
|
{ |
148
|
93 |
|
if ($container === null) { |
149
|
93 |
|
return; |
150
|
|
|
} |
151
|
8 |
|
if ($this->rootContainer === null) { |
152
|
8 |
|
$this->rootContainer = new CompositeContainer(); |
153
|
8 |
|
$this->setDefaultDefinitions(); |
154
|
|
|
} |
155
|
|
|
|
156
|
8 |
|
$this->rootContainer->attach($container); |
|
|
|
|
157
|
8 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets a definition to the container. Definition may be defined multiple ways. |
161
|
|
|
* |
162
|
|
|
* @param string $id |
163
|
|
|
* @param mixed $definition |
164
|
|
|
* |
165
|
|
|
* @throws InvalidConfigException |
166
|
|
|
* |
167
|
|
|
* @see `Normalizer::normalize()` |
168
|
|
|
*/ |
169
|
93 |
|
protected function set(string $id, $definition): void |
170
|
|
|
{ |
171
|
93 |
|
[$definition, $meta] = Normalizer::parse($definition, self::ALLOWED_META); |
172
|
|
|
|
173
|
93 |
|
Normalizer::validate($definition); |
174
|
93 |
|
if (isset($meta[self::META_TAGS])) { |
175
|
8 |
|
$this->validateTags($meta[self::META_TAGS]); |
176
|
8 |
|
$this->setTags($id, $meta[self::META_TAGS]); |
177
|
|
|
} |
178
|
93 |
|
if (isset($meta[self::META_RESET])) { |
179
|
5 |
|
$this->setResetter($id, $meta[self::META_RESET]); |
180
|
|
|
} |
181
|
|
|
|
182
|
93 |
|
unset($this->instances[$id]); |
183
|
93 |
|
$this->definitions[$id] = $definition; |
184
|
93 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sets multiple definitions at once. |
188
|
|
|
* |
189
|
|
|
* @param array $config definitions indexed by their ids |
190
|
|
|
* |
191
|
|
|
* @throws InvalidConfigException |
192
|
|
|
*/ |
193
|
93 |
|
protected function setMultiple(array $config): void |
194
|
|
|
{ |
195
|
93 |
|
foreach ($config as $id => $definition) { |
196
|
93 |
|
if (!is_string($id)) { |
197
|
1 |
|
throw new InvalidConfigException(sprintf('Key must be a string. %s given.', $this->getVariableType($id))); |
198
|
|
|
} |
199
|
93 |
|
$this->set($id, $definition); |
200
|
|
|
} |
201
|
93 |
|
} |
202
|
|
|
|
203
|
93 |
|
private function setDefaultDefinitions(): void |
204
|
|
|
{ |
205
|
93 |
|
$container = $this->rootContainer ?? $this; |
206
|
93 |
|
$this->setMultiple([ |
207
|
93 |
|
ContainerInterface::class => $container, |
208
|
93 |
|
Injector::class => new Injector($container), |
209
|
|
|
]); |
210
|
93 |
|
} |
211
|
|
|
|
212
|
8 |
|
private function validateTags(array $tags): void |
213
|
|
|
{ |
214
|
8 |
|
foreach ($tags as $tag) { |
215
|
8 |
|
if (!is_string($tag)) { |
216
|
|
|
throw new InvalidConfigException('Invalid tag. Expected a string, got ' . var_export($tag, true) . '.'); |
217
|
|
|
} |
218
|
|
|
} |
219
|
8 |
|
} |
220
|
|
|
|
221
|
8 |
|
private function setTags(string $id, array $tags): void |
222
|
|
|
{ |
223
|
8 |
|
foreach ($tags as $tag) { |
224
|
8 |
|
if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag], true)) { |
225
|
8 |
|
$this->tags[$tag][] = $id; |
226
|
|
|
} |
227
|
|
|
} |
228
|
8 |
|
} |
229
|
|
|
|
230
|
4 |
|
private function setResetter(string $id, Closure $resetter): void |
231
|
|
|
{ |
232
|
4 |
|
$this->resetters[$id] = $resetter; |
233
|
4 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Creates new instance by either interface name or alias. |
237
|
|
|
* |
238
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
239
|
|
|
* |
240
|
|
|
* @throws CircularReferenceException |
241
|
|
|
* @throws InvalidConfigException |
242
|
|
|
* @throws NotFoundException |
243
|
|
|
* |
244
|
|
|
* @return mixed|object New built instance of the specified class. |
245
|
|
|
* |
246
|
|
|
* @internal |
247
|
|
|
*/ |
248
|
89 |
|
private function build(string $id) |
249
|
|
|
{ |
250
|
89 |
|
if ($this->isTagAlias($id)) { |
251
|
9 |
|
return $this->getTaggedServices($id); |
252
|
|
|
} |
253
|
|
|
|
254
|
89 |
|
if (isset($this->building[$id])) { |
255
|
9 |
|
if ($id === ContainerInterface::class) { |
256
|
2 |
|
return $this; |
257
|
|
|
} |
258
|
7 |
|
throw new CircularReferenceException(sprintf( |
259
|
7 |
|
'Circular reference to "%s" detected while building: %s.', |
260
|
|
|
$id, |
261
|
7 |
|
implode(',', array_keys($this->building)) |
262
|
|
|
)); |
263
|
|
|
} |
264
|
|
|
|
265
|
89 |
|
$this->building[$id] = 1; |
266
|
|
|
try { |
267
|
89 |
|
$object = $this->buildInternal($id); |
268
|
89 |
|
} finally { |
269
|
89 |
|
unset($this->building[$id]); |
270
|
|
|
} |
271
|
|
|
|
272
|
89 |
|
return $object; |
273
|
|
|
} |
274
|
|
|
|
275
|
89 |
|
private function isTagAlias(string $id): bool |
276
|
|
|
{ |
277
|
89 |
|
return strpos($id, 'tag@') === 0; |
278
|
|
|
} |
279
|
|
|
|
280
|
9 |
|
private function getTaggedServices(string $tagAlias): array |
281
|
|
|
{ |
282
|
9 |
|
$tag = substr($tagAlias, 4); |
283
|
9 |
|
$services = []; |
284
|
9 |
|
if (isset($this->tags[$tag])) { |
285
|
8 |
|
foreach ($this->tags[$tag] as $service) { |
286
|
8 |
|
$services[] = $this->get($service); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
9 |
|
return $services; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param mixed $definition |
295
|
|
|
*/ |
296
|
89 |
|
private function processDefinition($definition): void |
297
|
|
|
{ |
298
|
89 |
|
if ($definition instanceof DeferredServiceProviderInterface) { |
299
|
1 |
|
$definition->register($this); |
300
|
|
|
} |
301
|
89 |
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param string $id |
305
|
|
|
* |
306
|
|
|
* @throws InvalidConfigException |
307
|
|
|
* @throws NotFoundException |
308
|
|
|
* |
309
|
|
|
* @return mixed|object |
310
|
|
|
*/ |
311
|
89 |
|
private function buildInternal(string $id) |
312
|
|
|
{ |
313
|
89 |
|
if (!isset($this->definitions[$id])) { |
314
|
51 |
|
return $this->buildPrimitive($id); |
315
|
|
|
} |
316
|
89 |
|
$this->processDefinition($this->definitions[$id]); |
317
|
89 |
|
$definition = Normalizer::normalize($this->definitions[$id], $id, [], false); |
318
|
|
|
|
319
|
89 |
|
return $definition->resolve($this->rootContainer ?? $this); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $class |
324
|
|
|
* |
325
|
|
|
* @throws InvalidConfigException |
326
|
|
|
* @throws NotFoundException |
327
|
|
|
* |
328
|
|
|
* @return mixed|object |
329
|
|
|
*/ |
330
|
51 |
|
private function buildPrimitive(string $class) |
331
|
|
|
{ |
332
|
51 |
|
if (class_exists($class)) { |
333
|
49 |
|
$definition = new ArrayDefinition([ArrayDefinition::CLASS_NAME => $class], false); |
334
|
|
|
|
335
|
49 |
|
return $definition->resolve($this->rootContainer ?? $this); |
336
|
|
|
} |
337
|
|
|
|
338
|
4 |
|
throw new NotFoundException($class); |
339
|
|
|
} |
340
|
|
|
|
341
|
90 |
|
private function addProviders(array $providers): void |
342
|
|
|
{ |
343
|
90 |
|
foreach ($providers as $provider) { |
344
|
6 |
|
$this->addProvider($provider); |
345
|
|
|
} |
346
|
88 |
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Adds service provider to the container. Unless service provider is deferred |
350
|
|
|
* it would be immediately registered. |
351
|
|
|
* |
352
|
|
|
* @param mixed $providerDefinition |
353
|
|
|
* |
354
|
|
|
* @throws InvalidConfigException |
355
|
|
|
* @throws NotInstantiableException |
356
|
|
|
* |
357
|
|
|
* @see ServiceProviderInterface |
358
|
|
|
* @see DeferredServiceProviderInterface |
359
|
|
|
*/ |
360
|
6 |
|
private function addProvider($providerDefinition): void |
361
|
|
|
{ |
362
|
6 |
|
$provider = $this->buildProvider($providerDefinition); |
363
|
|
|
|
364
|
5 |
|
if ($provider instanceof DeferredServiceProviderInterface) { |
365
|
1 |
|
foreach ($provider->provides() as $id) { |
366
|
1 |
|
$this->definitions[$id] = $provider; |
367
|
|
|
} |
368
|
|
|
} else { |
369
|
4 |
|
$provider->register($this); |
370
|
|
|
} |
371
|
4 |
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Builds service provider by definition. |
375
|
|
|
* |
376
|
|
|
* @param mixed $providerDefinition class name or definition of provider. |
377
|
|
|
* |
378
|
|
|
* @throws InvalidConfigException |
379
|
|
|
* |
380
|
|
|
* @return ServiceProviderInterface instance of service provider; |
381
|
|
|
*/ |
382
|
6 |
|
private function buildProvider($providerDefinition): ServiceProviderInterface |
383
|
|
|
{ |
384
|
6 |
|
$provider = Normalizer::normalize($providerDefinition)->resolve($this); |
385
|
5 |
|
assert($provider instanceof ServiceProviderInterface, new InvalidConfigException( |
386
|
5 |
|
sprintf( |
387
|
5 |
|
'Service provider should be an instance of %s. %s given.', |
388
|
5 |
|
ServiceProviderInterface::class, |
389
|
5 |
|
$this->getVariableType($provider) |
390
|
|
|
) |
391
|
|
|
)); |
392
|
|
|
|
393
|
5 |
|
return $provider; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @param mixed $variable |
398
|
|
|
*/ |
399
|
6 |
|
private function getVariableType($variable): string |
400
|
|
|
{ |
401
|
6 |
|
if (is_object($variable)) { |
402
|
5 |
|
return get_class($variable); |
403
|
|
|
} |
404
|
|
|
|
405
|
1 |
|
return gettype($variable); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
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.