1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Di\Contracts\DeferredServiceProviderInterface; |
9
|
|
|
use Yiisoft\Di\Contracts\ServiceProviderInterface; |
10
|
|
|
use Yiisoft\Factory\Exceptions\CircularReferenceException; |
11
|
|
|
use Yiisoft\Factory\Exceptions\InvalidConfigException; |
12
|
|
|
use Yiisoft\Factory\Exceptions\NotFoundException; |
13
|
|
|
use Yiisoft\Factory\Exceptions\NotInstantiableException; |
14
|
|
|
use Yiisoft\Factory\Definitions\Normalizer; |
15
|
|
|
use Yiisoft\Factory\Definitions\ArrayDefinition; |
16
|
|
|
use Yiisoft\Injector\Injector; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Container implements a [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) container. |
20
|
|
|
*/ |
21
|
|
|
final class Container extends AbstractContainerConfigurator implements ContainerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array object definitions indexed by their types |
25
|
|
|
*/ |
26
|
|
|
private array $definitions = []; |
27
|
|
|
/** |
28
|
|
|
* @var array used to collect ids instantiated during build |
29
|
|
|
* to detect circular references |
30
|
|
|
*/ |
31
|
|
|
private array $building = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var object[] |
35
|
|
|
*/ |
36
|
|
|
private array $instances = []; |
37
|
|
|
|
38
|
|
|
private array $tags; |
39
|
|
|
|
40
|
|
|
private ?CompositeContainer $rootContainer = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Container constructor. |
44
|
|
|
* |
45
|
|
|
* @param array $definitions Definitions to put into container. |
46
|
|
|
* @param ServiceProviderInterface[]|string[] $providers Service providers to get definitions from. |
47
|
|
|
* |
48
|
|
|
* @param ContainerInterface|null $rootContainer Root container to delegate lookup to in case definition |
49
|
|
|
* is not found in current container. |
50
|
65 |
|
* @throws InvalidConfigException |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
array $definitions = [], |
54
|
|
|
array $providers = [], |
55
|
65 |
|
array $tags = [], |
56
|
65 |
|
ContainerInterface $rootContainer = null |
57
|
63 |
|
) { |
58
|
63 |
|
|
59
|
|
|
$this->tags = $tags; |
60
|
63 |
|
$this->delegateLookup($rootContainer); |
61
|
|
|
|
62
|
|
|
$this->setMultiple($definitions); |
63
|
62 |
|
if (!$this->has(ContainerInterface::class)) { |
64
|
62 |
|
$this->set(ContainerInterface::class, $rootContainer ?? $this); |
65
|
|
|
} |
66
|
|
|
$this->addProviders($providers); |
67
|
|
|
|
68
|
|
|
# Prevent circular reference to ContainerInterface |
69
|
|
|
$this->get(ContainerInterface::class); |
70
|
|
|
} |
71
|
|
|
|
72
|
63 |
|
/** |
73
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
74
|
63 |
|
* @param string $id class name, interface name or alias name |
75
|
|
|
* @return bool whether the container is able to provide instance of class specified. |
76
|
|
|
* @see set() |
77
|
|
|
*/ |
78
|
|
|
public function has($id): bool |
79
|
|
|
{ |
80
|
|
|
if ($this->isTagAlias($id)) { |
81
|
|
|
$tag = substr($id, 4); |
82
|
|
|
return isset($this->tags[$tag]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return isset($this->definitions[$id]) || class_exists($id); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
62 |
|
* Returns an instance by either interface name or alias. |
90
|
|
|
* |
91
|
62 |
|
* Same instance of the class will be returned each time this method is called. |
92
|
62 |
|
* |
93
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
94
|
|
|
* @return object An instance of the requested interface. |
95
|
62 |
|
* @throws CircularReferenceException |
96
|
|
|
* @throws InvalidConfigException |
97
|
|
|
* @throws NotFoundException |
98
|
|
|
* @throws NotInstantiableException |
99
|
|
|
*/ |
100
|
|
|
public function get($id) |
101
|
|
|
{ |
102
|
65 |
|
if (!isset($this->instances[$id])) { |
103
|
|
|
$this->instances[$id] = $this->build($id); |
104
|
65 |
|
} |
105
|
65 |
|
|
106
|
|
|
return $this->instances[$id]; |
107
|
7 |
|
} |
108
|
7 |
|
|
109
|
|
|
/** |
110
|
|
|
* Delegate service lookup to another container. |
111
|
7 |
|
* @param ContainerInterface $container |
112
|
7 |
|
*/ |
113
|
|
|
protected function delegateLookup(?ContainerInterface $container): void |
114
|
|
|
{ |
115
|
|
|
if ($container === null) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
if ($this->rootContainer === null) { |
119
|
|
|
$this->rootContainer = new CompositeContainer(); |
120
|
|
|
} |
121
|
64 |
|
|
122
|
|
|
$this->rootContainer->attach($container); |
|
|
|
|
123
|
64 |
|
} |
124
|
63 |
|
|
125
|
63 |
|
/** |
126
|
63 |
|
* Sets a definition to the container. Definition may be defined multiple ways. |
127
|
|
|
* @param string $id |
128
|
|
|
* @param mixed $definition |
129
|
|
|
* @throws InvalidConfigException |
130
|
|
|
* @see `Normalizer::normalize()` |
131
|
|
|
*/ |
132
|
|
|
protected function set(string $id, $definition): void |
133
|
65 |
|
{ |
134
|
|
|
$tags = $this->extractTags($definition); |
135
|
65 |
|
$definition = $this->extractDefinition($definition); |
136
|
54 |
|
Normalizer::validate($definition); |
137
|
1 |
|
$this->setTags($id, $tags); |
138
|
|
|
|
139
|
53 |
|
$this->instances[$id] = null; |
140
|
|
|
$this->definitions[$id] = $definition; |
141
|
63 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Sets multiple definitions at once. |
145
|
|
|
* @param array $config definitions indexed by their ids |
146
|
|
|
* @throws InvalidConfigException |
147
|
|
|
*/ |
148
|
|
|
protected function setMultiple(array $config): void |
149
|
|
|
{ |
150
|
|
|
foreach ($config as $id => $definition) { |
151
|
|
|
if (!is_string($id)) { |
152
|
|
|
throw new InvalidConfigException('Key must be a string'); |
153
|
62 |
|
} |
154
|
|
|
$this->set($id, $definition); |
155
|
62 |
|
} |
156
|
12 |
|
} |
157
|
|
|
|
158
|
62 |
|
private function extractDefinition($definition) |
159
|
9 |
|
{ |
160
|
2 |
|
if (is_array($definition) && isset($definition['__definition'])) { |
161
|
|
|
$definition = $definition['__definition']; |
162
|
7 |
|
} |
163
|
7 |
|
|
164
|
|
|
return $definition; |
165
|
7 |
|
} |
166
|
|
|
|
167
|
|
|
private function extractTags($definition): array |
168
|
|
|
{ |
169
|
62 |
|
if (is_array($definition) && isset($definition['__tags']) && is_array($definition['__tags'])) { |
170
|
62 |
|
$this->checkTags($definition['__tags']); |
171
|
62 |
|
return $definition['__tags']; |
172
|
|
|
} |
173
|
62 |
|
|
174
|
|
|
return []; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function checkTags(array $tags): void |
178
|
|
|
{ |
179
|
62 |
|
foreach ($tags as $tag) { |
180
|
|
|
if (!(is_string($tag))) { |
181
|
62 |
|
throw new InvalidConfigException('Invalid tag: ' . var_export($tag, true)); |
182
|
1 |
|
} |
183
|
|
|
} |
184
|
62 |
|
} |
185
|
|
|
|
186
|
|
|
private function setTags(string $id, array $tags): void |
187
|
|
|
{ |
188
|
|
|
foreach ($tags as $tag) { |
189
|
|
|
if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag])) { |
190
|
|
|
$this->tags[$tag][] = $id; |
191
|
|
|
} |
192
|
|
|
} |
193
|
62 |
|
} |
194
|
|
|
|
195
|
62 |
|
/** |
196
|
41 |
|
* Creates new instance by either interface name or alias. |
197
|
|
|
* |
198
|
62 |
|
* @param string $id The interface or an alias name that was previously registered. |
199
|
62 |
|
* @return mixed|object New built instance of the specified class. |
200
|
|
|
* @throws CircularReferenceException |
201
|
62 |
|
* @throws InvalidConfigException |
202
|
|
|
* @throws NotFoundException |
203
|
|
|
* @internal |
204
|
|
|
*/ |
205
|
|
|
private function build(string $id) |
206
|
|
|
{ |
207
|
|
|
if ($this->isTagAlias($id)) { |
208
|
|
|
return $this->getTaggedServices($id); |
209
|
|
|
} |
210
|
|
|
|
211
|
41 |
|
if ($id === Injector::class) { |
212
|
|
|
return new Injector($this); |
213
|
41 |
|
} |
214
|
39 |
|
|
215
|
|
|
if (isset($this->building[$id])) { |
216
|
39 |
|
if ($id === ContainerInterface::class) { |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
4 |
|
throw new CircularReferenceException(sprintf( |
220
|
|
|
'Circular reference to "%s" detected while building: %s', |
221
|
|
|
$id, |
222
|
63 |
|
implode(',', array_keys($this->building)) |
223
|
|
|
)); |
224
|
63 |
|
} |
225
|
5 |
|
|
226
|
|
|
$this->building[$id] = 1; |
227
|
62 |
|
$object = $this->buildInternal($id); |
228
|
|
|
unset($this->building[$id]); |
229
|
|
|
|
230
|
|
|
return $object; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
private function isTagAlias(string $id): bool |
234
|
|
|
{ |
235
|
|
|
return strpos($id, 'tag@') === 0; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
private function getTaggedServices(string $tagAlias): array |
239
|
|
|
{ |
240
|
5 |
|
$tag = substr($tagAlias, 4); |
241
|
|
|
$services = []; |
242
|
5 |
|
if (isset($this->tags[$tag])) { |
243
|
|
|
foreach ($this->tags[$tag] as $service) { |
244
|
4 |
|
$services[] = $this->get($service); |
245
|
1 |
|
} |
246
|
1 |
|
} |
247
|
|
|
|
248
|
|
|
return $services; |
249
|
3 |
|
} |
250
|
|
|
|
251
|
4 |
|
/** |
252
|
|
|
* @param mixed $definition |
253
|
|
|
*/ |
254
|
|
|
private function processDefinition($definition): void |
255
|
|
|
{ |
256
|
|
|
if ($definition instanceof DeferredServiceProviderInterface) { |
257
|
|
|
$definition->register($this); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
5 |
|
/** |
262
|
|
|
* @param string $id |
263
|
5 |
|
* |
264
|
|
|
* @return mixed|object |
265
|
|
|
* @throws InvalidConfigException |
266
|
|
|
* @throws NotFoundException |
267
|
|
|
*/ |
268
|
4 |
|
private function buildInternal(string $id) |
269
|
|
|
{ |
270
|
|
|
if (!isset($this->definitions[$id])) { |
271
|
|
|
return $this->buildPrimitive($id); |
272
|
|
|
} |
273
|
|
|
$this->processDefinition($this->definitions[$id]); |
274
|
|
|
$definition = Normalizer::normalize($this->definitions[$id], $id); |
275
|
|
|
|
276
|
|
|
return $definition->resolve($this->rootContainer ?? $this); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $class |
281
|
|
|
* |
282
|
|
|
* @return mixed|object |
283
|
|
|
* @throws InvalidConfigException |
284
|
|
|
* @throws NotFoundException |
285
|
|
|
*/ |
286
|
|
|
private function buildPrimitive(string $class) |
287
|
|
|
{ |
288
|
|
|
if (class_exists($class)) { |
289
|
|
|
$definition = new ArrayDefinition($class); |
290
|
|
|
|
291
|
|
|
return $definition->resolve($this->rootContainer ?? $this); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
throw new NotFoundException("No definition for $class"); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
private function addProviders(array $providers): void |
298
|
|
|
{ |
299
|
|
|
foreach ($providers as $provider) { |
300
|
|
|
$this->addProvider($provider); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Adds service provider to the container. Unless service provider is deferred |
306
|
|
|
* it would be immediately registered. |
307
|
|
|
* |
308
|
|
|
* @param mixed $providerDefinition |
309
|
|
|
* |
310
|
|
|
* @throws InvalidConfigException |
311
|
|
|
* @throws NotInstantiableException |
312
|
|
|
* @see ServiceProviderInterface |
313
|
|
|
* @see DeferredServiceProviderInterface |
314
|
|
|
*/ |
315
|
|
|
private function addProvider($providerDefinition): void |
316
|
|
|
{ |
317
|
|
|
$provider = $this->buildProvider($providerDefinition); |
318
|
|
|
|
319
|
|
|
if ($provider instanceof DeferredServiceProviderInterface) { |
320
|
|
|
foreach ($provider->provides() as $id) { |
321
|
|
|
$this->definitions[$id] = $provider; |
322
|
|
|
} |
323
|
|
|
} else { |
324
|
|
|
$provider->register($this); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Builds service provider by definition. |
330
|
|
|
* |
331
|
|
|
* @param mixed $providerDefinition class name or definition of provider. |
332
|
|
|
* @return ServiceProviderInterface instance of service provider; |
333
|
|
|
* |
334
|
|
|
* @throws InvalidConfigException |
335
|
|
|
*/ |
336
|
|
|
private function buildProvider($providerDefinition): ServiceProviderInterface |
337
|
|
|
{ |
338
|
|
|
$provider = Normalizer::normalize($providerDefinition)->resolve($this); |
339
|
|
|
assert($provider instanceof ServiceProviderInterface, new InvalidConfigException( |
340
|
|
|
'Service provider should be an instance of ' . ServiceProviderInterface::class |
341
|
|
|
)); |
342
|
|
|
|
343
|
|
|
return $provider; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
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.