|
12 | 12 | namespace Symfony\Component\DependencyInjection\Compiler;
|
13 | 13 |
|
14 | 14 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
| 15 | +use Symfony\Component\DependencyInjection\Exception\LogicException; |
15 | 16 | use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
|
16 | 17 | use Symfony\Component\DependencyInjection\Extension\Extension;
|
| 18 | +use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
17 | 19 | use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
|
18 | 20 | use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
|
| 21 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
19 | 22 |
|
20 | 23 | /**
|
21 | 24 | * Merges extension configs into the container builder.
|
@@ -52,7 +55,7 @@ public function process(ContainerBuilder $container)
|
52 | 55 | }
|
53 | 56 | $config = $resolvingBag->resolveValue($config);
|
54 | 57 |
|
55 |
| - $tmpContainer = new ContainerBuilder($resolvingBag); |
| 58 | + $tmpContainer = new MergeExtensionConfigurationContainerBuilder($extension, $resolvingBag); |
56 | 59 | $tmpContainer->setResourceTracking($container->isTrackingResources());
|
57 | 60 | $tmpContainer->addObjectResource($extension);
|
58 | 61 | if ($extension instanceof ConfigurationExtensionInterface && null !== $configuration = $extension->getConfiguration($config, $tmpContainer)) {
|
@@ -121,3 +124,44 @@ public function getEnvPlaceholders()
|
121 | 124 | return null !== $this->processedEnvPlaceholders ? $this->processedEnvPlaceholders : parent::getEnvPlaceholders();
|
122 | 125 | }
|
123 | 126 | }
|
| 127 | + |
| 128 | +/** |
| 129 | + * A container builder preventing using methods that wouldn't have any effect from extensions. |
| 130 | + * |
| 131 | + * @internal |
| 132 | + */ |
| 133 | +class MergeExtensionConfigurationContainerBuilder extends ContainerBuilder |
| 134 | +{ |
| 135 | + private $extensionClass; |
| 136 | + |
| 137 | + public function __construct(ExtensionInterface $extension, ParameterBagInterface $parameterBag = null) |
| 138 | + { |
| 139 | + parent::__construct($parameterBag); |
| 140 | + |
| 141 | + $this->extensionClass = get_class($extension); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * {@inheritdoc} |
| 146 | + */ |
| 147 | + public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/) |
| 148 | + { |
| 149 | + 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)); |
| 150 | + } |
| 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_class($extension), $this->extensionClass)); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * {@inheritdoc} |
| 162 | + */ |
| 163 | + public function compile($resolveEnvPlaceholders = false) |
| 164 | + { |
| 165 | + throw new LogicException(sprintf('Cannot compile the container in extension "%s".', $this->extensionClass)); |
| 166 | + } |
| 167 | +} |
0 commit comments