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

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.                 if ($configAvailable) {
  73.                     BaseNode::resetPlaceholders();
  74.                 }
  75.                 throw $e;
  76.             }
  77.             if ($resolvingBag instanceof MergeExtensionConfigurationParameterBag) {
  78.                 // don't keep track of env vars that are *overridden* when configs are merged
  79.                 $resolvingBag->freezeAfterProcessing($extension$tmpContainer);
  80.             }
  81.             $container->merge($tmpContainer);
  82.             $container->getParameterBag()->add($parameters);
  83.         }
  84.         if ($configAvailable) {
  85.             BaseNode::resetPlaceholders();
  86.         }
  87.         $container->addDefinitions($definitions);
  88.         $container->addAliases($aliases);
  89.     }
  90. }
  91. /**
  92.  * @internal
  93.  */
  94. class MergeExtensionConfigurationParameterBag extends EnvPlaceholderParameterBag
  95. {
  96.     private $processedEnvPlaceholders;
  97.     public function __construct(parent $parameterBag)
  98.     {
  99.         parent::__construct($parameterBag->all());
  100.         $this->mergeEnvPlaceholders($parameterBag);
  101.     }
  102.     public function freezeAfterProcessing(Extension $extensionContainerBuilder $container)
  103.     {
  104.         if (!$config $extension->getProcessedConfigs()) {
  105.             // Extension::processConfiguration() wasn't called, we cannot know how configs were merged
  106.             return;
  107.         }
  108.         $this->processedEnvPlaceholders = [];
  109.         // serialize config and container to catch env vars nested in object graphs
  110.         $config serialize($config).serialize($container->getDefinitions()).serialize($container->getAliases()).serialize($container->getParameterBag()->all());
  111.         foreach (parent::getEnvPlaceholders() as $env => $placeholders) {
  112.             foreach ($placeholders as $placeholder) {
  113.                 if (false !== stripos($config$placeholder)) {
  114.                     $this->processedEnvPlaceholders[$env] = $placeholders;
  115.                     break;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function getEnvPlaceholders(): array
  124.     {
  125.         return $this->processedEnvPlaceholders ?? parent::getEnvPlaceholders();
  126.     }
  127.     public function getUnusedEnvPlaceholders(): array
  128.     {
  129.         return null === $this->processedEnvPlaceholders ? [] : array_diff_key(parent::getEnvPlaceholders(), $this->processedEnvPlaceholders);
  130.     }
  131. }
  132. /**
  133.  * A container builder preventing using methods that wouldn't have any effect from extensions.
  134.  *
  135.  * @internal
  136.  */
  137. class MergeExtensionConfigurationContainerBuilder extends ContainerBuilder
  138. {
  139.     private $extensionClass;
  140.     public function __construct(ExtensionInterface $extensionParameterBagInterface $parameterBag null)
  141.     {
  142.         parent::__construct($parameterBag);
  143.         $this->extensionClass \get_class($extension);
  144.     }
  145.     /**
  146.      * {@inheritdoc}
  147.      */
  148.     public function addCompilerPass(CompilerPassInterface $passstring $type PassConfig::TYPE_BEFORE_OPTIMIZATIONint $priority 0): self
  149.     {
  150.         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));
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public function registerExtension(ExtensionInterface $extension)
  156.     {
  157.         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));
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public function compile(bool $resolveEnvPlaceholders false)
  163.     {
  164.         throw new LogicException(sprintf('Cannot compile the container in extension "%s".'$this->extensionClass));
  165.     }
  166.     /**
  167.      * {@inheritdoc}
  168.      */
  169.     public function resolveEnvPlaceholders($value$format null, array &$usedEnvs null)
  170.     {
  171.         if (true !== $format || !\is_string($value)) {
  172.             return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  173.         }
  174.         $bag $this->getParameterBag();
  175.         $value $bag->resolveValue($value);
  176.         if (!$bag instanceof EnvPlaceholderParameterBag) {
  177.             return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  178.         }
  179.         foreach ($bag->getEnvPlaceholders() as $env => $placeholders) {
  180.             if (!str_contains($env':')) {
  181.                 continue;
  182.             }
  183.             foreach ($placeholders as $placeholder) {
  184.                 if (false !== stripos($value$placeholder)) {
  185.                     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));
  186.                 }
  187.             }
  188.         }
  189.         return parent::resolveEnvPlaceholders($value$format$usedEnvs);
  190.     }
  191. }