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

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