Permalink
Browse files

bug #27472 [DI] Ignore missing tree root nodes on validate (ro0NL)

This PR was squashed before being merged into the 4.1 branch (closes #27472).

Discussion
----------

[DI] Ignore missing tree root nodes on validate

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | yes
| New feature?  | technically yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #27450
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

<!--
Write a short README entry for your feature/bugfix here (replace this comment block.)
This will help people understand your PR and can be used as a start of the Doc PR.
Additionally:
 - Bug fixes must be submitted against the lowest branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against the master branch.
-->

Commits
-------

b3cdfc6 [DI] Ignore missing tree root nodes on validate
  • Loading branch information...
fabpot committed Jun 5, 2018
2 parents 0ed3d0d + b3cdfc6 commit 8130f22f4aa4a138153aa002337d5d7c223730d8
@@ -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.');
}
}
}
@@ -0,0 +1,19 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <franssen.roland@gmail.com>
*/
class TreeWithoutRootNodeException extends \RuntimeException
{
}
@@ -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();
@@ -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,23 +279,41 @@ 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';
}
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()

0 comments on commit 8130f22

Please sign in to comment.