vendor/symfony/dependency-injection/Compiler/MergeExtensionConfigurationPass.php line 73

  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\Compiler;
  11. use Symfony\Component\Config\Definition\BaseNode;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\LogicException;
  14. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  15. use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
  16. use Symfony\Component\DependencyInjection\Extension\Extension;
  17. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  18. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  19. use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
  20. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  21. /**
  22.  * Merges extension configs into the container builder.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  */
  26. class MergeExtensionConfigurationPass implements CompilerPassInterface
  27. {
  28.     public function process(ContainerBuilder $container)
  29.     {
  30.         $parameters $container->getParameterBag()->all();
  31.         $definitions $container->getDefinitions();
  32.         $aliases $container->getAliases();
  33.         $exprLangProviders $container->getExpressionLanguageProviders();
  34.         $configAvailable class_exists(BaseNode::class);
  35.         foreach ($container->getExtensions() as $extension) {
  36.             if ($extension instanceof PrependExtensionInterface) {
  37.                 $extension->prepend($container);
  38.             }
  39.         }
  40.         foreach ($container->getExtensions() as $name => $extension) {
  41.             if (!$config $container->getExtensionConfig($name)) {
  42.                 // this extension was not called
  43.                 continue;
  44.             }
  45.             $resolvingBag $container->getParameterBag();
  46.             if ($resolvingBag instanceof EnvPlaceholderParameterBag && $extension instanceof Extension) {
  47.                 // create a dedicated bag so that we can track env vars per-extension
  48.                 $resolvingBag = new MergeExtensionConfigurationParameterBag($resolvingBag);
  49.                 if ($configAvailable) {
  50.                     BaseNode::setPlaceholderUniquePrefix($resolvingBag->getEnvPlaceholderUniquePrefix());
  51.                 }
  52.             }
  53.             $config $resolvingBag->resolveValue($config);
  54.             try {
  55.                 $tmpContainer = new MergeExtensionConfigurationContainerBuilder($extension$resolvingBag);
  56.                 $tmpContainer->setResourceTracking($container->isTrackingResources());
  57.                 $tmpContainer->addObjectResource($extension);
  58.                 if ($extension instanceof ConfigurationExtensionInterface && null !== $configuration $extension->getConfiguration($config$tmpContainer)) {
  59.                     $tmpContainer->addObjectResource($configuration);
  60.                 }
  61.                 foreach ($exprLangProviders as $provider) {
  62.                     $tmpContainer->addExpressionLanguageProvider($provider);
  63.                 }
  64.                 $extension->load($config$tmpContainer);
  65.             } catch (\Exception $e) {
  66.                 if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
  67.                     $container->getParameterBag()->mergeEnvPlaceholders($resolvingBag);
  68.                 }
  69.                 throw $e;
  70.             }
  71.             if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
  72.                 // don't keep track of env vars that are *overridden* when configs are merged
  73.                 $resolvingBag->freezeAfterProcessing($extension$tmpContainer);
  74.             }
  75.             $container->merge($tmpContainer);
  76.             $container->getParameterBag()->add($parameters);
  77.         }
  78.         $container->addDefinitions($definitions);
  79.         $container->addAliases($aliases);
  80.     }
  81. }
  82. /**
  83.  * @internal
  84.  */
  85. class MergeExtensionConfigurationParameterBag extends EnvPlaceholderParameterBag
  86. {
  87.     private array $processedEnvPlaceholders;
  88.     public function __construct(parent $parameterBag)
  89.     {
  90.         parent::__construct($parameterBag->all());
  91.         $this->mergeEnvPlaceholders($parameterBag);
  92.     }
  93.     public function freezeAfterProcessing(Extension $extensionContainerBuilder $container)
  94.     {
  95.         if (!$config $extension->getProcessedConfigs()) {
  96.             // Extension::processConfiguration() wasn't called, we cannot know how configs were merged
  97.             return;
  98.         }
  99.         $this->processedEnvPlaceholders = [];
  100.         // serialize config and container to catch env vars nested in object graphs
  101.         $config serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all());
  102.         if (false === stripos($config'env_')) {
  103.             return;
  104.         }
  105.         preg_match_all('/env_[a-f0-9]{16}_\w+_[a-f0-9]{32}/Ui'$config$matches);
  106.         $usedPlaceholders array_flip($matches[0]);
  107.         foreach (parent::getEnvPlaceholders() as $env => $placeholders) {
  108.             foreach ($placeholders as $placeholder) {
  109.                 if (isset($usedPlaceholders[$placeholder])) {
  110.                     $this->processedEnvPlaceholders[$env] = $placeholders;
  111.                     break;
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     public function getEnvPlaceholders(): array
  117.     {
  118.         return $this->processedEnvPlaceholders ?? parent::getEnvPlaceholders();
  119.     }
  120.     public function getUnusedEnvPlaceholders(): array
  121.     {
  122.         return !isset($this->processedEnvPlaceholders) ? [] : array_diff_key(parent::getEnvPlaceholders(), $this->processedEnvPlaceholders);
  123.     }
  124. }
  125. /**
  126.  * A container builder preventing using methods that wouldn't have any effect from extensions.
  127.  *
  128.  * @internal
  129.  */
  130. class MergeExtensionConfigurationContainerBuilder extends ContainerBuilder
  131. {
  132.     private string $extensionClass;
  133.     public function __construct(ExtensionInterface $extensionParameterBagInterface $parameterBag null)
  134.     {
  135.         parent::__construct($parameterBag);
  136.         $this->extensionClass $extension::class;
  137.     }
  138.     public function addCompilerPass(CompilerPassInterface $passstring $type PassConfig::TYPE_BEFORE_OPTIMIZATIONint $priority 0): static
  139.     {
  140.         throw new LogicException(sprintf('You cannot add compiler pass "%s" from extension "%s". Compiler passes must be registered before the container is compiled.'get_debug_type($pass), $this->extensionClass));
  141.     }
  142.     public function registerExtension(ExtensionInterface $extension)
  143.     {
  144.         throw new LogicException(sprintf('You cannot register extension "%s" from "%s". Extensions must be registered before the container is compiled.'get_debug_type($extension), $this->extensionClass));
  145.     }
  146.     public function compile(bool $resolveEnvPlaceholders false)
  147.     {
  148.         throw new LogicException(sprintf('Cannot compile the container in extension "%s".'$this->extensionClass));
  149.     }
  150.     public function resolveEnvPlaceholders(mixed $valuestring|bool $format null, array &$usedEnvs null): mixed
  151.     {
  152.         if (true !== $format || !\is_string($value)) {
  153.             return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  154.         }
  155.         $bag $this->getParameterBag();
  156.         $value $bag->resolveValue($value);
  157.         if (!$bag instanceof EnvPlaceholderParameterBag) {
  158.             return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  159.         }
  160.         foreach ($bag->getEnvPlaceholders() as $env => $placeholders) {
  161.             if (!str_contains($env':')) {
  162.                 continue;
  163.             }
  164.             foreach ($placeholders as $placeholder) {
  165.                 if (false !== stripos($value$placeholder)) {
  166.                     throw new RuntimeException(sprintf('Using a cast in "env(%s)" is incompatible with resolution at compile time in "%s". The logic in the extension should be moved to a compiler pass, or an env parameter with no cast should be used instead.'$env$this->extensionClass));
  167.                 }
  168.             }
  169.         }
  170.         return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  171.     }
  172. }