|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
use Yiisoft\Definitions\ArrayDefinition; |
|
13
|
|
|
use Yiisoft\Definitions\DefinitionStorage; |
|
14
|
|
|
use Yiisoft\Definitions\Contract\DefinitionInterface; |
|
15
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
|
16
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
17
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
|
18
|
|
|
use Yiisoft\Definitions\Helpers\DefinitionValidator; |
|
19
|
|
|
use Yiisoft\Definitions\LazyDefinition; |
|
20
|
|
|
use Yiisoft\Di\Helpers\DefinitionNormalizer; |
|
21
|
|
|
use Yiisoft\Di\Helpers\DefinitionParser; |
|
22
|
|
|
use Yiisoft\Di\Helpers\TagHelper; |
|
23
|
|
|
|
|
24
|
|
|
use function array_key_exists; |
|
25
|
|
|
use function array_keys; |
|
26
|
|
|
use function implode; |
|
27
|
|
|
use function in_array; |
|
28
|
|
|
use function is_array; |
|
29
|
|
|
use function is_callable; |
|
30
|
|
|
use function is_object; |
|
31
|
|
|
use function is_string; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Container implements a [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection) container. |
|
35
|
|
|
*/ |
|
36
|
|
|
final class Container implements ContainerInterface |
|
37
|
|
|
{ |
|
38
|
|
|
private const META_TAGS = 'tags'; |
|
39
|
|
|
private const META_RESET = 'reset'; |
|
40
|
|
|
private const META_LAZY = 'lazy'; |
|
41
|
|
|
private const ALLOWED_META = [self::META_TAGS, self::META_RESET, self::META_LAZY]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var DefinitionStorage Storage of object definitions. |
|
45
|
|
|
*/ |
|
46
|
|
|
private DefinitionStorage $definitions; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array Used to collect IDs of objects instantiated during build |
|
50
|
|
|
* to detect circular references. |
|
51
|
|
|
*/ |
|
52
|
|
|
private array $building = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var bool $validate If definitions should be validated. |
|
56
|
|
|
*/ |
|
57
|
|
|
private bool $validate; |
|
58
|
|
|
|
|
59
|
|
|
private array $instances = []; |
|
60
|
|
|
|
|
61
|
|
|
private CompositeContainer $delegates; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var array Tagged service IDs. The structure is `['tagID' => ['service1', 'service2']]`. |
|
65
|
|
|
* @psalm-var array<string, list<string>> |
|
66
|
|
|
*/ |
|
67
|
|
|
private array $tags; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var Closure[] |
|
71
|
|
|
*/ |
|
72
|
|
|
private array $resetters = []; |
|
73
|
|
|
private bool $useResettersFromMeta = true; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param ContainerConfigInterface $config Container configuration. |
|
77
|
139 |
|
* |
|
78
|
|
|
* @throws InvalidConfigException If configuration is not valid. |
|
79
|
139 |
|
*/ |
|
80
|
139 |
|
public function __construct(ContainerConfigInterface $config) |
|
81
|
139 |
|
{ |
|
82
|
139 |
|
$this->definitions = new DefinitionStorage( |
|
83
|
139 |
|
[ |
|
84
|
139 |
|
ContainerInterface::class => $this, |
|
85
|
139 |
|
StateResetter::class => StateResetter::class, |
|
86
|
139 |
|
], |
|
87
|
139 |
|
$config->useStrictMode() |
|
88
|
136 |
|
); |
|
89
|
126 |
|
$this->validate = $config->shouldValidate(); |
|
90
|
120 |
|
$this->setTags($config->getTags()); |
|
91
|
|
|
$this->addDefinitions($config->getDefinitions()); |
|
92
|
|
|
$this->addProviders($config->getProviders()); |
|
93
|
|
|
$this->setDelegates($config->getDelegates()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $id Class name, interface name or alias name. |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool Whether the container is able to provide instance of class specified. |
|
102
|
47 |
|
* |
|
103
|
|
|
* @see addDefinition() |
|
104
|
47 |
|
*/ |
|
105
|
3 |
|
public function has(string $id): bool |
|
106
|
3 |
|
{ |
|
107
|
|
|
if (TagHelper::isTagAlias($id)) { |
|
108
|
|
|
$tag = TagHelper::extractTagFromAlias($id); |
|
109
|
|
|
return isset($this->tags[$tag]); |
|
110
|
44 |
|
} |
|
111
|
3 |
|
|
|
112
|
3 |
|
try { |
|
113
|
|
|
return $this->definitions->has($id); |
|
114
|
|
|
} catch (CircularReferenceException) { |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns an instance by either interface name or alias. |
|
121
|
|
|
* |
|
122
|
|
|
* Same instance of the class will be returned each time this method is called. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
|
125
|
|
|
* |
|
126
|
|
|
* @throws CircularReferenceException |
|
127
|
|
|
* @throws InvalidConfigException |
|
128
|
|
|
* @throws NotFoundExceptionInterface |
|
129
|
|
|
* @throws NotInstantiableException |
|
130
|
|
|
* @throws BuildingException |
|
131
|
|
|
* |
|
132
|
|
|
* @return mixed|object An instance of the requested interface. |
|
133
|
|
|
* |
|
134
|
|
|
* @psalm-template T |
|
135
|
120 |
|
* @psalm-param string|class-string<T> $id |
|
136
|
|
|
* @psalm-return ($id is class-string ? T : mixed) |
|
137
|
120 |
|
*/ |
|
138
|
|
|
public function get(string $id) |
|
139
|
|
|
{ |
|
140
|
120 |
|
if (!array_key_exists($id, $this->instances)) { |
|
141
|
21 |
|
try { |
|
142
|
12 |
|
try { |
|
143
|
9 |
|
$this->instances[$id] = $this->build($id); |
|
144
|
|
|
} catch (NotFoundExceptionInterface $e) { |
|
145
|
|
|
if (!$this->delegates->has($id)) { |
|
146
|
|
|
throw $e; |
|
147
|
120 |
|
} |
|
148
|
|
|
|
|
149
|
18 |
|
/** @psalm-suppress MixedReturnStatement */ |
|
150
|
18 |
|
return $this->delegates->get($id); |
|
151
|
16 |
|
} |
|
152
|
|
|
} catch (Throwable $e) { |
|
153
|
2 |
|
if ($e instanceof ContainerExceptionInterface && !$e instanceof InvalidConfigException) { |
|
154
|
|
|
throw $e; |
|
155
|
|
|
} |
|
156
|
|
|
throw new BuildingException($id, $e, $this->definitions->getBuildStack(), $e); |
|
157
|
120 |
|
} |
|
158
|
10 |
|
} |
|
159
|
10 |
|
|
|
160
|
2 |
|
if ($id === StateResetter::class) { |
|
161
|
|
|
$delegatesResetter = null; |
|
162
|
|
|
if ($this->delegates->has(StateResetter::class)) { |
|
163
|
|
|
$delegatesResetter = $this->delegates->get(StateResetter::class); |
|
164
|
10 |
|
} |
|
165
|
|
|
|
|
166
|
10 |
|
/** @var StateResetter $mainResetter */ |
|
167
|
|
|
$mainResetter = $this->instances[$id]; |
|
168
|
7 |
|
|
|
169
|
7 |
|
if ($this->useResettersFromMeta) { |
|
170
|
7 |
|
/** @var StateResetter[] $resetters */ |
|
171
|
7 |
|
$resetters = []; |
|
172
|
|
|
foreach ($this->resetters as $serviceId => $callback) { |
|
173
|
|
|
if (isset($this->instances[$serviceId])) { |
|
174
|
7 |
|
$resetters[$serviceId] = $callback; |
|
175
|
1 |
|
} |
|
176
|
|
|
} |
|
177
|
7 |
|
if ($delegatesResetter !== null) { |
|
178
|
5 |
|
$resetters[] = $delegatesResetter; |
|
179
|
1 |
|
} |
|
180
|
1 |
|
$mainResetter->setResetters($resetters); |
|
181
|
|
|
} elseif ($delegatesResetter !== null) { |
|
182
|
1 |
|
$resetter = new StateResetter($this->get(ContainerInterface::class)); |
|
183
|
|
|
$resetter->setResetters([$mainResetter, $delegatesResetter]); |
|
184
|
|
|
|
|
185
|
|
|
return $resetter; |
|
186
|
|
|
} |
|
187
|
120 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** @psalm-suppress MixedReturnStatement */ |
|
190
|
|
|
return $this->instances[$id]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Sets a definition to the container. Definition may be defined multiple ways. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $id ID to set definition for. |
|
197
|
|
|
* @param mixed $definition Definition to set. |
|
198
|
|
|
* |
|
199
|
|
|
* @throws InvalidConfigException |
|
200
|
111 |
|
* |
|
201
|
|
|
* @see DefinitionNormalizer::normalize() |
|
202
|
|
|
*/ |
|
203
|
111 |
|
private function addDefinition(string $id, mixed $definition): void |
|
204
|
111 |
|
{ |
|
205
|
111 |
|
/** @var mixed $definition */ |
|
206
|
108 |
|
[$definition, $meta] = DefinitionParser::parse($definition); |
|
207
|
|
|
if ($this->validate) { |
|
208
|
|
|
$this->validateDefinition($definition, $id); |
|
209
|
|
|
$this->validateMeta($meta); |
|
210
|
|
|
} |
|
211
|
|
|
/** |
|
212
|
102 |
|
* @psalm-var array{reset?:Closure,tags?:string[]} $meta |
|
213
|
9 |
|
*/ |
|
214
|
|
|
|
|
215
|
102 |
|
if (isset($meta[self::META_TAGS])) { |
|
216
|
7 |
|
$this->setDefinitionTags($id, $meta[self::META_TAGS]); |
|
217
|
|
|
} |
|
218
|
|
|
if (isset($meta[self::META_RESET])) { |
|
219
|
102 |
|
$this->setDefinitionResetter($id, $meta[self::META_RESET]); |
|
220
|
102 |
|
} |
|
221
|
|
|
if (isset($meta[self::META_LAZY]) && $meta[self::META_LAZY] === true) { |
|
222
|
|
|
$definition = $this->decorateLazy($id, $definition); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
unset($this->instances[$id]); |
|
226
|
|
|
$this->addDefinitionToStorage($id, $definition); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
136 |
|
* Sets multiple definitions at once. |
|
231
|
|
|
* |
|
232
|
|
|
* @param array $config Definitions indexed by their IDs. |
|
233
|
136 |
|
* |
|
234
|
110 |
|
* @throws InvalidConfigException |
|
235
|
1 |
|
*/ |
|
236
|
1 |
|
private function addDefinitions(array $config): void |
|
237
|
1 |
|
{ |
|
238
|
1 |
|
/** @var mixed $definition */ |
|
239
|
1 |
|
foreach ($config as $id => $definition) { |
|
240
|
1 |
|
if ($this->validate && !is_string($id)) { |
|
241
|
|
|
throw new InvalidConfigException( |
|
242
|
|
|
sprintf( |
|
243
|
|
|
'Key must be a string. %s given.', |
|
244
|
109 |
|
get_debug_type($id) |
|
245
|
|
|
) |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
/** @var string $id */ |
|
249
|
|
|
|
|
250
|
|
|
$this->addDefinition($id, $definition); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Set container delegates. |
|
256
|
|
|
* |
|
257
|
|
|
* Each delegate must is a callable in format "function (ContainerInterface $container): ContainerInterface". |
|
258
|
120 |
|
* The container instance returned is used in case a service can not be found in primary container. |
|
259
|
|
|
* |
|
260
|
120 |
|
* @throws InvalidConfigException |
|
261
|
120 |
|
*/ |
|
262
|
|
|
private function setDelegates(array $delegates): void |
|
263
|
120 |
|
{ |
|
264
|
6 |
|
$this->delegates = new CompositeContainer(); |
|
265
|
1 |
|
$container = $this->get(ContainerInterface::class); |
|
266
|
1 |
|
|
|
267
|
1 |
|
foreach ($delegates as $delegate) { |
|
268
|
|
|
if (!$delegate instanceof Closure) { |
|
269
|
|
|
throw new InvalidConfigException( |
|
270
|
|
|
'Delegate must be callable in format "function (ContainerInterface $container): ContainerInterface".' |
|
271
|
5 |
|
); |
|
272
|
|
|
} |
|
273
|
5 |
|
|
|
274
|
1 |
|
/** @var ContainerInterface */ |
|
275
|
1 |
|
$delegate = $delegate($container); |
|
276
|
1 |
|
|
|
277
|
|
|
if (!$delegate instanceof ContainerInterface) { |
|
278
|
|
|
throw new InvalidConfigException( |
|
279
|
4 |
|
'Delegate callable must return an object that implements ContainerInterface.' |
|
280
|
|
|
); |
|
281
|
118 |
|
} |
|
282
|
|
|
|
|
283
|
|
|
$this->delegates->attach($delegate); |
|
284
|
|
|
} |
|
285
|
|
|
$this->definitions->setDelegateContainer($this->delegates); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param mixed $definition Definition to validate. |
|
290
|
111 |
|
* @param string|null $id ID of the definition to validate. |
|
291
|
|
|
* |
|
292
|
111 |
|
* @throws InvalidConfigException |
|
293
|
|
|
*/ |
|
294
|
47 |
|
private function validateDefinition(mixed $definition, ?string $id = null): void |
|
295
|
|
|
{ |
|
296
|
|
|
if (is_array($definition) && isset($definition[DefinitionParser::IS_PREPARED_ARRAY_DEFINITION_DATA])) { |
|
297
|
47 |
|
/** @var mixed $class */ |
|
298
|
|
|
$class = $definition['class']; |
|
299
|
|
|
|
|
300
|
|
|
/** @var mixed $constructorArguments */ |
|
301
|
|
|
$constructorArguments = $definition['__construct()']; |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
47 |
|
* @var array $methodsAndProperties Is always array for prepared array definition data. |
|
305
|
|
|
* |
|
306
|
47 |
|
* @see DefinitionParser::parse() |
|
307
|
47 |
|
*/ |
|
308
|
47 |
|
$methodsAndProperties = $definition['methodsAndProperties']; |
|
309
|
|
|
|
|
310
|
47 |
|
$definition = array_merge( |
|
311
|
47 |
|
$class === null ? [] : [ArrayDefinition::CLASS_NAME => $class], |
|
312
|
|
|
[ArrayDefinition::CONSTRUCTOR => $constructorArguments], |
|
313
|
|
|
// extract only value from parsed definition method |
|
314
|
111 |
|
array_map(fn (array $data): mixed => $data[2], $methodsAndProperties), |
|
315
|
1 |
|
); |
|
316
|
1 |
|
} |
|
317
|
1 |
|
|
|
318
|
|
|
if ($definition instanceof ExtensibleService) { |
|
319
|
|
|
throw new InvalidConfigException( |
|
320
|
110 |
|
'Invalid definition. ExtensibleService is only allowed in provider extensions.' |
|
321
|
|
|
); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
DefinitionValidator::validate($definition, $id); |
|
325
|
|
|
} |
|
326
|
108 |
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @throws InvalidConfigException |
|
329
|
108 |
|
*/ |
|
330
|
22 |
|
private function validateMeta(array $meta): void |
|
331
|
3 |
|
{ |
|
332
|
3 |
|
/** @var mixed $value */ |
|
333
|
3 |
|
foreach ($meta as $key => $value) { |
|
334
|
3 |
|
if (!in_array($key, self::ALLOWED_META, true)) { |
|
335
|
3 |
|
throw new InvalidConfigException( |
|
336
|
3 |
|
sprintf( |
|
337
|
3 |
|
'Invalid definition: metadata "%s" is not allowed. Did you mean "%s()" or "$%s"?', |
|
338
|
3 |
|
$key, |
|
339
|
|
|
$key, |
|
340
|
|
|
$key, |
|
341
|
20 |
|
) |
|
342
|
12 |
|
); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
18 |
|
if ($key === self::META_TAGS) { |
|
346
|
8 |
|
$this->validateDefinitionTags($value); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
if ($key === self::META_RESET) { |
|
350
|
|
|
$this->validateDefinitionReset($value); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
12 |
|
|
|
355
|
|
|
/** |
|
356
|
12 |
|
* @throws InvalidConfigException |
|
357
|
1 |
|
*/ |
|
358
|
1 |
|
private function validateDefinitionTags(mixed $tags): void |
|
359
|
1 |
|
{ |
|
360
|
1 |
|
if (!is_array($tags)) { |
|
361
|
1 |
|
throw new InvalidConfigException( |
|
362
|
1 |
|
sprintf( |
|
363
|
|
|
'Invalid definition: tags should be array of strings, %s given.', |
|
364
|
|
|
get_debug_type($tags) |
|
365
|
11 |
|
) |
|
366
|
11 |
|
); |
|
367
|
1 |
|
} |
|
368
|
|
|
|
|
369
|
|
|
foreach ($tags as $tag) { |
|
370
|
|
|
if (!is_string($tag)) { |
|
371
|
|
|
throw new InvalidConfigException('Invalid tag. Expected a string, got ' . var_export($tag, true) . '.'); |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
8 |
|
|
|
376
|
|
|
/** |
|
377
|
8 |
|
* @throws InvalidConfigException |
|
378
|
1 |
|
*/ |
|
379
|
1 |
|
private function validateDefinitionReset(mixed $reset): void |
|
380
|
1 |
|
{ |
|
381
|
1 |
|
if (!$reset instanceof Closure) { |
|
382
|
1 |
|
throw new InvalidConfigException( |
|
383
|
1 |
|
sprintf( |
|
384
|
|
|
'Invalid definition: "reset" should be closure, %s given.', |
|
385
|
|
|
get_debug_type($reset) |
|
386
|
|
|
) |
|
387
|
|
|
); |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
139 |
|
|
|
391
|
|
|
/** |
|
392
|
139 |
|
* @throws InvalidConfigException |
|
393
|
139 |
|
*/ |
|
394
|
5 |
|
private function setTags(array $tags): void |
|
395
|
1 |
|
{ |
|
396
|
1 |
|
if ($this->validate) { |
|
397
|
1 |
|
foreach ($tags as $tag => $services) { |
|
398
|
1 |
|
if (!is_string($tag)) { |
|
399
|
1 |
|
throw new InvalidConfigException( |
|
400
|
1 |
|
sprintf( |
|
401
|
|
|
'Invalid tags configuration: tag should be string, %s given.', |
|
402
|
4 |
|
$tag |
|
403
|
1 |
|
) |
|
404
|
1 |
|
); |
|
405
|
1 |
|
} |
|
406
|
1 |
|
if (!is_array($services)) { |
|
407
|
1 |
|
throw new InvalidConfigException( |
|
408
|
1 |
|
sprintf( |
|
409
|
|
|
'Invalid tags configuration: tag should contain array of service IDs, %s given.', |
|
410
|
|
|
get_debug_type($services) |
|
411
|
3 |
|
) |
|
412
|
3 |
|
); |
|
413
|
1 |
|
} |
|
414
|
1 |
|
/** @var mixed $service */ |
|
415
|
1 |
|
foreach ($services as $service) { |
|
416
|
1 |
|
if (!is_string($service)) { |
|
417
|
1 |
|
throw new InvalidConfigException( |
|
418
|
1 |
|
sprintf( |
|
419
|
|
|
'Invalid tags configuration: service should be defined as class string, %s given.', |
|
420
|
|
|
get_debug_type($service) |
|
421
|
|
|
) |
|
422
|
|
|
); |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
136 |
|
} |
|
426
|
|
|
} |
|
427
|
|
|
/** @psalm-var array<string, list<string>> $tags */ |
|
428
|
|
|
|
|
429
|
|
|
$this->tags = $tags; |
|
430
|
|
|
} |
|
431
|
9 |
|
|
|
432
|
|
|
/** |
|
433
|
9 |
|
* @psalm-param string[] $tags |
|
434
|
9 |
|
*/ |
|
435
|
9 |
|
private function setDefinitionTags(string $id, array $tags): void |
|
436
|
|
|
{ |
|
437
|
|
|
foreach ($tags as $tag) { |
|
438
|
|
|
if (!isset($this->tags[$tag]) || !in_array($id, $this->tags[$tag], true)) { |
|
439
|
|
|
$this->tags[$tag][] = $id; |
|
440
|
7 |
|
} |
|
441
|
|
|
} |
|
442
|
7 |
|
} |
|
443
|
|
|
|
|
444
|
|
|
private function setDefinitionResetter(string $id, Closure $resetter): void |
|
445
|
|
|
{ |
|
446
|
|
|
$this->resetters[$id] = $resetter; |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* Add definition to storage. |
|
451
|
|
|
* |
|
452
|
|
|
* @param string $id ID to set definition for. |
|
453
|
102 |
|
* @param mixed|object $definition Definition to set. |
|
454
|
|
|
* |
|
455
|
102 |
|
* @see $definitions |
|
456
|
|
|
*/ |
|
457
|
102 |
|
private function addDefinitionToStorage(string $id, $definition): void |
|
458
|
5 |
|
{ |
|
459
|
|
|
$this->definitions->set($id, $definition); |
|
460
|
|
|
|
|
461
|
|
|
if ($id === StateResetter::class) { |
|
462
|
|
|
$this->useResettersFromMeta = false; |
|
463
|
|
|
} |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* Creates new instance by either interface name or alias. |
|
468
|
|
|
* |
|
469
|
|
|
* @param string $id The interface or an alias name that was previously registered. |
|
470
|
|
|
* |
|
471
|
|
|
* @throws InvalidConfigException |
|
472
|
|
|
* @throws NotFoundExceptionInterface |
|
473
|
|
|
* @throws CircularReferenceException |
|
474
|
|
|
* |
|
475
|
120 |
|
* @return mixed|object New built instance of the specified class. |
|
476
|
|
|
* |
|
477
|
120 |
|
* @internal |
|
478
|
10 |
|
*/ |
|
479
|
|
|
private function build(string $id) |
|
480
|
|
|
{ |
|
481
|
120 |
|
if (TagHelper::isTagAlias($id)) { |
|
482
|
120 |
|
return $this->getTaggedServices($id); |
|
483
|
120 |
|
} |
|
484
|
|
|
|
|
485
|
4 |
|
if (isset($this->building[$id])) { |
|
486
|
4 |
|
if ($id === ContainerInterface::class) { |
|
487
|
4 |
|
return $this; |
|
488
|
4 |
|
} |
|
489
|
4 |
|
throw new CircularReferenceException( |
|
490
|
4 |
|
sprintf( |
|
491
|
4 |
|
'Circular reference to "%s" detected while building: %s.', |
|
492
|
|
|
$id, |
|
493
|
|
|
implode(', ', array_keys($this->building)) |
|
494
|
120 |
|
) |
|
495
|
|
|
); |
|
496
|
|
|
} |
|
497
|
120 |
|
|
|
498
|
|
|
$this->building[$id] = 1; |
|
499
|
120 |
|
try { |
|
500
|
|
|
/** @var mixed $object */ |
|
501
|
|
|
$object = $this->buildInternal($id); |
|
502
|
120 |
|
} finally { |
|
503
|
|
|
unset($this->building[$id]); |
|
504
|
|
|
} |
|
505
|
10 |
|
|
|
506
|
|
|
return $object; |
|
507
|
10 |
|
} |
|
508
|
10 |
|
|
|
509
|
10 |
|
private function getTaggedServices(string $tagAlias): array |
|
510
|
9 |
|
{ |
|
511
|
|
|
$tag = TagHelper::extractTagFromAlias($tagAlias); |
|
512
|
9 |
|
$services = []; |
|
513
|
|
|
if (isset($this->tags[$tag])) { |
|
514
|
|
|
foreach ($this->tags[$tag] as $service) { |
|
515
|
|
|
/** @var mixed */ |
|
516
|
10 |
|
$services[] = $this->get($service); |
|
517
|
|
|
} |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
return $services; |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* @throws NotFoundExceptionInterface |
|
525
|
120 |
|
* @throws InvalidConfigException |
|
526
|
|
|
* |
|
527
|
120 |
|
* @return mixed|object |
|
528
|
120 |
|
*/ |
|
529
|
|
|
private function buildInternal(string $id) |
|
530
|
120 |
|
{ |
|
531
|
|
|
if ($this->definitions->has($id)) { |
|
532
|
|
|
$definition = DefinitionNormalizer::normalize($this->definitions->get($id), $id); |
|
533
|
12 |
|
|
|
534
|
|
|
return $definition->resolve($this->get(ContainerInterface::class)); |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
throw new NotFoundException($id, $this->definitions->getBuildStack()); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
126 |
|
/** |
|
541
|
|
|
* @throws CircularReferenceException |
|
542
|
126 |
|
* @throws InvalidConfigException |
|
543
|
|
|
*/ |
|
544
|
126 |
|
private function addProviders(array $providers): void |
|
545
|
16 |
|
{ |
|
546
|
14 |
|
$extensions = []; |
|
547
|
14 |
|
/** @var mixed $provider */ |
|
548
|
|
|
foreach ($providers as $provider) { |
|
549
|
|
|
$providerInstance = $this->buildProvider($provider); |
|
550
|
124 |
|
$extensions[] = $providerInstance->getExtensions(); |
|
551
|
|
|
$this->addDefinitions($providerInstance->getDefinitions()); |
|
552
|
14 |
|
} |
|
553
|
10 |
|
|
|
554
|
1 |
|
foreach ($extensions as $providerExtensions) { |
|
555
|
1 |
|
/** @var mixed $extension */ |
|
556
|
1 |
|
foreach ($providerExtensions as $id => $extension) { |
|
557
|
|
|
if (!is_string($id)) { |
|
558
|
|
|
throw new InvalidConfigException( |
|
559
|
9 |
|
sprintf('Extension key must be a service ID as string, %s given.', $id) |
|
560
|
1 |
|
); |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
8 |
|
if ($id === ContainerInterface::class) { |
|
564
|
1 |
|
throw new InvalidConfigException('ContainerInterface extensions are not allowed.'); |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
8 |
|
if (!$this->definitions->has($id)) { |
|
568
|
1 |
|
throw new InvalidConfigException("Extended service \"$id\" doesn't exist."); |
|
569
|
1 |
|
} |
|
570
|
1 |
|
|
|
571
|
1 |
|
if (!is_callable($extension)) { |
|
572
|
1 |
|
throw new InvalidConfigException( |
|
573
|
1 |
|
sprintf( |
|
574
|
|
|
'Extension of service should be callable, %s given.', |
|
575
|
|
|
get_debug_type($extension) |
|
576
|
|
|
) |
|
577
|
7 |
|
); |
|
578
|
7 |
|
} |
|
579
|
7 |
|
|
|
580
|
7 |
|
/** @var mixed $definition */ |
|
581
|
|
|
$definition = $this->definitions->get($id); |
|
582
|
|
|
if (!$definition instanceof ExtensibleService) { |
|
583
|
7 |
|
$definition = new ExtensibleService($definition, $id); |
|
584
|
|
|
$this->addDefinitionToStorage($id, $definition); |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
$definition->addExtension($extension); |
|
588
|
|
|
} |
|
589
|
|
|
} |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
/** |
|
593
|
|
|
* Builds service provider by definition. |
|
594
|
|
|
* |
|
595
|
|
|
* @param mixed $provider Class name or instance of provider. |
|
596
|
|
|
* |
|
597
|
16 |
|
* @throws InvalidConfigException If provider argument is not valid. |
|
598
|
|
|
* |
|
599
|
16 |
|
* @return ServiceProviderInterface Instance of service provider. |
|
600
|
1 |
|
*/ |
|
601
|
1 |
|
private function buildProvider(mixed $provider): ServiceProviderInterface |
|
602
|
1 |
|
{ |
|
603
|
1 |
|
if ($this->validate && !(is_string($provider) || $provider instanceof ServiceProviderInterface)) { |
|
604
|
1 |
|
throw new InvalidConfigException( |
|
605
|
1 |
|
sprintf( |
|
606
|
1 |
|
'Service provider should be a class name or an instance of %s. %s given.', |
|
607
|
|
|
ServiceProviderInterface::class, |
|
608
|
|
|
get_debug_type($provider) |
|
609
|
|
|
) |
|
610
|
|
|
); |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
15 |
|
/** |
|
614
|
15 |
|
* @psalm-suppress MixedMethodCall Service provider defined as class string |
|
615
|
1 |
|
* should container public constructor, otherwise throws error. |
|
616
|
1 |
|
*/ |
|
617
|
1 |
|
$providerInstance = is_object($provider) ? $provider : new $provider(); |
|
618
|
1 |
|
if (!$providerInstance instanceof ServiceProviderInterface) { |
|
619
|
1 |
|
throw new InvalidConfigException( |
|
620
|
1 |
|
sprintf( |
|
621
|
1 |
|
'Service provider should be an instance of %s. %s given.', |
|
622
|
|
|
ServiceProviderInterface::class, |
|
623
|
|
|
get_debug_type($providerInstance) |
|
624
|
14 |
|
) |
|
625
|
|
|
); |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
return $providerInstance; |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
private function decorateLazy(string $id, mixed $definition): DefinitionInterface |
|
632
|
|
|
{ |
|
633
|
|
|
if (class_exists($id) || interface_exists($id)) { |
|
634
|
|
|
$class = $id; |
|
635
|
|
|
} elseif (is_array($definition) && array_key_exists(ArrayDefinition::CLASS_NAME, $definition)) { |
|
636
|
|
|
$class = (string) $definition[ArrayDefinition::CLASS_NAME]; |
|
637
|
|
|
} else { |
|
638
|
|
|
throw new InvalidConfigException( |
|
639
|
|
|
sprintf( |
|
640
|
|
|
'Invalid definition: lazy services are only available with array definitions or references. Got type "%s" for definition ID: "%s"', |
|
641
|
|
|
get_debug_type($definition), |
|
642
|
|
|
$id, |
|
643
|
|
|
) |
|
644
|
|
|
); |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
|
|
return new LazyDefinition($definition, $class); |
|
648
|
|
|
} |
|
649
|
|
|
} |
|
650
|
|
|
|