|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Symfony\Component\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Composer\Autoload\ClassLoader; |
|
15
|
|
|
use Composer\InstalledVersions; |
|
16
|
|
|
use Symfony\Component\Config\Resource\ClassExistenceResource; |
|
17
|
|
|
use Symfony\Component\Config\Resource\ComposerResource; |
|
18
|
|
|
use Symfony\Component\Config\Resource\DirectoryResource; |
|
19
|
|
|
use Symfony\Component\Config\Resource\FileExistenceResource; |
|
20
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
|
21
|
|
|
use Symfony\Component\Config\Resource\GlobResource; |
|
22
|
|
|
use Symfony\Component\Config\Resource\ReflectionClassResource; |
|
23
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\Argument\AbstractArgument; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
|
26
|
|
|
use Symfony\Component\DependencyInjection\Argument\LazyClosure; |
|
27
|
|
|
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; |
|
28
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
|
29
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceLocator; |
|
|
|
|
|
|
30
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; |
|
31
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Target; |
|
32
|
|
|
use Symfony\Component\DependencyInjection\Compiler\Compiler; |
|
33
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
34
|
|
|
use Symfony\Component\DependencyInjection\Compiler\PassConfig; |
|
35
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass; |
|
36
|
|
|
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException; |
|
37
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
38
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException; |
|
39
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
|
40
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
|
41
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
|
42
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
43
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
44
|
|
|
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface; |
|
45
|
|
|
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\LazyServiceInstantiator; |
|
46
|
|
|
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator; |
|
47
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; |
|
48
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
|
49
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
50
|
|
|
use Symfony\Component\ErrorHandler\DebugClassLoader; |
|
51
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
52
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* ContainerBuilder is a DI container that provides an API to easily describe services. |
|
56
|
|
|
* |
|
57
|
|
|
* @author Fabien Potencier <[email protected]> |
|
58
|
|
|
*/ |
|
59
|
|
|
class ContainerBuilder extends Container implements TaggedContainerInterface |
|
60
|
|
|
{ |
|
61
|
|
|
/** |
|
62
|
|
|
* @var array<string, ExtensionInterface> |
|
63
|
|
|
*/ |
|
64
|
|
|
private array $extensions = []; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array<string, ExtensionInterface> |
|
68
|
|
|
*/ |
|
69
|
|
|
private array $extensionsByNs = []; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @var array<string, Definition> |
|
73
|
|
|
*/ |
|
74
|
|
|
private array $definitions = []; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var array<string, Alias> |
|
78
|
|
|
*/ |
|
79
|
|
|
private array $aliasDefinitions = []; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var array<string, ResourceInterface> |
|
83
|
|
|
*/ |
|
84
|
|
|
private array $resources = []; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @var array<string, array<array<string, mixed>>> |
|
88
|
|
|
*/ |
|
89
|
|
|
private array $extensionConfigs = []; |
|
90
|
|
|
|
|
91
|
|
|
private Compiler $compiler; |
|
92
|
|
|
private bool $trackResources; |
|
93
|
|
|
private InstantiatorInterface $proxyInstantiator; |
|
94
|
|
|
private ExpressionLanguage $expressionLanguage; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @var ExpressionFunctionProviderInterface[] |
|
98
|
|
|
*/ |
|
99
|
|
|
private array $expressionLanguageProviders = []; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var string[] with tag names used by findTaggedServiceIds |
|
103
|
|
|
*/ |
|
104
|
|
|
private array $usedTags = []; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @var string[][] a map of env var names to their placeholders |
|
108
|
|
|
*/ |
|
109
|
|
|
private array $envPlaceholders = []; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @var int[] a map of env vars to their resolution counter |
|
113
|
|
|
*/ |
|
114
|
|
|
private array $envCounters = []; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @var string[] the list of vendor directories |
|
118
|
|
|
*/ |
|
119
|
|
|
private array $vendors; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @var array<string, bool> whether a path is in a vendor directory |
|
123
|
|
|
*/ |
|
124
|
|
|
private array $pathsInVendor = []; |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @var array<string, ChildDefinition> |
|
128
|
|
|
*/ |
|
129
|
|
|
private array $autoconfiguredInstanceof = []; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @var array<string, callable> |
|
133
|
|
|
*/ |
|
134
|
|
|
private array $autoconfiguredAttributes = []; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @var array<string, bool> |
|
138
|
|
|
*/ |
|
139
|
|
|
private array $removedIds = []; |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @var array<int, bool> |
|
143
|
|
|
*/ |
|
144
|
|
|
private array $removedBindingIds = []; |
|
145
|
|
|
|
|
146
|
|
|
private const INTERNAL_TYPES = [ |
|
147
|
|
|
'int' => true, |
|
148
|
|
|
'float' => true, |
|
149
|
|
|
'string' => true, |
|
150
|
|
|
'bool' => true, |
|
151
|
|
|
'resource' => true, |
|
152
|
|
|
'object' => true, |
|
153
|
|
|
'array' => true, |
|
154
|
|
|
'null' => true, |
|
155
|
|
|
'callable' => true, |
|
156
|
|
|
'iterable' => true, |
|
157
|
|
|
'mixed' => true, |
|
158
|
|
|
]; |
|
159
|
|
|
|
|
160
|
|
|
public function __construct(?ParameterBagInterface $parameterBag = null) |
|
161
|
|
|
{ |
|
162
|
|
|
parent::__construct($parameterBag); |
|
163
|
|
|
|
|
164
|
|
|
$this->trackResources = interface_exists(ResourceInterface::class); |
|
165
|
|
|
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true)->setPublic(true)); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @var array<string, \ReflectionClass> |
|
170
|
|
|
*/ |
|
171
|
|
|
private array $classReflectors; |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Sets the track resources flag. |
|
175
|
|
|
* |
|
176
|
|
|
* If you are not using the loaders and therefore don't want |
|
177
|
|
|
* to depend on the Config component, set this flag to false. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setResourceTracking(bool $track): void |
|
180
|
|
|
{ |
|
181
|
|
|
$this->trackResources = $track; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Checks if resources are tracked. |
|
186
|
|
|
*/ |
|
187
|
|
|
public function isTrackingResources(): bool |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->trackResources; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Sets the instantiator to be used when fetching proxies. |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setProxyInstantiator(InstantiatorInterface $proxyInstantiator): void |
|
196
|
|
|
{ |
|
197
|
|
|
$this->proxyInstantiator = $proxyInstantiator; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function registerExtension(ExtensionInterface $extension): void |
|
201
|
|
|
{ |
|
202
|
|
|
$this->extensions[$extension->getAlias()] = $extension; |
|
203
|
|
|
|
|
204
|
|
|
if (false !== $extension->getNamespace()) { |
|
|
|
|
|
|
205
|
|
|
$this->extensionsByNs[$extension->getNamespace()] = $extension; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Returns an extension by alias or namespace. |
|
211
|
|
|
* |
|
212
|
|
|
* @throws LogicException if the extension is not registered |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getExtension(string $name): ExtensionInterface |
|
215
|
|
|
{ |
|
216
|
|
|
if (isset($this->extensions[$name])) { |
|
217
|
|
|
return $this->extensions[$name]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (isset($this->extensionsByNs[$name])) { |
|
221
|
|
|
return $this->extensionsByNs[$name]; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
throw new LogicException(\sprintf('Container extension "%s" is not registered.', $name)); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Returns all registered extensions. |
|
229
|
|
|
* |
|
230
|
|
|
* @return array<string, ExtensionInterface> |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getExtensions(): array |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->extensions; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Checks if we have an extension. |
|
239
|
|
|
*/ |
|
240
|
|
|
public function hasExtension(string $name): bool |
|
241
|
|
|
{ |
|
242
|
|
|
return isset($this->extensions[$name]) || isset($this->extensionsByNs[$name]); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Returns an array of resources loaded to build this configuration. |
|
247
|
|
|
* |
|
248
|
|
|
* @return ResourceInterface[] |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getResources(): array |
|
251
|
|
|
{ |
|
252
|
|
|
return array_values($this->resources); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @return $this |
|
257
|
|
|
*/ |
|
258
|
|
|
public function addResource(ResourceInterface $resource): static |
|
259
|
|
|
{ |
|
260
|
|
|
if (!$this->trackResources) { |
|
261
|
|
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
if ($resource instanceof GlobResource && $this->inVendors($resource->getPrefix())) { |
|
265
|
|
|
return $this; |
|
266
|
|
|
} |
|
267
|
|
|
if ($resource instanceof FileExistenceResource && $this->inVendors($resource->getResource())) { |
|
268
|
|
|
return $this; |
|
269
|
|
|
} |
|
270
|
|
|
if ($resource instanceof FileResource && $this->inVendors($resource->getResource())) { |
|
271
|
|
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
if ($resource instanceof DirectoryResource && $this->inVendors($resource->getResource())) { |
|
274
|
|
|
return $this; |
|
275
|
|
|
} |
|
276
|
|
|
if ($resource instanceof ClassExistenceResource) { |
|
277
|
|
|
$class = $resource->getResource(); |
|
278
|
|
|
|
|
279
|
|
|
$inVendor = false; |
|
280
|
|
|
foreach (spl_autoload_functions() as $autoloader) { |
|
281
|
|
|
if (!\is_array($autoloader)) { |
|
282
|
|
|
continue; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
if ($autoloader[0] instanceof DebugClassLoader) { |
|
286
|
|
|
$autoloader = $autoloader[0]->getClassLoader(); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
if (!\is_array($autoloader) || !$autoloader[0] instanceof ClassLoader || !$autoloader[0]->findFile(__CLASS__)) { |
|
290
|
|
|
continue; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
foreach ($autoloader[0]->getPrefixesPsr4() as $prefix => $dirs) { |
|
294
|
|
|
if ('' === $prefix || !str_starts_with($class, $prefix)) { |
|
295
|
|
|
continue; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
foreach ($dirs as $dir) { |
|
299
|
|
|
if (!$dir = realpath($dir)) { |
|
300
|
|
|
continue; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
if (!$inVendor = $this->inVendors($dir)) { |
|
304
|
|
|
break 3; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
if ($inVendor) { |
|
311
|
|
|
return $this; |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$this->resources[(string) $resource] = $resource; |
|
316
|
|
|
|
|
317
|
|
|
return $this; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Sets the resources for this configuration. |
|
322
|
|
|
* |
|
323
|
|
|
* @param array<string, ResourceInterface> $resources |
|
324
|
|
|
* |
|
325
|
|
|
* @return $this |
|
326
|
|
|
*/ |
|
327
|
|
|
public function setResources(array $resources): static |
|
328
|
|
|
{ |
|
329
|
|
|
if (!$this->trackResources) { |
|
330
|
|
|
return $this; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
$this->resources = $resources; |
|
334
|
|
|
|
|
335
|
|
|
return $this; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Adds the object class hierarchy as resources. |
|
340
|
|
|
* |
|
341
|
|
|
* @param object|string $object An object instance or class name |
|
342
|
|
|
* |
|
343
|
|
|
* @return $this |
|
344
|
|
|
*/ |
|
345
|
|
|
public function addObjectResource(object|string $object): static |
|
346
|
|
|
{ |
|
347
|
|
|
if ($this->trackResources) { |
|
348
|
|
|
if (\is_object($object)) { |
|
349
|
|
|
$object = $object::class; |
|
350
|
|
|
} |
|
351
|
|
|
if (!isset($this->classReflectors[$object])) { |
|
352
|
|
|
$this->classReflectors[$object] = new \ReflectionClass($object); |
|
353
|
|
|
} |
|
354
|
|
|
$class = $this->classReflectors[$object]; |
|
355
|
|
|
|
|
356
|
|
|
foreach ($class->getInterfaceNames() as $name) { |
|
357
|
|
|
if (null === $interface = &$this->classReflectors[$name]) { |
|
358
|
|
|
$interface = new \ReflectionClass($name); |
|
359
|
|
|
} |
|
360
|
|
|
$file = $interface->getFileName(); |
|
361
|
|
|
if (false !== $file && file_exists($file)) { |
|
362
|
|
|
$this->fileExists($file); |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
do { |
|
366
|
|
|
$file = $class->getFileName(); |
|
367
|
|
|
if (false !== $file && file_exists($file)) { |
|
368
|
|
|
$this->fileExists($file); |
|
369
|
|
|
} |
|
370
|
|
|
foreach ($class->getTraitNames() as $name) { |
|
371
|
|
|
$this->addObjectResource($name); |
|
372
|
|
|
} |
|
373
|
|
|
} while ($class = $class->getParentClass()); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
return $this; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Retrieves the requested reflection class and registers it for resource tracking. |
|
381
|
|
|
* |
|
382
|
|
|
* @throws \ReflectionException when a parent class/interface/trait is not found and $throw is true |
|
383
|
|
|
* |
|
384
|
|
|
* @final |
|
385
|
|
|
*/ |
|
386
|
|
|
public function getReflectionClass(?string $class, bool $throw = true): ?\ReflectionClass |
|
387
|
|
|
{ |
|
388
|
|
|
if (!$class = $this->getParameterBag()->resolveValue($class)) { |
|
389
|
|
|
return null; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
if (isset(self::INTERNAL_TYPES[$class])) { |
|
393
|
|
|
return null; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
$resource = $classReflector = null; |
|
397
|
|
|
|
|
398
|
|
|
try { |
|
399
|
|
|
if (isset($this->classReflectors[$class])) { |
|
400
|
|
|
$classReflector = $this->classReflectors[$class]; |
|
401
|
|
|
} elseif (class_exists(ClassExistenceResource::class)) { |
|
402
|
|
|
$resource = new ClassExistenceResource($class, false); |
|
403
|
|
|
$classReflector = $resource->isFresh(0) ? false : new \ReflectionClass($class); |
|
404
|
|
|
} else { |
|
405
|
|
|
$classReflector = class_exists($class) || interface_exists($class, false) ? new \ReflectionClass($class) : false; |
|
406
|
|
|
} |
|
407
|
|
|
} catch (\ReflectionException $e) { |
|
408
|
|
|
if ($throw) { |
|
409
|
|
|
throw $e; |
|
410
|
|
|
} |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
if ($this->trackResources) { |
|
414
|
|
|
if (!$classReflector) { |
|
415
|
|
|
$this->addResource($resource ?? new ClassExistenceResource($class, false)); |
|
416
|
|
|
} elseif (!$classReflector->isInternal()) { |
|
417
|
|
|
$path = $classReflector->getFileName(); |
|
418
|
|
|
|
|
419
|
|
|
if (!$this->inVendors($path)) { |
|
420
|
|
|
$this->addResource(new ReflectionClassResource($classReflector, $this->vendors)); |
|
421
|
|
|
} |
|
422
|
|
|
} |
|
423
|
|
|
$this->classReflectors[$class] = $classReflector; |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
return $classReflector ?: null; |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* Checks whether the requested file or directory exists and registers the result for resource tracking. |
|
431
|
|
|
* |
|
432
|
|
|
* @param string $path The file or directory path for which to check the existence |
|
433
|
|
|
* @param bool|string $trackContents Whether to track contents of the given resource. If a string is passed, |
|
434
|
|
|
* it will be used as pattern for tracking contents of the requested directory |
|
435
|
|
|
* |
|
436
|
|
|
* @final |
|
437
|
|
|
*/ |
|
438
|
|
|
public function fileExists(string $path, bool|string $trackContents = true): bool |
|
439
|
|
|
{ |
|
440
|
|
|
$exists = file_exists($path); |
|
441
|
|
|
|
|
442
|
|
|
if (!$this->trackResources || $this->inVendors($path)) { |
|
443
|
|
|
return $exists; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
if (!$exists) { |
|
447
|
|
|
$this->addResource(new FileExistenceResource($path)); |
|
448
|
|
|
|
|
449
|
|
|
return false; |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
if (is_dir($path)) { |
|
453
|
|
|
if ($trackContents) { |
|
454
|
|
|
$this->addResource(new DirectoryResource($path, \is_string($trackContents) ? $trackContents : null)); |
|
455
|
|
|
} else { |
|
456
|
|
|
$this->addResource(new GlobResource($path, '/*', false)); |
|
457
|
|
|
} |
|
458
|
|
|
} elseif ($trackContents) { |
|
459
|
|
|
$this->addResource(new FileResource($path)); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
return true; |
|
463
|
|
|
} |
|
464
|
|
|
|
|
465
|
|
|
/** |
|
466
|
|
|
* Loads the configuration for an extension. |
|
467
|
|
|
* |
|
468
|
|
|
* @param string $extension The extension alias or namespace |
|
469
|
|
|
* @param array<string, mixed>|null $values An array of values that customizes the extension |
|
470
|
|
|
* |
|
471
|
|
|
* @return $this |
|
472
|
|
|
* |
|
473
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is compiled |
|
474
|
|
|
* @throws \LogicException if the extension is not registered |
|
475
|
|
|
*/ |
|
476
|
|
|
public function loadFromExtension(string $extension, ?array $values = null): static |
|
477
|
|
|
{ |
|
478
|
|
|
if ($this->isCompiled()) { |
|
479
|
|
|
throw new BadMethodCallException('Cannot load from an extension on a compiled container.'); |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
$namespace = $this->getExtension($extension)->getAlias(); |
|
483
|
|
|
|
|
484
|
|
|
$this->extensionConfigs[$namespace][] = $values ?? []; |
|
485
|
|
|
|
|
486
|
|
|
return $this; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* Adds a compiler pass. |
|
491
|
|
|
* |
|
492
|
|
|
* @param string $type The type of compiler pass |
|
493
|
|
|
* @param int $priority Used to sort the passes |
|
494
|
|
|
* |
|
495
|
|
|
* @return $this |
|
496
|
|
|
*/ |
|
497
|
|
|
public function addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0): static |
|
498
|
|
|
{ |
|
499
|
|
|
$this->getCompiler()->addPass($pass, $type, $priority); |
|
500
|
|
|
|
|
501
|
|
|
$this->addObjectResource($pass); |
|
502
|
|
|
|
|
503
|
|
|
return $this; |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
/** |
|
507
|
|
|
* Returns the compiler pass config which can then be modified. |
|
508
|
|
|
*/ |
|
509
|
|
|
public function getCompilerPassConfig(): PassConfig |
|
510
|
|
|
{ |
|
511
|
|
|
return $this->getCompiler()->getPassConfig(); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Returns the compiler. |
|
516
|
|
|
*/ |
|
517
|
|
|
public function getCompiler(): Compiler |
|
518
|
|
|
{ |
|
519
|
|
|
return $this->compiler ??= new Compiler(); |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* Sets a service. |
|
524
|
|
|
* |
|
525
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is compiled |
|
526
|
|
|
*/ |
|
527
|
|
|
public function set(string $id, ?object $service): void |
|
528
|
|
|
{ |
|
529
|
|
|
if ($this->isCompiled() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) { |
|
530
|
|
|
// setting a synthetic service on a compiled container is alright |
|
531
|
|
|
throw new BadMethodCallException(\sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a compiled container is not allowed.', $id)); |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
unset($this->definitions[$id], $this->aliasDefinitions[$id], $this->removedIds[$id]); |
|
535
|
|
|
|
|
536
|
|
|
parent::set($id, $service); |
|
537
|
|
|
} |
|
538
|
|
|
|
|
539
|
|
|
/** |
|
540
|
|
|
* Removes a service definition. |
|
541
|
|
|
*/ |
|
542
|
|
|
public function removeDefinition(string $id): void |
|
543
|
|
|
{ |
|
544
|
|
|
if (isset($this->definitions[$id])) { |
|
545
|
|
|
unset($this->definitions[$id]); |
|
546
|
|
|
if ('.' !== ($id[0] ?? '-')) { |
|
547
|
|
|
$this->removedIds[$id] = true; |
|
548
|
|
|
} |
|
549
|
|
|
} |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
public function has(string $id): bool |
|
553
|
|
|
{ |
|
554
|
|
|
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* @throws InvalidArgumentException when no definitions are available |
|
559
|
|
|
* @throws ServiceCircularReferenceException When a circular reference is detected |
|
560
|
|
|
* @throws ServiceNotFoundException When the service is not defined |
|
561
|
|
|
* @throws \Exception |
|
562
|
|
|
* |
|
563
|
|
|
* @see Reference |
|
564
|
|
|
*/ |
|
565
|
|
|
public function get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE): ?object |
|
566
|
|
|
{ |
|
567
|
|
|
if ($this->isCompiled() && isset($this->removedIds[$id])) { |
|
568
|
|
|
return ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior ? parent::get($id) : null; |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
return $this->doGet($id, $invalidBehavior); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
private function doGet(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, ?array &$inlineServices = null, bool $isConstructorArgument = false): mixed |
|
575
|
|
|
{ |
|
576
|
|
|
if (isset($inlineServices[$id])) { |
|
577
|
|
|
return $inlineServices[$id]; |
|
578
|
|
|
} |
|
579
|
|
|
if (null === $inlineServices) { |
|
|
|
|
|
|
580
|
|
|
$isConstructorArgument = true; |
|
581
|
|
|
$inlineServices = []; |
|
582
|
|
|
} |
|
583
|
|
|
try { |
|
584
|
|
|
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) { |
|
585
|
|
|
return $this->privates[$id] ?? parent::get($id, $invalidBehavior); |
|
586
|
|
|
} |
|
587
|
|
|
if (null !== $service = $this->privates[$id] ?? parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) { |
|
588
|
|
|
return $service; |
|
589
|
|
|
} |
|
590
|
|
|
} catch (ServiceCircularReferenceException $e) { |
|
591
|
|
|
if ($isConstructorArgument) { |
|
|
|
|
|
|
592
|
|
|
throw $e; |
|
593
|
|
|
} |
|
594
|
|
|
} |
|
595
|
|
|
|
|
596
|
|
|
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) { |
|
597
|
|
|
$alias = $this->aliasDefinitions[$id]; |
|
598
|
|
|
|
|
599
|
|
|
if ($alias->isDeprecated()) { |
|
600
|
|
|
$deprecation = $alias->getDeprecation($id); |
|
601
|
|
|
trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']); |
|
602
|
|
|
} |
|
603
|
|
|
|
|
604
|
|
|
return $this->doGet((string) $alias, $invalidBehavior, $inlineServices, $isConstructorArgument); |
|
605
|
|
|
} |
|
606
|
|
|
|
|
607
|
|
|
try { |
|
608
|
|
|
$definition = $this->getDefinition($id); |
|
609
|
|
|
} catch (ServiceNotFoundException $e) { |
|
610
|
|
|
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE < $invalidBehavior) { |
|
611
|
|
|
return null; |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
|
|
throw $e; |
|
615
|
|
|
} |
|
616
|
|
|
|
|
617
|
|
|
if ($definition->hasErrors() && $e = $definition->getErrors()) { |
|
618
|
|
|
throw new RuntimeException(reset($e)); |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
if ($isConstructorArgument) { |
|
|
|
|
|
|
622
|
|
|
$this->loading[$id] = true; |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
try { |
|
626
|
|
|
return $this->createService($definition, $inlineServices, $isConstructorArgument, $id); |
|
627
|
|
|
} finally { |
|
628
|
|
|
if ($isConstructorArgument) { |
|
629
|
|
|
unset($this->loading[$id]); |
|
630
|
|
|
} |
|
631
|
|
|
} |
|
632
|
|
|
} |
|
633
|
|
|
|
|
634
|
|
|
/** |
|
635
|
|
|
* Merges a ContainerBuilder with the current ContainerBuilder configuration. |
|
636
|
|
|
* |
|
637
|
|
|
* Service definitions overrides the current defined ones. |
|
638
|
|
|
* |
|
639
|
|
|
* But for parameters, they are overridden by the current ones. It allows |
|
640
|
|
|
* the parameters passed to the container constructor to have precedence |
|
641
|
|
|
* over the loaded ones. |
|
642
|
|
|
* |
|
643
|
|
|
* $container = new ContainerBuilder(new ParameterBag(['foo' => 'bar'])); |
|
644
|
|
|
* $loader = new LoaderXXX($container); |
|
645
|
|
|
* $loader->load('resource_name'); |
|
646
|
|
|
* $container->register('foo', 'stdClass'); |
|
647
|
|
|
* |
|
648
|
|
|
* In the above example, even if the loaded resource defines a foo |
|
649
|
|
|
* parameter, the value will still be 'bar' as defined in the ContainerBuilder |
|
650
|
|
|
* constructor. |
|
651
|
|
|
* |
|
652
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is compiled |
|
653
|
|
|
*/ |
|
654
|
|
|
public function merge(self $container): void |
|
655
|
|
|
{ |
|
656
|
|
|
if ($this->isCompiled()) { |
|
657
|
|
|
throw new BadMethodCallException('Cannot merge on a compiled container.'); |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
foreach ($container->getDefinitions() as $id => $definition) { |
|
661
|
|
|
if (!$definition->hasTag('container.excluded') || !$this->has($id)) { |
|
662
|
|
|
$this->setDefinition($id, $definition); |
|
663
|
|
|
} |
|
664
|
|
|
} |
|
665
|
|
|
$this->addAliases($container->getAliases()); |
|
666
|
|
|
$parameterBag = $this->getParameterBag(); |
|
667
|
|
|
$otherBag = $container->getParameterBag(); |
|
668
|
|
|
$parameterBag->add($otherBag->all()); |
|
669
|
|
|
|
|
670
|
|
|
if ($parameterBag instanceof ParameterBag && $otherBag instanceof ParameterBag) { |
|
671
|
|
|
foreach ($otherBag->allDeprecated() as $name => $deprecated) { |
|
672
|
|
|
$parameterBag->deprecate($name, ...$deprecated); |
|
|
|
|
|
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
foreach ($otherBag->allNonEmpty() as $name => $message) { |
|
676
|
|
|
$parameterBag->cannotBeEmpty($name, $message); |
|
677
|
|
|
} |
|
678
|
|
|
} |
|
679
|
|
|
|
|
680
|
|
|
if ($this->trackResources) { |
|
681
|
|
|
foreach ($container->getResources() as $resource) { |
|
682
|
|
|
$this->addResource($resource); |
|
683
|
|
|
} |
|
684
|
|
|
} |
|
685
|
|
|
|
|
686
|
|
|
foreach ($this->extensions as $name => $extension) { |
|
687
|
|
|
if (!isset($this->extensionConfigs[$name])) { |
|
688
|
|
|
$this->extensionConfigs[$name] = []; |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
$this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name)); |
|
692
|
|
|
} |
|
693
|
|
|
|
|
694
|
|
|
if ($parameterBag instanceof EnvPlaceholderParameterBag && $otherBag instanceof EnvPlaceholderParameterBag) { |
|
695
|
|
|
$envPlaceholders = $otherBag->getEnvPlaceholders(); |
|
696
|
|
|
$parameterBag->mergeEnvPlaceholders($otherBag); |
|
697
|
|
|
} else { |
|
698
|
|
|
$envPlaceholders = []; |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
foreach ($container->envCounters as $env => $count) { |
|
702
|
|
|
if (!$count && !isset($envPlaceholders[$env])) { |
|
703
|
|
|
continue; |
|
704
|
|
|
} |
|
705
|
|
|
if (!isset($this->envCounters[$env])) { |
|
706
|
|
|
$this->envCounters[$env] = $count; |
|
707
|
|
|
} else { |
|
708
|
|
|
$this->envCounters[$env] += $count; |
|
709
|
|
|
} |
|
710
|
|
|
} |
|
711
|
|
|
|
|
712
|
|
|
foreach ($container->getAutoconfiguredInstanceof() as $interface => $childDefinition) { |
|
713
|
|
|
if (isset($this->autoconfiguredInstanceof[$interface])) { |
|
714
|
|
|
throw new InvalidArgumentException(\sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same class/interface.', $interface)); |
|
715
|
|
|
} |
|
716
|
|
|
|
|
717
|
|
|
$this->autoconfiguredInstanceof[$interface] = $childDefinition; |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
foreach ($container->getAutoconfiguredAttributes() as $attribute => $configurator) { |
|
721
|
|
|
if (isset($this->autoconfiguredAttributes[$attribute])) { |
|
722
|
|
|
throw new InvalidArgumentException(\sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same attribute.', $attribute)); |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
$this->autoconfiguredAttributes[$attribute] = $configurator; |
|
726
|
|
|
} |
|
727
|
|
|
} |
|
728
|
|
|
|
|
729
|
|
|
/** |
|
730
|
|
|
* Returns the configuration array for the given extension. |
|
731
|
|
|
* |
|
732
|
|
|
* @return array<array<string, mixed>> |
|
733
|
|
|
*/ |
|
734
|
|
|
public function getExtensionConfig(string $name): array |
|
735
|
|
|
{ |
|
736
|
|
|
if (!isset($this->extensionConfigs[$name])) { |
|
737
|
|
|
$this->extensionConfigs[$name] = []; |
|
738
|
|
|
} |
|
739
|
|
|
|
|
740
|
|
|
return $this->extensionConfigs[$name]; |
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
/** |
|
744
|
|
|
* Prepends a config array to the configs of the given extension. |
|
745
|
|
|
* |
|
746
|
|
|
* @param array<string, mixed> $config |
|
747
|
|
|
*/ |
|
748
|
|
|
public function prependExtensionConfig(string $name, array $config): void |
|
749
|
|
|
{ |
|
750
|
|
|
if (!isset($this->extensionConfigs[$name])) { |
|
751
|
|
|
$this->extensionConfigs[$name] = []; |
|
752
|
|
|
} |
|
753
|
|
|
|
|
754
|
|
|
array_unshift($this->extensionConfigs[$name], $config); |
|
755
|
|
|
} |
|
756
|
|
|
|
|
757
|
|
|
/** |
|
758
|
|
|
* Deprecates a service container parameter. |
|
759
|
|
|
* |
|
760
|
|
|
* @throws ParameterNotFoundException if the parameter is not defined |
|
761
|
|
|
*/ |
|
762
|
|
|
public function deprecateParameter(string $name, string $package, string $version, string $message = 'The parameter "%s" is deprecated.'): void |
|
763
|
|
|
{ |
|
764
|
|
|
if (!$this->parameterBag instanceof ParameterBag) { |
|
765
|
|
|
throw new BadMethodCallException(\sprintf('The parameter bag must be an instance of "%s" to call "%s".', ParameterBag::class, __METHOD__)); |
|
766
|
|
|
} |
|
767
|
|
|
|
|
768
|
|
|
$this->parameterBag->deprecate($name, $package, $version, $message); |
|
769
|
|
|
} |
|
770
|
|
|
|
|
771
|
|
|
public function parameterCannotBeEmpty(string $name, string $message): void |
|
772
|
|
|
{ |
|
773
|
|
|
if (!$this->parameterBag instanceof ParameterBag) { |
|
774
|
|
|
throw new BadMethodCallException(\sprintf('The parameter bag must be an instance of "%s" to call "%s()".', ParameterBag::class, __METHOD__)); |
|
775
|
|
|
} |
|
776
|
|
|
|
|
777
|
|
|
$this->parameterBag->cannotBeEmpty($name, $message); |
|
778
|
|
|
} |
|
779
|
|
|
|
|
780
|
|
|
/** |
|
781
|
|
|
* Compiles the container. |
|
782
|
|
|
* |
|
783
|
|
|
* This method passes the container to compiler |
|
784
|
|
|
* passes whose job is to manipulate and optimize |
|
785
|
|
|
* the container. |
|
786
|
|
|
* |
|
787
|
|
|
* The main compiler passes roughly do four things: |
|
788
|
|
|
* |
|
789
|
|
|
* * The extension configurations are merged; |
|
790
|
|
|
* * Parameter values are resolved; |
|
791
|
|
|
* * The parameter bag is frozen; |
|
792
|
|
|
* * Extension loading is disabled. |
|
793
|
|
|
* |
|
794
|
|
|
* @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current |
|
795
|
|
|
* env vars or be replaced by uniquely identifiable placeholders. |
|
796
|
|
|
* Set to "true" when you want to use the current ContainerBuilder |
|
797
|
|
|
* directly, keep to "false" when the container is dumped instead. |
|
798
|
|
|
*/ |
|
799
|
|
|
public function compile(bool $resolveEnvPlaceholders = false): void |
|
800
|
|
|
{ |
|
801
|
|
|
$compiler = $this->getCompiler(); |
|
802
|
|
|
|
|
803
|
|
|
if ($this->trackResources) { |
|
804
|
|
|
foreach ($compiler->getPassConfig()->getPasses() as $pass) { |
|
805
|
|
|
$this->addObjectResource($pass); |
|
806
|
|
|
} |
|
807
|
|
|
} |
|
808
|
|
|
$bag = $this->getParameterBag(); |
|
809
|
|
|
|
|
810
|
|
|
if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) { |
|
811
|
|
|
$compiler->addPass(new ResolveEnvPlaceholdersPass(), PassConfig::TYPE_AFTER_REMOVING, -1000); |
|
812
|
|
|
} |
|
813
|
|
|
|
|
814
|
|
|
$compiler->compile($this); |
|
815
|
|
|
|
|
816
|
|
|
foreach ($this->definitions as $id => $definition) { |
|
817
|
|
|
if ($this->trackResources && $definition->isLazy()) { |
|
818
|
|
|
$this->getReflectionClass($definition->getClass()); |
|
819
|
|
|
} |
|
820
|
|
|
} |
|
821
|
|
|
|
|
822
|
|
|
$this->extensionConfigs = []; |
|
823
|
|
|
|
|
824
|
|
|
if ($bag instanceof EnvPlaceholderParameterBag) { |
|
825
|
|
|
if ($resolveEnvPlaceholders) { |
|
826
|
|
|
$this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true)); |
|
827
|
|
|
} |
|
828
|
|
|
|
|
829
|
|
|
$this->envPlaceholders = $bag->getEnvPlaceholders(); |
|
830
|
|
|
} |
|
831
|
|
|
|
|
832
|
|
|
parent::compile(); |
|
833
|
|
|
|
|
834
|
|
|
foreach ($this->definitions + $this->aliasDefinitions as $id => $definition) { |
|
835
|
|
|
if ('.' === ($id[0] ?? '-')) { |
|
836
|
|
|
continue; |
|
837
|
|
|
} |
|
838
|
|
|
if (!$definition->isPublic() || $definition->isPrivate()) { |
|
839
|
|
|
$this->removedIds[$id] = true; |
|
840
|
|
|
} |
|
841
|
|
|
} |
|
842
|
|
|
} |
|
843
|
|
|
|
|
844
|
|
|
public function getServiceIds(): array |
|
845
|
|
|
{ |
|
846
|
|
|
return array_map('strval', array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliasDefinitions), parent::getServiceIds()))); |
|
847
|
|
|
} |
|
848
|
|
|
|
|
849
|
|
|
/** |
|
850
|
|
|
* Gets removed service or alias ids. |
|
851
|
|
|
* |
|
852
|
|
|
* @return array<string, bool> |
|
853
|
|
|
*/ |
|
854
|
|
|
public function getRemovedIds(): array |
|
855
|
|
|
{ |
|
856
|
|
|
return $this->removedIds; |
|
857
|
|
|
} |
|
858
|
|
|
|
|
859
|
|
|
/** |
|
860
|
|
|
* Adds the service aliases. |
|
861
|
|
|
* |
|
862
|
|
|
* @param array<string, string|Alias> $aliases |
|
863
|
|
|
*/ |
|
864
|
|
|
public function addAliases(array $aliases): void |
|
865
|
|
|
{ |
|
866
|
|
|
foreach ($aliases as $alias => $id) { |
|
867
|
|
|
$this->setAlias($alias, $id); |
|
868
|
|
|
} |
|
869
|
|
|
} |
|
870
|
|
|
|
|
871
|
|
|
/** |
|
872
|
|
|
* Sets the service aliases. |
|
873
|
|
|
* |
|
874
|
|
|
* @param array<string, string|Alias> $aliases |
|
875
|
|
|
*/ |
|
876
|
|
|
public function setAliases(array $aliases): void |
|
877
|
|
|
{ |
|
878
|
|
|
$this->aliasDefinitions = []; |
|
879
|
|
|
$this->addAliases($aliases); |
|
880
|
|
|
} |
|
881
|
|
|
|
|
882
|
|
|
/** |
|
883
|
|
|
* Sets an alias for an existing service. |
|
884
|
|
|
* |
|
885
|
|
|
* @throws InvalidArgumentException if the id is not a string or an Alias |
|
886
|
|
|
* @throws InvalidArgumentException if the alias is for itself |
|
887
|
|
|
*/ |
|
888
|
|
|
public function setAlias(string $alias, string|Alias $id): Alias |
|
889
|
|
|
{ |
|
890
|
|
|
if ('' === $alias || '\\' === $alias[-1] || \strlen($alias) !== strcspn($alias, "\0\r\n'")) { |
|
891
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid alias id: "%s".', $alias)); |
|
892
|
|
|
} |
|
893
|
|
|
|
|
894
|
|
|
if (\is_string($id)) { |
|
895
|
|
|
$id = new Alias($id); |
|
896
|
|
|
} |
|
897
|
|
|
|
|
898
|
|
|
if ($alias === (string) $id) { |
|
899
|
|
|
throw new InvalidArgumentException(\sprintf('An alias cannot reference itself, got a circular reference on "%s".', $alias)); |
|
900
|
|
|
} |
|
901
|
|
|
|
|
902
|
|
|
unset($this->definitions[$alias], $this->removedIds[$alias]); |
|
903
|
|
|
|
|
904
|
|
|
return $this->aliasDefinitions[$alias] = $id; |
|
905
|
|
|
} |
|
906
|
|
|
|
|
907
|
|
|
public function removeAlias(string $alias): void |
|
908
|
|
|
{ |
|
909
|
|
|
if (isset($this->aliasDefinitions[$alias])) { |
|
910
|
|
|
unset($this->aliasDefinitions[$alias]); |
|
911
|
|
|
if ('.' !== ($alias[0] ?? '-')) { |
|
912
|
|
|
$this->removedIds[$alias] = true; |
|
913
|
|
|
} |
|
914
|
|
|
} |
|
915
|
|
|
} |
|
916
|
|
|
|
|
917
|
|
|
public function hasAlias(string $id): bool |
|
918
|
|
|
{ |
|
919
|
|
|
return isset($this->aliasDefinitions[$id]); |
|
920
|
|
|
} |
|
921
|
|
|
|
|
922
|
|
|
/** |
|
923
|
|
|
* @return array<string, Alias> |
|
924
|
|
|
*/ |
|
925
|
|
|
public function getAliases(): array |
|
926
|
|
|
{ |
|
927
|
|
|
return $this->aliasDefinitions; |
|
928
|
|
|
} |
|
929
|
|
|
|
|
930
|
|
|
/** |
|
931
|
|
|
* @throws InvalidArgumentException if the alias does not exist |
|
932
|
|
|
*/ |
|
933
|
|
|
public function getAlias(string $id): Alias |
|
934
|
|
|
{ |
|
935
|
|
|
if (!isset($this->aliasDefinitions[$id])) { |
|
936
|
|
|
throw new InvalidArgumentException(\sprintf('The service alias "%s" does not exist.', $id)); |
|
937
|
|
|
} |
|
938
|
|
|
|
|
939
|
|
|
return $this->aliasDefinitions[$id]; |
|
940
|
|
|
} |
|
941
|
|
|
|
|
942
|
|
|
/** |
|
943
|
|
|
* Registers a service definition. |
|
944
|
|
|
* |
|
945
|
|
|
* This method allows for simple registration of service definition |
|
946
|
|
|
* with a fluid interface. |
|
947
|
|
|
*/ |
|
948
|
|
|
public function register(string $id, ?string $class = null): Definition |
|
949
|
|
|
{ |
|
950
|
|
|
return $this->setDefinition($id, new Definition($class)); |
|
951
|
|
|
} |
|
952
|
|
|
|
|
953
|
|
|
/** |
|
954
|
|
|
* This method provides a fluid interface for easily registering a child |
|
955
|
|
|
* service definition of the given parent service. |
|
956
|
|
|
*/ |
|
957
|
|
|
public function registerChild(string $id, string $parent): ChildDefinition |
|
958
|
|
|
{ |
|
959
|
|
|
return $this->setDefinition($id, new ChildDefinition($parent)); |
|
960
|
|
|
} |
|
961
|
|
|
|
|
962
|
|
|
/** |
|
963
|
|
|
* Registers an autowired service definition. |
|
964
|
|
|
* |
|
965
|
|
|
* This method implements a shortcut for using setDefinition() with |
|
966
|
|
|
* an autowired definition. |
|
967
|
|
|
*/ |
|
968
|
|
|
public function autowire(string $id, ?string $class = null): Definition |
|
969
|
|
|
{ |
|
970
|
|
|
return $this->setDefinition($id, (new Definition($class))->setAutowired(true)); |
|
971
|
|
|
} |
|
972
|
|
|
|
|
973
|
|
|
/** |
|
974
|
|
|
* Adds the service definitions. |
|
975
|
|
|
* |
|
976
|
|
|
* @param array<string, Definition> $definitions |
|
977
|
|
|
*/ |
|
978
|
|
|
public function addDefinitions(array $definitions): void |
|
979
|
|
|
{ |
|
980
|
|
|
foreach ($definitions as $id => $definition) { |
|
981
|
|
|
$this->setDefinition($id, $definition); |
|
982
|
|
|
} |
|
983
|
|
|
} |
|
984
|
|
|
|
|
985
|
|
|
/** |
|
986
|
|
|
* Sets the service definitions. |
|
987
|
|
|
* |
|
988
|
|
|
* @param array<string, Definition> $definitions |
|
989
|
|
|
*/ |
|
990
|
|
|
public function setDefinitions(array $definitions): void |
|
991
|
|
|
{ |
|
992
|
|
|
$this->definitions = []; |
|
993
|
|
|
$this->addDefinitions($definitions); |
|
994
|
|
|
} |
|
995
|
|
|
|
|
996
|
|
|
/** |
|
997
|
|
|
* Gets all service definitions. |
|
998
|
|
|
* |
|
999
|
|
|
* @return array<string, Definition> |
|
1000
|
|
|
*/ |
|
1001
|
|
|
public function getDefinitions(): array |
|
1002
|
|
|
{ |
|
1003
|
|
|
return $this->definitions; |
|
1004
|
|
|
} |
|
1005
|
|
|
|
|
1006
|
|
|
/** |
|
1007
|
|
|
* Sets a service definition. |
|
1008
|
|
|
* |
|
1009
|
|
|
* @throws BadMethodCallException When this ContainerBuilder is compiled |
|
1010
|
|
|
*/ |
|
1011
|
|
|
public function setDefinition(string $id, Definition $definition): Definition |
|
1012
|
|
|
{ |
|
1013
|
|
|
if ($this->isCompiled()) { |
|
1014
|
|
|
throw new BadMethodCallException('Adding definition to a compiled container is not allowed.'); |
|
1015
|
|
|
} |
|
1016
|
|
|
|
|
1017
|
|
|
if ('' === $id || '\\' === $id[-1] || \strlen($id) !== strcspn($id, "\0\r\n'")) { |
|
1018
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid service id: "%s".', $id)); |
|
1019
|
|
|
} |
|
1020
|
|
|
|
|
1021
|
|
|
unset($this->aliasDefinitions[$id], $this->removedIds[$id]); |
|
1022
|
|
|
|
|
1023
|
|
|
return $this->definitions[$id] = $definition; |
|
1024
|
|
|
} |
|
1025
|
|
|
|
|
1026
|
|
|
/** |
|
1027
|
|
|
* Returns true if a service definition exists under the given identifier. |
|
1028
|
|
|
*/ |
|
1029
|
|
|
public function hasDefinition(string $id): bool |
|
1030
|
|
|
{ |
|
1031
|
|
|
return isset($this->definitions[$id]); |
|
1032
|
|
|
} |
|
1033
|
|
|
|
|
1034
|
|
|
/** |
|
1035
|
|
|
* Gets a service definition. |
|
1036
|
|
|
* |
|
1037
|
|
|
* @throws ServiceNotFoundException if the service definition does not exist |
|
1038
|
|
|
*/ |
|
1039
|
|
|
public function getDefinition(string $id): Definition |
|
1040
|
|
|
{ |
|
1041
|
|
|
if (!isset($this->definitions[$id])) { |
|
1042
|
|
|
throw new ServiceNotFoundException($id); |
|
1043
|
|
|
} |
|
1044
|
|
|
|
|
1045
|
|
|
return $this->definitions[$id]; |
|
1046
|
|
|
} |
|
1047
|
|
|
|
|
1048
|
|
|
/** |
|
1049
|
|
|
* Gets a service definition by id or alias. |
|
1050
|
|
|
* |
|
1051
|
|
|
* The method "unaliases" recursively to return a Definition instance. |
|
1052
|
|
|
* |
|
1053
|
|
|
* @throws ServiceNotFoundException if the service definition does not exist |
|
1054
|
|
|
*/ |
|
1055
|
|
|
public function findDefinition(string $id): Definition |
|
1056
|
|
|
{ |
|
1057
|
|
|
$seen = []; |
|
1058
|
|
|
while (isset($this->aliasDefinitions[$id])) { |
|
1059
|
|
|
$id = (string) $this->aliasDefinitions[$id]; |
|
1060
|
|
|
|
|
1061
|
|
|
if (isset($seen[$id])) { |
|
1062
|
|
|
$seen = array_values($seen); |
|
1063
|
|
|
$seen = \array_slice($seen, array_search($id, $seen)); |
|
|
|
|
|
|
1064
|
|
|
$seen[] = $id; |
|
1065
|
|
|
|
|
1066
|
|
|
throw new ServiceCircularReferenceException($id, $seen); |
|
1067
|
|
|
} |
|
1068
|
|
|
|
|
1069
|
|
|
$seen[$id] = $id; |
|
1070
|
|
|
} |
|
1071
|
|
|
|
|
1072
|
|
|
return $this->getDefinition($id); |
|
1073
|
|
|
} |
|
1074
|
|
|
|
|
1075
|
|
|
/** |
|
1076
|
|
|
* Creates a service for a service definition. |
|
1077
|
|
|
* |
|
1078
|
|
|
* @throws RuntimeException When the factory definition is incomplete |
|
1079
|
|
|
* @throws RuntimeException When the service is a synthetic service |
|
1080
|
|
|
* @throws InvalidArgumentException When configure callable is not callable |
|
1081
|
|
|
*/ |
|
1082
|
|
|
private function createService(Definition $definition, array &$inlineServices, bool $isConstructorArgument = false, ?string $id = null, bool|object $tryProxy = true): mixed |
|
1083
|
|
|
{ |
|
1084
|
|
|
if (null === $id && isset($inlineServices[$h = spl_object_hash($definition)])) { |
|
1085
|
|
|
return $inlineServices[$h]; |
|
1086
|
|
|
} |
|
1087
|
|
|
|
|
1088
|
|
|
if ($definition instanceof ChildDefinition) { |
|
1089
|
|
|
throw new RuntimeException(\sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id)); |
|
1090
|
|
|
} |
|
1091
|
|
|
|
|
1092
|
|
|
if ($definition->isSynthetic()) { |
|
1093
|
|
|
throw new RuntimeException(\sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id)); |
|
1094
|
|
|
} |
|
1095
|
|
|
|
|
1096
|
|
|
if ($definition->isDeprecated()) { |
|
1097
|
|
|
$deprecation = $definition->getDeprecation($id); |
|
|
|
|
|
|
1098
|
|
|
trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']); |
|
1099
|
|
|
} |
|
1100
|
|
|
|
|
1101
|
|
|
$parameterBag = $this->getParameterBag(); |
|
1102
|
|
|
$class = $parameterBag->resolveValue($definition->getClass()) ?: (['Closure', 'fromCallable'] === $definition->getFactory() ? 'Closure' : null); |
|
1103
|
|
|
|
|
1104
|
|
|
if (['Closure', 'fromCallable'] === $definition->getFactory() && ('Closure' !== $class || $definition->isLazy())) { |
|
1105
|
|
|
$callable = $parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArgument(0))); |
|
1106
|
|
|
|
|
1107
|
|
|
if ($callable instanceof Reference || $callable instanceof Definition) { |
|
1108
|
|
|
$callable = [$callable, '__invoke']; |
|
1109
|
|
|
} |
|
1110
|
|
|
|
|
1111
|
|
|
if (\is_array($callable) && ( |
|
1112
|
|
|
$callable[0] instanceof Reference |
|
1113
|
|
|
|| $callable[0] instanceof Definition && !isset($inlineServices[spl_object_hash($callable[0])]) |
|
1114
|
|
|
)) { |
|
1115
|
|
|
$initializer = function () use ($callable, &$inlineServices) { |
|
|
|
|
|
|
1116
|
|
|
return $this->doResolveServices($callable[0], $inlineServices); |
|
1117
|
|
|
}; |
|
1118
|
|
|
|
|
1119
|
|
|
$proxy = eval('return '.LazyClosure::getCode('$initializer', $callable, $definition, $this, $id).';'); |
|
|
|
|
|
|
1120
|
|
|
$this->shareService($definition, $proxy, $id, $inlineServices); |
|
1121
|
|
|
|
|
1122
|
|
|
return $proxy; |
|
1123
|
|
|
} |
|
1124
|
|
|
} |
|
1125
|
|
|
|
|
1126
|
|
|
if (true === $tryProxy && $definition->isLazy() && ['Closure', 'fromCallable'] !== $definition->getFactory() |
|
1127
|
|
|
&& !$tryProxy = !($proxy = $this->proxyInstantiator ??= new LazyServiceInstantiator()) || $proxy instanceof RealServiceInstantiator |
|
|
|
|
|
|
1128
|
|
|
) { |
|
1129
|
|
|
$proxy = $proxy->instantiateProxy( |
|
1130
|
|
|
$this, |
|
1131
|
|
|
(clone $definition) |
|
1132
|
|
|
->setClass($class) |
|
1133
|
|
|
->setTags(($definition->hasTag('proxy') ? ['proxy' => $parameterBag->resolveValue($definition->getTag('proxy'))] : []) + $definition->getTags()), |
|
1134
|
|
|
$id, function ($proxy = false) use ($definition, &$inlineServices, $id) { |
|
|
|
|
|
|
1135
|
|
|
return $this->createService($definition, $inlineServices, true, $id, $proxy); |
|
1136
|
|
|
} |
|
1137
|
|
|
); |
|
1138
|
|
|
$this->shareService($definition, $proxy, $id, $inlineServices); |
|
1139
|
|
|
|
|
1140
|
|
|
return $proxy; |
|
1141
|
|
|
} |
|
1142
|
|
|
|
|
1143
|
|
|
if (null !== $definition->getFile()) { |
|
1144
|
|
|
require_once $parameterBag->resolveValue($definition->getFile()); |
|
1145
|
|
|
} |
|
1146
|
|
|
|
|
1147
|
|
|
$arguments = $definition->getArguments(); |
|
1148
|
|
|
|
|
1149
|
|
|
if (null !== $factory = $definition->getFactory()) { |
|
1150
|
|
|
if (\is_array($factory)) { |
|
1151
|
|
|
$factory = [$this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices, $isConstructorArgument), $factory[1]]; |
|
1152
|
|
|
} elseif (!\is_string($factory)) { |
|
|
|
|
|
|
1153
|
|
|
throw new RuntimeException(\sprintf('Cannot create service "%s" because of invalid factory.', $id)); |
|
1154
|
|
|
} elseif (str_starts_with($factory, '@=')) { |
|
1155
|
|
|
$factory = fn (ServiceLocator $arguments) => $this->getExpressionLanguage()->evaluate(substr($factory, 2), ['container' => $this, 'args' => $arguments]); |
|
|
|
|
|
|
1156
|
|
|
$arguments = [new ServiceLocatorArgument($arguments)]; |
|
1157
|
|
|
} |
|
1158
|
|
|
} |
|
1159
|
|
|
|
|
1160
|
|
|
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($arguments)), $inlineServices, $isConstructorArgument); |
|
1161
|
|
|
|
|
1162
|
|
|
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && (true === $tryProxy || !$definition->isLazy())) { |
|
1163
|
|
|
return $this->services[$id] ?? $this->privates[$id]; |
|
1164
|
|
|
} |
|
1165
|
|
|
|
|
1166
|
|
|
if (!array_is_list($arguments)) { |
|
|
|
|
|
|
1167
|
|
|
$arguments = array_combine(array_map(fn ($k) => preg_replace('/^.*\\$/', '', $k), array_keys($arguments)), $arguments); |
|
1168
|
|
|
} |
|
1169
|
|
|
|
|
1170
|
|
|
if (null !== $factory) { |
|
1171
|
|
|
$service = $factory(...$arguments); |
|
1172
|
|
|
|
|
1173
|
|
|
if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) { |
|
1174
|
|
|
$r = new \ReflectionClass($factory[0]); |
|
1175
|
|
|
|
|
1176
|
|
|
if (0 < strpos($r->getDocComment(), "\n * @deprecated ")) { |
|
1177
|
|
|
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" factory class. It should either be deprecated or its factory upgraded.', $id, $r->name); |
|
1178
|
|
|
} |
|
1179
|
|
|
} |
|
1180
|
|
|
} else { |
|
1181
|
|
|
$r = new \ReflectionClass($class); |
|
1182
|
|
|
|
|
1183
|
|
|
if (\is_object($tryProxy)) { |
|
1184
|
|
|
if ($r->getConstructor()) { |
|
1185
|
|
|
$tryProxy->__construct(...$arguments); |
|
1186
|
|
|
} |
|
1187
|
|
|
|
|
1188
|
|
|
$service = $tryProxy; |
|
1189
|
|
|
} else { |
|
1190
|
|
|
$service = $r->getConstructor() ? $r->newInstanceArgs($arguments) : $r->newInstance(); |
|
1191
|
|
|
} |
|
1192
|
|
|
|
|
1193
|
|
|
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) { |
|
1194
|
|
|
trigger_deprecation('', '', 'The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name); |
|
1195
|
|
|
} |
|
1196
|
|
|
} |
|
1197
|
|
|
|
|
1198
|
|
|
$lastWitherIndex = null; |
|
1199
|
|
|
foreach ($definition->getMethodCalls() as $k => $call) { |
|
1200
|
|
|
if ($call[2] ?? false) { |
|
1201
|
|
|
$lastWitherIndex = $k; |
|
1202
|
|
|
} |
|
1203
|
|
|
} |
|
1204
|
|
|
|
|
1205
|
|
|
if (null === $lastWitherIndex && (true === $tryProxy || !$definition->isLazy())) { |
|
1206
|
|
|
// share only if proxying failed, or if not a proxy, and if no withers are found |
|
1207
|
|
|
$this->shareService($definition, $service, $id, $inlineServices); |
|
1208
|
|
|
} |
|
1209
|
|
|
|
|
1210
|
|
|
$properties = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())), $inlineServices); |
|
1211
|
|
|
foreach ($properties as $name => $value) { |
|
1212
|
|
|
$service->$name = $value; |
|
1213
|
|
|
} |
|
1214
|
|
|
|
|
1215
|
|
|
foreach ($definition->getMethodCalls() as $k => $call) { |
|
1216
|
|
|
$service = $this->callMethod($service, $call, $inlineServices); |
|
1217
|
|
|
|
|
1218
|
|
|
if ($lastWitherIndex === $k && (true === $tryProxy || !$definition->isLazy())) { |
|
1219
|
|
|
// share only if proxying failed, or if not a proxy, and this is the last wither |
|
1220
|
|
|
$this->shareService($definition, $service, $id, $inlineServices); |
|
1221
|
|
|
} |
|
1222
|
|
|
} |
|
1223
|
|
|
|
|
1224
|
|
|
if ($callable = $definition->getConfigurator()) { |
|
1225
|
|
|
if (\is_array($callable)) { |
|
1226
|
|
|
$callable[0] = $parameterBag->resolveValue($callable[0]); |
|
1227
|
|
|
|
|
1228
|
|
|
if ($callable[0] instanceof Reference) { |
|
1229
|
|
|
$callable[0] = $this->doGet((string) $callable[0], $callable[0]->getInvalidBehavior(), $inlineServices); |
|
1230
|
|
|
} elseif ($callable[0] instanceof Definition) { |
|
1231
|
|
|
$callable[0] = $this->createService($callable[0], $inlineServices); |
|
1232
|
|
|
} |
|
1233
|
|
|
} |
|
1234
|
|
|
|
|
1235
|
|
|
if (!\is_callable($callable)) { |
|
1236
|
|
|
throw new InvalidArgumentException(\sprintf('The configure callable for class "%s" is not a callable.', get_debug_type($service))); |
|
1237
|
|
|
} |
|
1238
|
|
|
|
|
1239
|
|
|
$callable($service); |
|
1240
|
|
|
} |
|
1241
|
|
|
|
|
1242
|
|
|
return $service; |
|
1243
|
|
|
} |
|
1244
|
|
|
|
|
1245
|
|
|
/** |
|
1246
|
|
|
* Replaces service references by the real service instance and evaluates expressions. |
|
1247
|
|
|
* |
|
1248
|
|
|
* @return mixed The same value with all service references replaced by |
|
1249
|
|
|
* the real service instances and all expressions evaluated |
|
1250
|
|
|
*/ |
|
1251
|
|
|
public function resolveServices(mixed $value): mixed |
|
1252
|
|
|
{ |
|
1253
|
|
|
return $this->doResolveServices($value); |
|
1254
|
|
|
} |
|
1255
|
|
|
|
|
1256
|
|
|
private function doResolveServices(mixed $value, array &$inlineServices = [], bool $isConstructorArgument = false): mixed |
|
1257
|
|
|
{ |
|
1258
|
|
|
if (\is_array($value)) { |
|
1259
|
|
|
foreach ($value as $k => $v) { |
|
1260
|
|
|
$value[$k] = $this->doResolveServices($v, $inlineServices, $isConstructorArgument); |
|
1261
|
|
|
} |
|
1262
|
|
|
} elseif ($value instanceof ServiceClosureArgument) { |
|
1263
|
|
|
$reference = $value->getValues()[0]; |
|
1264
|
|
|
$value = fn () => $this->resolveServices($reference); |
|
1265
|
|
|
} elseif ($value instanceof IteratorArgument) { |
|
1266
|
|
|
$value = new RewindableGenerator(function () use ($value, &$inlineServices) { |
|
1267
|
|
|
foreach ($value->getValues() as $k => $v) { |
|
1268
|
|
|
foreach (self::getServiceConditionals($v) as $s) { |
|
1269
|
|
|
if (!$this->has($s)) { |
|
1270
|
|
|
continue 2; |
|
1271
|
|
|
} |
|
1272
|
|
|
} |
|
1273
|
|
|
foreach (self::getInitializedConditionals($v) as $s) { |
|
1274
|
|
|
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE, $inlineServices)) { |
|
1275
|
|
|
continue 2; |
|
1276
|
|
|
} |
|
1277
|
|
|
} |
|
1278
|
|
|
|
|
1279
|
|
|
yield $k => $this->doResolveServices($v, $inlineServices); |
|
1280
|
|
|
} |
|
1281
|
|
|
}, function () use ($value): int { |
|
1282
|
|
|
$count = 0; |
|
1283
|
|
|
foreach ($value->getValues() as $v) { |
|
1284
|
|
|
foreach (self::getServiceConditionals($v) as $s) { |
|
1285
|
|
|
if (!$this->has($s)) { |
|
1286
|
|
|
continue 2; |
|
1287
|
|
|
} |
|
1288
|
|
|
} |
|
1289
|
|
|
foreach (self::getInitializedConditionals($v) as $s) { |
|
1290
|
|
|
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) { |
|
1291
|
|
|
continue 2; |
|
1292
|
|
|
} |
|
1293
|
|
|
} |
|
1294
|
|
|
|
|
1295
|
|
|
++$count; |
|
1296
|
|
|
} |
|
1297
|
|
|
|
|
1298
|
|
|
return $count; |
|
1299
|
|
|
}); |
|
1300
|
|
|
} elseif ($value instanceof ServiceLocatorArgument) { |
|
1301
|
|
|
$refs = $types = []; |
|
1302
|
|
|
foreach ($value->getValues() as $k => $v) { |
|
1303
|
|
|
$refs[$k] = [$v, null]; |
|
1304
|
|
|
$types[$k] = $v instanceof TypedReference ? $v->getType() : '?'; |
|
1305
|
|
|
} |
|
1306
|
|
|
$value = new ServiceLocator($this->resolveServices(...), $refs, $types); |
|
1307
|
|
|
} elseif ($value instanceof Reference) { |
|
1308
|
|
|
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices, $isConstructorArgument); |
|
1309
|
|
|
} elseif ($value instanceof Definition) { |
|
1310
|
|
|
$value = $this->createService($value, $inlineServices, $isConstructorArgument); |
|
1311
|
|
|
} elseif ($value instanceof Parameter) { |
|
1312
|
|
|
$value = $this->getParameter((string) $value); |
|
1313
|
|
|
} elseif ($value instanceof Expression) { |
|
1314
|
|
|
$value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this]); |
|
1315
|
|
|
} elseif ($value instanceof AbstractArgument) { |
|
1316
|
|
|
throw new RuntimeException($value->getTextWithContext()); |
|
1317
|
|
|
} |
|
1318
|
|
|
|
|
1319
|
|
|
return $value; |
|
1320
|
|
|
} |
|
1321
|
|
|
|
|
1322
|
|
|
/** |
|
1323
|
|
|
* Returns service ids for a given tag. |
|
1324
|
|
|
* |
|
1325
|
|
|
* Example: |
|
1326
|
|
|
* |
|
1327
|
|
|
* $container->register('foo')->addTag('my.tag', ['hello' => 'world']); |
|
1328
|
|
|
* |
|
1329
|
|
|
* $serviceIds = $container->findTaggedServiceIds('my.tag'); |
|
1330
|
|
|
* foreach ($serviceIds as $serviceId => $tags) { |
|
1331
|
|
|
* foreach ($tags as $tag) { |
|
1332
|
|
|
* echo $tag['hello']; |
|
1333
|
|
|
* } |
|
1334
|
|
|
* } |
|
1335
|
|
|
* |
|
1336
|
|
|
* @return array<string, array> An array of tags with the tagged service as key, holding a list of attribute arrays |
|
1337
|
|
|
*/ |
|
1338
|
|
|
public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false): array |
|
1339
|
|
|
{ |
|
1340
|
|
|
$this->usedTags[] = $name; |
|
1341
|
|
|
$tags = []; |
|
1342
|
|
|
foreach ($this->getDefinitions() as $id => $definition) { |
|
1343
|
|
|
if ($definition->hasTag($name) && !$definition->hasTag('container.excluded')) { |
|
1344
|
|
|
if ($throwOnAbstract && $definition->isAbstract()) { |
|
1345
|
|
|
throw new InvalidArgumentException(\sprintf('The service "%s" tagged "%s" must not be abstract.', $id, $name)); |
|
1346
|
|
|
} |
|
1347
|
|
|
$tags[$id] = $definition->getTag($name); |
|
1348
|
|
|
} |
|
1349
|
|
|
} |
|
1350
|
|
|
|
|
1351
|
|
|
return $tags; |
|
1352
|
|
|
} |
|
1353
|
|
|
|
|
1354
|
|
|
/** |
|
1355
|
|
|
* Returns all tags the defined services use. |
|
1356
|
|
|
* |
|
1357
|
|
|
* @return string[] |
|
1358
|
|
|
*/ |
|
1359
|
|
|
public function findTags(): array |
|
1360
|
|
|
{ |
|
1361
|
|
|
$tags = []; |
|
1362
|
|
|
foreach ($this->getDefinitions() as $id => $definition) { |
|
1363
|
|
|
$tags[] = array_keys($definition->getTags()); |
|
1364
|
|
|
} |
|
1365
|
|
|
|
|
1366
|
|
|
return array_unique(array_merge([], ...$tags)); |
|
1367
|
|
|
} |
|
1368
|
|
|
|
|
1369
|
|
|
/** |
|
1370
|
|
|
* Returns all tags not queried by findTaggedServiceIds. |
|
1371
|
|
|
* |
|
1372
|
|
|
* @return string[] |
|
1373
|
|
|
*/ |
|
1374
|
|
|
public function findUnusedTags(): array |
|
1375
|
|
|
{ |
|
1376
|
|
|
return array_values(array_diff($this->findTags(), $this->usedTags)); |
|
1377
|
|
|
} |
|
1378
|
|
|
|
|
1379
|
|
|
public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider): void |
|
1380
|
|
|
{ |
|
1381
|
|
|
$this->expressionLanguageProviders[] = $provider; |
|
1382
|
|
|
} |
|
1383
|
|
|
|
|
1384
|
|
|
/** |
|
1385
|
|
|
* @return ExpressionFunctionProviderInterface[] |
|
1386
|
|
|
*/ |
|
1387
|
|
|
public function getExpressionLanguageProviders(): array |
|
1388
|
|
|
{ |
|
1389
|
|
|
return $this->expressionLanguageProviders; |
|
1390
|
|
|
} |
|
1391
|
|
|
|
|
1392
|
|
|
/** |
|
1393
|
|
|
* Returns a ChildDefinition that will be used for autoconfiguring the interface/class. |
|
1394
|
|
|
*/ |
|
1395
|
|
|
public function registerForAutoconfiguration(string $interface): ChildDefinition |
|
1396
|
|
|
{ |
|
1397
|
|
|
if (!isset($this->autoconfiguredInstanceof[$interface])) { |
|
1398
|
|
|
$this->autoconfiguredInstanceof[$interface] = new ChildDefinition(''); |
|
1399
|
|
|
} |
|
1400
|
|
|
|
|
1401
|
|
|
return $this->autoconfiguredInstanceof[$interface]; |
|
1402
|
|
|
} |
|
1403
|
|
|
|
|
1404
|
|
|
/** |
|
1405
|
|
|
* Registers an attribute that will be used for autoconfiguring annotated classes. |
|
1406
|
|
|
* |
|
1407
|
|
|
* The third argument passed to the callable is the reflector of the |
|
1408
|
|
|
* class/method/property/parameter that the attribute targets. Using one or many of |
|
1409
|
|
|
* \ReflectionClass|\ReflectionMethod|\ReflectionProperty|\ReflectionParameter as a type-hint |
|
1410
|
|
|
* for this argument allows filtering which attributes should be passed to the callable. |
|
1411
|
|
|
* |
|
1412
|
|
|
* @template T |
|
1413
|
|
|
* |
|
1414
|
|
|
* @param class-string<T> $attributeClass |
|
|
|
|
|
|
1415
|
|
|
* @param callable(ChildDefinition, T, \Reflector): void $configurator |
|
1416
|
|
|
*/ |
|
1417
|
|
|
public function registerAttributeForAutoconfiguration(string $attributeClass, callable $configurator): void |
|
1418
|
|
|
{ |
|
1419
|
|
|
$this->autoconfiguredAttributes[$attributeClass] = $configurator; |
|
1420
|
|
|
} |
|
1421
|
|
|
|
|
1422
|
|
|
/** |
|
1423
|
|
|
* Registers an autowiring alias that only binds to a specific argument name. |
|
1424
|
|
|
* |
|
1425
|
|
|
* The argument name is derived from $name if provided (from $id otherwise) |
|
1426
|
|
|
* using camel case: "foo.bar" or "foo_bar" creates an alias bound to |
|
1427
|
|
|
* "$fooBar"-named arguments with $type as type-hint. Such arguments will |
|
1428
|
|
|
* receive the service $id when autowiring is used. |
|
1429
|
|
|
*/ |
|
1430
|
|
|
public function registerAliasForArgument(string $id, string $type, ?string $name = null): Alias |
|
1431
|
|
|
{ |
|
1432
|
|
|
$parsedName = (new Target($name ??= $id))->getParsedName(); |
|
1433
|
|
|
|
|
1434
|
|
|
if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $parsedName)) { |
|
1435
|
|
|
if ($id !== $name) { |
|
1436
|
|
|
$id = \sprintf(' for service "%s"', $id); |
|
1437
|
|
|
} |
|
1438
|
|
|
|
|
1439
|
|
|
throw new InvalidArgumentException(\sprintf('Invalid argument name "%s"'.$id.': the first character must be a letter.', $name)); |
|
1440
|
|
|
} |
|
1441
|
|
|
|
|
1442
|
|
|
if ($parsedName !== $name) { |
|
1443
|
|
|
$this->setAlias('.'.$type.' $'.$name, $type.' $'.$parsedName); |
|
1444
|
|
|
} |
|
1445
|
|
|
|
|
1446
|
|
|
return $this->setAlias($type.' $'.$parsedName, $id); |
|
1447
|
|
|
} |
|
1448
|
|
|
|
|
1449
|
|
|
/** |
|
1450
|
|
|
* Returns an array of ChildDefinition[] keyed by interface. |
|
1451
|
|
|
* |
|
1452
|
|
|
* @return array<string, ChildDefinition> |
|
1453
|
|
|
*/ |
|
1454
|
|
|
public function getAutoconfiguredInstanceof(): array |
|
1455
|
|
|
{ |
|
1456
|
|
|
return $this->autoconfiguredInstanceof; |
|
1457
|
|
|
} |
|
1458
|
|
|
|
|
1459
|
|
|
/** |
|
1460
|
|
|
* @return array<string, callable> |
|
1461
|
|
|
*/ |
|
1462
|
|
|
public function getAutoconfiguredAttributes(): array |
|
1463
|
|
|
{ |
|
1464
|
|
|
return $this->autoconfiguredAttributes; |
|
1465
|
|
|
} |
|
1466
|
|
|
|
|
1467
|
|
|
/** |
|
1468
|
|
|
* Resolves env parameter placeholders in a string or an array. |
|
1469
|
|
|
* |
|
1470
|
|
|
* @param string|true|null $format A sprintf() format returning the replacement for each env var name or |
|
1471
|
|
|
* null to resolve back to the original "%env(VAR)%" format or |
|
1472
|
|
|
* true to resolve to the actual values of the referenced env vars |
|
1473
|
|
|
* @param array &$usedEnvs Env vars found while resolving are added to this array |
|
1474
|
|
|
* |
|
1475
|
|
|
* @return mixed The value with env parameters resolved if a string or an array is passed |
|
1476
|
|
|
*/ |
|
1477
|
|
|
public function resolveEnvPlaceholders(mixed $value, string|bool|null $format = null, ?array &$usedEnvs = null): mixed |
|
1478
|
|
|
{ |
|
1479
|
|
|
$bag = $this->getParameterBag(); |
|
1480
|
|
|
if (true === $format ??= '%%env(%s)%%') { |
|
1481
|
|
|
$value = $bag->resolveValue($value); |
|
1482
|
|
|
} |
|
1483
|
|
|
|
|
1484
|
|
|
if ($value instanceof Definition) { |
|
1485
|
|
|
$value = (array) $value; |
|
1486
|
|
|
} |
|
1487
|
|
|
|
|
1488
|
|
|
if (\is_array($value)) { |
|
1489
|
|
|
$result = []; |
|
1490
|
|
|
foreach ($value as $k => $v) { |
|
1491
|
|
|
$result[\is_string($k) ? $this->resolveEnvPlaceholders($k, $format, $usedEnvs) : $k] = $this->resolveEnvPlaceholders($v, $format, $usedEnvs); |
|
1492
|
|
|
} |
|
1493
|
|
|
|
|
1494
|
|
|
return $result; |
|
1495
|
|
|
} |
|
1496
|
|
|
|
|
1497
|
|
|
if (!\is_string($value) || 38 > \strlen($value) || false === stripos($value, 'env_')) { |
|
1498
|
|
|
return $value; |
|
1499
|
|
|
} |
|
1500
|
|
|
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders; |
|
1501
|
|
|
|
|
1502
|
|
|
$completed = false; |
|
1503
|
|
|
preg_match_all('/env_[a-f0-9]{16}_\w+_[a-f0-9]{32}/Ui', $value, $matches); |
|
1504
|
|
|
$usedPlaceholders = array_flip($matches[0]); |
|
1505
|
|
|
foreach ($envPlaceholders as $env => $placeholders) { |
|
1506
|
|
|
foreach ($placeholders as $placeholder) { |
|
1507
|
|
|
if (isset($usedPlaceholders[$placeholder])) { |
|
1508
|
|
|
if (true === $format) { |
|
1509
|
|
|
$resolved = $bag->escapeValue($this->getEnv($env)); |
|
1510
|
|
|
} else { |
|
1511
|
|
|
$resolved = \sprintf($format, $env); |
|
|
|
|
|
|
1512
|
|
|
} |
|
1513
|
|
|
if ($placeholder === $value) { |
|
1514
|
|
|
$value = $resolved; |
|
1515
|
|
|
$completed = true; |
|
1516
|
|
|
} else { |
|
1517
|
|
|
if (!\is_string($resolved) && !is_numeric($resolved)) { |
|
1518
|
|
|
throw new RuntimeException(\sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type "%s" inside string value "%s".', $env, get_debug_type($resolved), $this->resolveEnvPlaceholders($value))); |
|
1519
|
|
|
} |
|
1520
|
|
|
$value = str_ireplace($placeholder, $resolved, $value); |
|
1521
|
|
|
} |
|
1522
|
|
|
$usedEnvs[$env] = $env; |
|
1523
|
|
|
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1; |
|
1524
|
|
|
|
|
1525
|
|
|
if ($completed) { |
|
1526
|
|
|
break 2; |
|
1527
|
|
|
} |
|
1528
|
|
|
} |
|
1529
|
|
|
} |
|
1530
|
|
|
} |
|
1531
|
|
|
|
|
1532
|
|
|
return $value; |
|
1533
|
|
|
} |
|
1534
|
|
|
|
|
1535
|
|
|
/** |
|
1536
|
|
|
* Get statistics about env usage. |
|
1537
|
|
|
* |
|
1538
|
|
|
* @return int[] The number of time each env vars has been resolved |
|
1539
|
|
|
*/ |
|
1540
|
|
|
public function getEnvCounters(): array |
|
1541
|
|
|
{ |
|
1542
|
|
|
$bag = $this->getParameterBag(); |
|
1543
|
|
|
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders; |
|
1544
|
|
|
|
|
1545
|
|
|
foreach ($envPlaceholders as $env => $placeholders) { |
|
1546
|
|
|
if (!isset($this->envCounters[$env])) { |
|
1547
|
|
|
$this->envCounters[$env] = 0; |
|
1548
|
|
|
} |
|
1549
|
|
|
} |
|
1550
|
|
|
|
|
1551
|
|
|
return $this->envCounters; |
|
1552
|
|
|
} |
|
1553
|
|
|
|
|
1554
|
|
|
/** |
|
1555
|
|
|
* @final |
|
1556
|
|
|
*/ |
|
1557
|
|
|
public function log(CompilerPassInterface $pass, string $message): void |
|
1558
|
|
|
{ |
|
1559
|
|
|
$this->getCompiler()->log($pass, $this->resolveEnvPlaceholders($message)); |
|
1560
|
|
|
} |
|
1561
|
|
|
|
|
1562
|
|
|
/** |
|
1563
|
|
|
* Checks whether a class is available and will remain available in the "no-dev" mode of Composer. |
|
1564
|
|
|
* |
|
1565
|
|
|
* When parent packages are provided and if any of them is in dev-only mode, |
|
1566
|
|
|
* the class will be considered available even if it is also in dev-only mode. |
|
1567
|
|
|
* |
|
1568
|
|
|
* @throws \LogicException If dependencies have been installed with Composer 1 |
|
1569
|
|
|
*/ |
|
1570
|
|
|
final public static function willBeAvailable(string $package, string $class, array $parentPackages): bool |
|
1571
|
|
|
{ |
|
1572
|
|
|
if (!class_exists(InstalledVersions::class)) { |
|
1573
|
|
|
throw new \LogicException(\sprintf('Calling "%s" when dependencies have been installed with Composer 1 is not supported. Consider upgrading to Composer 2.', __METHOD__)); |
|
1574
|
|
|
} |
|
1575
|
|
|
|
|
1576
|
|
|
if (!class_exists($class) && !interface_exists($class, false) && !trait_exists($class, false)) { |
|
1577
|
|
|
return false; |
|
1578
|
|
|
} |
|
1579
|
|
|
|
|
1580
|
|
|
if (!InstalledVersions::isInstalled($package) || InstalledVersions::isInstalled($package, false)) { |
|
1581
|
|
|
return true; |
|
1582
|
|
|
} |
|
1583
|
|
|
|
|
1584
|
|
|
// the package is installed but in dev-mode only, check if this applies to one of the parent packages too |
|
1585
|
|
|
|
|
1586
|
|
|
$rootPackage = InstalledVersions::getRootPackage()['name'] ?? ''; |
|
1587
|
|
|
|
|
1588
|
|
|
if ('symfony/symfony' === $rootPackage) { |
|
1589
|
|
|
return true; |
|
1590
|
|
|
} |
|
1591
|
|
|
|
|
1592
|
|
|
foreach ($parentPackages as $parentPackage) { |
|
1593
|
|
|
if ($rootPackage === $parentPackage || (InstalledVersions::isInstalled($parentPackage) && !InstalledVersions::isInstalled($parentPackage, false))) { |
|
1594
|
|
|
return true; |
|
1595
|
|
|
} |
|
1596
|
|
|
} |
|
1597
|
|
|
|
|
1598
|
|
|
return false; |
|
1599
|
|
|
} |
|
1600
|
|
|
|
|
1601
|
|
|
/** |
|
1602
|
|
|
* Gets removed binding ids. |
|
1603
|
|
|
* |
|
1604
|
|
|
* @return array<int, bool> |
|
1605
|
|
|
* |
|
1606
|
|
|
* @internal |
|
1607
|
|
|
*/ |
|
1608
|
|
|
public function getRemovedBindingIds(): array |
|
1609
|
|
|
{ |
|
1610
|
|
|
return $this->removedBindingIds; |
|
1611
|
|
|
} |
|
1612
|
|
|
|
|
1613
|
|
|
/** |
|
1614
|
|
|
* Removes bindings for a service. |
|
1615
|
|
|
* |
|
1616
|
|
|
* @internal |
|
1617
|
|
|
*/ |
|
1618
|
|
|
public function removeBindings(string $id): void |
|
1619
|
|
|
{ |
|
1620
|
|
|
if ($this->hasDefinition($id)) { |
|
1621
|
|
|
foreach ($this->getDefinition($id)->getBindings() as $key => $binding) { |
|
1622
|
|
|
[, $bindingId] = $binding->getValues(); |
|
1623
|
|
|
$this->removedBindingIds[(int) $bindingId] = true; |
|
1624
|
|
|
} |
|
1625
|
|
|
} |
|
1626
|
|
|
} |
|
1627
|
|
|
|
|
1628
|
|
|
/** |
|
1629
|
|
|
* @return string[] |
|
1630
|
|
|
* |
|
1631
|
|
|
* @internal |
|
1632
|
|
|
*/ |
|
1633
|
|
|
public static function getServiceConditionals(mixed $value): array |
|
1634
|
|
|
{ |
|
1635
|
|
|
$services = []; |
|
1636
|
|
|
|
|
1637
|
|
|
if (\is_array($value)) { |
|
1638
|
|
|
foreach ($value as $v) { |
|
1639
|
|
|
$services = array_unique(array_merge($services, self::getServiceConditionals($v))); |
|
1640
|
|
|
} |
|
1641
|
|
|
} elseif ($value instanceof Reference && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) { |
|
1642
|
|
|
$services[] = (string) $value; |
|
1643
|
|
|
} |
|
1644
|
|
|
|
|
1645
|
|
|
return $services; |
|
1646
|
|
|
} |
|
1647
|
|
|
|
|
1648
|
|
|
/** |
|
1649
|
|
|
* @return string[] |
|
1650
|
|
|
* |
|
1651
|
|
|
* @internal |
|
1652
|
|
|
*/ |
|
1653
|
|
|
public static function getInitializedConditionals(mixed $value): array |
|
1654
|
|
|
{ |
|
1655
|
|
|
$services = []; |
|
1656
|
|
|
|
|
1657
|
|
|
if (\is_array($value)) { |
|
1658
|
|
|
foreach ($value as $v) { |
|
1659
|
|
|
$services = array_unique(array_merge($services, self::getInitializedConditionals($v))); |
|
1660
|
|
|
} |
|
1661
|
|
|
} elseif ($value instanceof Reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()) { |
|
1662
|
|
|
$services[] = (string) $value; |
|
1663
|
|
|
} |
|
1664
|
|
|
|
|
1665
|
|
|
return $services; |
|
1666
|
|
|
} |
|
1667
|
|
|
|
|
1668
|
|
|
/** |
|
1669
|
|
|
* Computes a reasonably unique hash of a serializable value. |
|
1670
|
|
|
*/ |
|
1671
|
|
|
public static function hash(mixed $value): string |
|
1672
|
|
|
{ |
|
1673
|
|
|
$hash = substr(base64_encode(hash('xxh128', serialize($value), true)), 0, 7); |
|
1674
|
|
|
|
|
1675
|
|
|
return str_replace(['/', '+'], ['.', '_'], $hash); |
|
1676
|
|
|
} |
|
1677
|
|
|
|
|
1678
|
|
|
protected function getEnv(string $name): mixed |
|
1679
|
|
|
{ |
|
1680
|
|
|
$value = parent::getEnv($name); |
|
1681
|
|
|
$bag = $this->getParameterBag(); |
|
1682
|
|
|
|
|
1683
|
|
|
if (!\is_string($value) || !$bag instanceof EnvPlaceholderParameterBag) { |
|
1684
|
|
|
return $value; |
|
1685
|
|
|
} |
|
1686
|
|
|
|
|
1687
|
|
|
$envPlaceholders = $bag->getEnvPlaceholders(); |
|
1688
|
|
|
if (isset($envPlaceholders[$name][$value])) { |
|
1689
|
|
|
$bag = new ParameterBag($bag->all()); |
|
1690
|
|
|
|
|
1691
|
|
|
return $bag->unescapeValue($bag->get("env($name)")); |
|
1692
|
|
|
} |
|
1693
|
|
|
foreach ($envPlaceholders as $env => $placeholders) { |
|
1694
|
|
|
if (isset($placeholders[$value])) { |
|
1695
|
|
|
return $this->getEnv($env); |
|
1696
|
|
|
} |
|
1697
|
|
|
} |
|
1698
|
|
|
|
|
1699
|
|
|
$this->resolving["env($name)"] = true; |
|
1700
|
|
|
try { |
|
1701
|
|
|
return $bag->unescapeValue($this->resolveEnvPlaceholders($bag->escapeValue($value), true)); |
|
1702
|
|
|
} finally { |
|
1703
|
|
|
unset($this->resolving["env($name)"]); |
|
1704
|
|
|
} |
|
1705
|
|
|
} |
|
1706
|
|
|
|
|
1707
|
|
|
private function callMethod(object $service, array $call, array &$inlineServices): mixed |
|
1708
|
|
|
{ |
|
1709
|
|
|
foreach (self::getServiceConditionals($call[1]) as $s) { |
|
1710
|
|
|
if (!$this->has($s)) { |
|
1711
|
|
|
return $service; |
|
1712
|
|
|
} |
|
1713
|
|
|
} |
|
1714
|
|
|
foreach (self::getInitializedConditionals($call[1]) as $s) { |
|
1715
|
|
|
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE, $inlineServices)) { |
|
1716
|
|
|
return $service; |
|
1717
|
|
|
} |
|
1718
|
|
|
} |
|
1719
|
|
|
|
|
1720
|
|
|
$result = $service->{$call[0]}(...$this->doResolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1])), $inlineServices)); |
|
1721
|
|
|
|
|
1722
|
|
|
return empty($call[2]) ? $service : $result; |
|
1723
|
|
|
} |
|
1724
|
|
|
|
|
1725
|
|
|
private function shareService(Definition $definition, mixed $service, ?string $id, array &$inlineServices): void |
|
1726
|
|
|
{ |
|
1727
|
|
|
$inlineServices[$id ?? spl_object_hash($definition)] = $service; |
|
1728
|
|
|
|
|
1729
|
|
|
if (null !== $id && $definition->isShared()) { |
|
1730
|
|
|
if ($definition->isPrivate() && $this->isCompiled()) { |
|
1731
|
|
|
$this->privates[$id] = $service; |
|
1732
|
|
|
} else { |
|
1733
|
|
|
$this->services[$id] = $service; |
|
1734
|
|
|
} |
|
1735
|
|
|
unset($this->loading[$id]); |
|
1736
|
|
|
} |
|
1737
|
|
|
} |
|
1738
|
|
|
|
|
1739
|
|
|
private function getExpressionLanguage(): ExpressionLanguage |
|
1740
|
|
|
{ |
|
1741
|
|
|
if (!isset($this->expressionLanguage)) { |
|
1742
|
|
|
if (!class_exists(Expression::class)) { |
|
1743
|
|
|
throw new LogicException('Expressions cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'); |
|
1744
|
|
|
} |
|
1745
|
|
|
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders, null, $this->getEnv(...)); |
|
1746
|
|
|
} |
|
1747
|
|
|
|
|
1748
|
|
|
return $this->expressionLanguage; |
|
1749
|
|
|
} |
|
1750
|
|
|
|
|
1751
|
|
|
private function inVendors(string $path): bool |
|
1752
|
|
|
{ |
|
1753
|
|
|
$path = is_file($path) ? \dirname($path) : $path; |
|
1754
|
|
|
|
|
1755
|
|
|
if (isset($this->pathsInVendor[$path])) { |
|
1756
|
|
|
return $this->pathsInVendor[$path]; |
|
1757
|
|
|
} |
|
1758
|
|
|
|
|
1759
|
|
|
$this->vendors ??= (new ComposerResource())->getVendors(); |
|
1760
|
|
|
$path = realpath($path) ?: $path; |
|
1761
|
|
|
|
|
1762
|
|
|
if (isset($this->pathsInVendor[$path])) { |
|
1763
|
|
|
return $this->pathsInVendor[$path]; |
|
1764
|
|
|
} |
|
1765
|
|
|
|
|
1766
|
|
|
foreach ($this->vendors as $vendor) { |
|
1767
|
|
|
if (\in_array($path[\strlen($vendor)] ?? '', ['/', \DIRECTORY_SEPARATOR], true) && str_starts_with($path, $vendor)) { |
|
1768
|
|
|
$this->pathsInVendor[$vendor.\DIRECTORY_SEPARATOR.'composer'] = false; |
|
1769
|
|
|
$this->addResource(new FileResource($vendor.\DIRECTORY_SEPARATOR.'composer'.\DIRECTORY_SEPARATOR.'installed.json')); |
|
1770
|
|
|
$this->pathsInVendor[$vendor.\DIRECTORY_SEPARATOR.'composer'] = true; |
|
1771
|
|
|
|
|
1772
|
|
|
return $this->pathsInVendor[$path] = true; |
|
1773
|
|
|
} |
|
1774
|
|
|
} |
|
1775
|
|
|
|
|
1776
|
|
|
return $this->pathsInVendor[$path] = false; |
|
1777
|
|
|
} |
|
1778
|
|
|
} |
|
1779
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: