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

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\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.     /**
  29.      * {@inheritdoc}
  30.      */
  31.     public function process(ContainerBuilder $container)
  32.     {
  33.         $parameters $container->getParameterBag()->all();
  34.         $definitions $container->getDefinitions();
  35.         $aliases $container->getAliases();
  36.         $exprLangProviders $container->getExpressionLanguageProviders();
  37.         $configAvailable class_exists(BaseNode::class);
  38.         foreach ($container->getExtensions() as $extension) {
  39.             if ($extension instanceof PrependExtensionInterface) {
  40.                 $extension->prepend($container);
  41.             }
  42.         }
  43.         foreach ($container->getExtensions() as $name => $extension) {
  44.             if (!$config $container->getExtensionConfig($name)) {
  45.                 // this extension was not called
  46.                 continue;
  47.             }
  48.             $resolvingBag $container->getParameterBag();
  49.             if ($resolvingBag instanceof EnvPlaceholderParameterBag && $extension instanceof Extension) {
  50.                 // create a dedicated bag so that we can track env vars per-extension
  51.                 $resolvingBag = new MergeExtensionConfigurationParameterBag($resolvingBag);
  52.                 if ($configAvailable) {
  53.                     BaseNode::setPlaceholderUniquePrefix($resolvingBag->getEnvPlaceholderUniquePrefix());
  54.                 }
  55.             }
  56.             $config $resolvingBag->resolveValue($config);
  57.             try {
  58.                 $tmpContainer = new MergeExtensionConfigurationContainerBuilder($extension$resolvingBag);
  59.                 $tmpContainer->setResourceTracking($container->isTrackingResources());
  60.                 $tmpContainer->addObjectResource($extension);
  61.                 if ($extension instanceof ConfigurationExtensionInterface && null !== $configuration $extension->getConfiguration($config$tmpContainer)) {
  62.                     $tmpContainer->addObjectResource($configuration);
  63.                 }
  64.                 foreach ($exprLangProviders as $provider) {
  65.                     $tmpContainer->addExpressionLanguageProvider($provider);
  66.                 }
  67.                 $extension->load($config$tmpContainer);
  68.             } catch (\Exception $e) {
  69.                 if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
  70.                     $container->getParameterBag()->mergeEnvPlaceholders($resolvingBag);
  71.                 }
  72.                 throw $e;
  73.             }
  74.             if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
  75.                 // don't keep track of env vars that are *overridden* when configs are merged
  76.                 $resolvingBag->freezeAfterProcessing($extension$tmpContainer);
  77.             }
  78.             $container->merge($tmpContainer);
  79.             $container->getParameterBag()->add($parameters);
  80.         }
  81.         $container->addDefinitions($definitions);
  82.         $container->addAliases($aliases);
  83.     }
  84. }
  85. /**
  86.  * @internal
  87.  */
  88. class MergeExtensionConfigurationParameterBag extends EnvPlaceholderParameterBag
  89. {
  90.     private $processedEnvPlaceholders;
  91.     public function __construct(parent $parameterBag)
  92.     {
  93.         parent::__construct($parameterBag->all());
  94.         $this->mergeEnvPlaceholders($parameterBag);
  95.     }
  96.     public function freezeAfterProcessing(Extension $extensionContainerBuilder $container)
  97.     {
  98.         if (!$config $extension->getProcessedConfigs()) {
  99.             // Extension::processConfiguration() wasn't called, we cannot know how configs were merged
  100.             return;
  101.         }
  102.         $this->processedEnvPlaceholders = [];
  103.         // serialize config and container to catch env vars nested in object graphs
  104.         $config serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all());
  105.         foreach (parent::getEnvPlaceholders() as $env => $placeholders) {
  106.             foreach ($placeholders as $placeholder) {
  107.                 if (false !== stripos($config$placeholder)) {
  108.                     $this->processedEnvPlaceholders[$env] = $placeholders;
  109.                     break;
  110.                 }
  111.             }
  112.         }
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public function getEnvPlaceholders(): array
  118.     {
  119.         return $this->processedEnvPlaceholders ?? parent::getEnvPlaceholders();
  120.     }
  121.     public function getUnusedEnvPlaceholders(): array
  122.     {
  123.         return null === $this->processedEnvPlaceholders ? [] : array_diff_key(parent::getEnvPlaceholders(), $this->processedEnvPlaceholders);
  124.     }
  125. }
  126. /**
  127.  * A container builder preventing using methods that wouldn't have any effect from extensions.
  128.  *
  129.  * @internal
  130.  */
  131. class MergeExtensionConfigurationContainerBuilder extends ContainerBuilder
  132. {
  133.     private $extensionClass;
  134.     public function __construct(ExtensionInterface $extensionParameterBagInterface $parameterBag null)
  135.     {
  136.         parent::__construct($parameterBag);
  137.         $this->extensionClass \get_class($extension);
  138.     }
  139.     /**
  140.      * {@inheritdoc}
  141.      */
  142.     public function addCompilerPass(CompilerPassInterface $passstring $type PassConfig::TYPE_BEFORE_OPTIMIZATIONint $priority 0): self
  143.     {
  144.         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));
  145.     }
  146.     /**
  147.      * {@inheritdoc}
  148.      */
  149.     public function registerExtension(ExtensionInterface $extension)
  150.     {
  151.         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));
  152.     }
  153.     /**
  154.      * {@inheritdoc}
  155.      */
  156.     public function compile(bool $resolveEnvPlaceholders false)
  157.     {
  158.         throw new LogicException(sprintf('Cannot compile the container in extension "%s".'$this->extensionClass));
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function resolveEnvPlaceholders($value$format null, array &$usedEnvs null)
  164.     {
  165.         if (true !== $format || !\is_string($value)) {
  166.             return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  167.         }
  168.         $bag $this->getParameterBag();
  169.         $value $bag->resolveValue($value);
  170.         if (!$bag instanceof EnvPlaceholderParameterBag) {
  171.             return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  172.         }
  173.         foreach ($bag->getEnvPlaceholders() as $env => $placeholders) {
  174.             if (!str_contains($env':')) {
  175.                 continue;
  176.             }
  177.             foreach ($placeholders as $placeholder) {
  178.                 if (false !== stripos($value$placeholder)) {
  179.                     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));
  180.                 }
  181.             }
  182.         }
  183.         return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  184.     }
  185. }