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