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