Skip to content

Commit 73cbb01

Browse files
committed
bug symfony#25891 [DependencyInjection] allow null values for root nodes in YAML configs (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [DependencyInjection] allow null values for root nodes in YAML configs | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#25760 | License | MIT | Doc PR | Commits ------- 52dd825 allow null values for root nodes in YAML configs
2 parents 34b6bdc + 52dd825 commit 73cbb01

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,16 @@ public function addClassResource(\ReflectionClass $class)
271271
* @throws BadMethodCallException When this ContainerBuilder is frozen
272272
* @throws \LogicException if the container is frozen
273273
*/
274-
public function loadFromExtension($extension, array $values = array())
274+
public function loadFromExtension($extension, array $values = null)
275275
{
276276
if ($this->isFrozen()) {
277277
throw new BadMethodCallException('Cannot load from an extension on a frozen container.');
278278
}
279279

280+
if (func_num_args() < 2) {
281+
$values = array();
282+
}
283+
280284
$namespace = $this->getExtension($extension)->getAlias();
281285

282286
$this->extensionConfigs[$namespace][] = $values;

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ private function loadFromExtensions(array $content)
425425
continue;
426426
}
427427

428-
if (!is_array($values)) {
428+
if (!is_array($values) && null !== $values) {
429429
$values = array();
430430
}
431431

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/ProjectExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ class ProjectExtension implements ExtensionInterface
88
{
99
public function load(array $configs, ContainerBuilder $configuration)
1010
{
11-
$config = call_user_func_array('array_merge', $configs);
11+
$configuration->setParameter('project.configs', $configs);
12+
$configs = array_filter($configs);
13+
14+
if ($configs) {
15+
$config = call_user_func_array('array_merge', $configs);
16+
} else {
17+
$config = array();
18+
}
1219

1320
$configuration->setDefinition('project.service.bar', new Definition('FooClass'));
1421
$configuration->setParameter('project.parameter.bar', isset($config['foo']) ? $config['foo'] : 'foobar');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
project: ~

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ public function testExtensions()
207207
}
208208
}
209209

210+
public function testExtensionWithNullConfig()
211+
{
212+
$container = new ContainerBuilder();
213+
$container->registerExtension(new \ProjectExtension());
214+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
215+
$loader->load('null_config.yml');
216+
$container->compile();
217+
218+
$this->assertSame(array(null), $container->getParameter('project.configs'));
219+
}
220+
210221
public function testSupports()
211222
{
212223
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());

0 commit comments

Comments
 (0)