vendor/symfony/dependency-injection/ContainerBuilder.php line 1511

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