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

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