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