diff --git a/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php b/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php index 6da510ec1c2d..6b4323d42f51 100644 --- a/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php +++ b/src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Config\Definition\Builder; +use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException; use Symfony\Component\Config\Definition\NodeInterface; /** @@ -74,7 +75,7 @@ public function setPathSeparator(string $separator) private function assertTreeHasRootNode() { if (null === $this->root) { - throw new \RuntimeException('The configuration tree has no root node.'); + throw new TreeWithoutRootNodeException('The configuration tree has no root node.'); } } } diff --git a/src/Symfony/Component/Config/Definition/Exception/TreeWithoutRootNodeException.php b/src/Symfony/Component/Config/Definition/Exception/TreeWithoutRootNodeException.php new file mode 100644 index 000000000000..67b0c4bb2e08 --- /dev/null +++ b/src/Symfony/Component/Config/Definition/Exception/TreeWithoutRootNodeException.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Config\Definition\Exception; + +/** + * @author Roland Franssen + */ +class TreeWithoutRootNodeException extends \RuntimeException +{ +} diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php index 09c83ce3bf2e..58fc95b5fc55 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ValidateEnvPlaceholdersPass.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Compiler; use Symfony\Component\Config\Definition\BaseNode; +use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException; use Symfony\Component\Config\Definition\Processor; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface; @@ -75,7 +76,10 @@ public function process(ContainerBuilder $container) continue; } - $processor->processConfiguration($configuration, $config); + try { + $processor->processConfiguration($configuration, $config); + } catch (TreeWithoutRootNodeException $e) { + } } } finally { BaseNode::resetPlaceholders(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php index 9534b11a90bb..0fb2f6dc91a4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/ValidateEnvPlaceholdersPassTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; +use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException; use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass; use Symfony\Component\DependencyInjection\Compiler\RegisterEnvVarProcessorsPass; use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass; @@ -222,6 +223,17 @@ public function testEnvWithVariableNode(): void $this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig())); } + public function testConfigurationWithoutRootNode(): void + { + $container = new ContainerBuilder(); + $container->registerExtension($ext = new EnvExtension(new EnvConfigurationWithoutRootNode())); + $container->loadFromExtension('env_extension'); + + $this->doProcess($container); + + $this->addToAssertionCount(1); + } + private function doProcess(ContainerBuilder $container): void { (new MergeExtensionConfigurationPass())->process($container); @@ -267,10 +279,24 @@ public function getConfigTreeBuilder() } } +class EnvConfigurationWithoutRootNode implements ConfigurationInterface +{ + public function getConfigTreeBuilder() + { + return new TreeBuilder(); + } +} + class EnvExtension extends Extension { + private $configuration; private $config; + public function __construct(ConfigurationInterface $configuration = null) + { + $this->configuration = $configuration ?? new EnvConfiguration(); + } + public function getAlias() { return 'env_extension'; @@ -278,12 +304,16 @@ public function getAlias() public function getConfiguration(array $config, ContainerBuilder $container) { - return new EnvConfiguration(); + return $this->configuration; } public function load(array $configs, ContainerBuilder $container) { - $this->config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); + try { + $this->config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); + } catch (TreeWithoutRootNodeException $e) { + $this->config = null; + } } public function getConfig()