Skip to content

Commit 8839f62

Browse files
committed
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 ------- b3cdfc64b5 [DI] Ignore missing tree root nodes on validate
2 parents ed37583 + bf577c9 commit 8839f62

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

Compiler/ValidateEnvPlaceholdersPass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\Config\Definition\BaseNode;
15+
use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException;
1516
use Symfony\Component\Config\Definition\Processor;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
@@ -75,7 +76,10 @@ public function process(ContainerBuilder $container)
7576
continue;
7677
}
7778

78-
$processor->processConfiguration($configuration, $config);
79+
try {
80+
$processor->processConfiguration($configuration, $config);
81+
} catch (TreeWithoutRootNodeException $e) {
82+
}
7983
}
8084
} finally {
8185
BaseNode::resetPlaceholders();

Tests/Compiler/ValidateEnvPlaceholdersPassTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1616
use Symfony\Component\Config\Definition\ConfigurationInterface;
17+
use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException;
1718
use Symfony\Component\DependencyInjection\Compiler\MergeExtensionConfigurationPass;
1819
use Symfony\Component\DependencyInjection\Compiler\RegisterEnvVarProcessorsPass;
1920
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
@@ -222,6 +223,17 @@ public function testEnvWithVariableNode(): void
222223
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
223224
}
224225

226+
public function testConfigurationWithoutRootNode(): void
227+
{
228+
$container = new ContainerBuilder();
229+
$container->registerExtension($ext = new EnvExtension(new EnvConfigurationWithoutRootNode()));
230+
$container->loadFromExtension('env_extension');
231+
232+
$this->doProcess($container);
233+
234+
$this->addToAssertionCount(1);
235+
}
236+
225237
private function doProcess(ContainerBuilder $container): void
226238
{
227239
(new MergeExtensionConfigurationPass())->process($container);
@@ -267,23 +279,41 @@ public function getConfigTreeBuilder()
267279
}
268280
}
269281

282+
class EnvConfigurationWithoutRootNode implements ConfigurationInterface
283+
{
284+
public function getConfigTreeBuilder()
285+
{
286+
return new TreeBuilder();
287+
}
288+
}
289+
270290
class EnvExtension extends Extension
271291
{
292+
private $configuration;
272293
private $config;
273294

295+
public function __construct(ConfigurationInterface $configuration = null)
296+
{
297+
$this->configuration = $configuration ?? new EnvConfiguration();
298+
}
299+
274300
public function getAlias()
275301
{
276302
return 'env_extension';
277303
}
278304

279305
public function getConfiguration(array $config, ContainerBuilder $container)
280306
{
281-
return new EnvConfiguration();
307+
return $this->configuration;
282308
}
283309

284310
public function load(array $configs, ContainerBuilder $container)
285311
{
286-
$this->config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
312+
try {
313+
$this->config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
314+
} catch (TreeWithoutRootNodeException $e) {
315+
$this->config = null;
316+
}
287317
}
288318

289319
public function getConfig()

0 commit comments

Comments
 (0)