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

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