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