Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3cdfc6

Browse files
ro0NLfabpot
authored andcommitted
[DI] Ignore missing tree root nodes on validate
1 parent ca5e561 commit b3cdfc6

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

src/Symfony/Component/Config/Definition/Builder/TreeBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Config\Definition\Builder;
1313

14+
use Symfony\Component\Config\Definition\Exception\TreeWithoutRootNodeException;
1415
use Symfony\Component\Config\Definition\NodeInterface;
1516

1617
/**
@@ -74,7 +75,7 @@ public function setPathSeparator(string $separator)
7475
private function assertTreeHasRootNode()
7576
{
7677
if (null === $this->root) {
77-
throw new \RuntimeException('The configuration tree has no root node.');
78+
throw new TreeWithoutRootNodeException('The configuration tree has no root node.');
7879
}
7980
}
8081
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Definition\Exception;
13+
14+
/**
15+
* @author Roland Franssen <[email protected]>
16+
*/
17+
class TreeWithoutRootNodeException extends \RuntimeException
18+
{
19+
}

src/Symfony/Component/DependencyInjection/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\Exception\LogicException;
@@ -77,7 +78,10 @@ public function process(ContainerBuilder $container)
7778
continue;
7879
}
7980

80-
$processor->processConfiguration($configuration, $config);
81+
try {
82+
$processor->processConfiguration($configuration, $config);
83+
} catch (TreeWithoutRootNodeException $e) {
84+
}
8185
}
8286
} finally {
8387
BaseNode::resetPlaceholders();

src/Symfony/Component/DependencyInjection/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()

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3333
},
3434
"conflict": {
35-
"symfony/config": "<4.1",
35+
"symfony/config": "<4.1.1",
3636
"symfony/finder": "<3.4",
3737
"symfony/proxy-manager-bridge": "<3.4",
3838
"symfony/yaml": "<3.4"

0 commit comments

Comments
 (0)