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