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

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