Skip to content

Commit

Permalink
bug #25891 [DependencyInjection] allow null values for root nodes in …
Browse files Browse the repository at this point in the history
…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 | #25760
| License       | MIT
| Doc PR        |

Commits
-------

52dd825 allow null values for root nodes in YAML configs
  • Loading branch information
fabpot committed Jan 23, 2018
2 parents 34b6bdc + 52dd825 commit 73cbb01
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
Expand Up @@ -271,12 +271,16 @@ public function addClassResource(\ReflectionClass $class)
* @throws BadMethodCallException When this ContainerBuilder is frozen
* @throws \LogicException if the container is frozen
*/
public function loadFromExtension($extension, array $values = array())
public function loadFromExtension($extension, array $values = null)
{
if ($this->isFrozen()) {
throw new BadMethodCallException('Cannot load from an extension on a frozen container.');
}

if (func_num_args() < 2) {
$values = array();
}

$namespace = $this->getExtension($extension)->getAlias();

$this->extensionConfigs[$namespace][] = $values;
Expand Down
Expand Up @@ -425,7 +425,7 @@ private function loadFromExtensions(array $content)
continue;
}

if (!is_array($values)) {
if (!is_array($values) && null !== $values) {
$values = array();
}

Expand Down
Expand Up @@ -8,7 +8,14 @@ class ProjectExtension implements ExtensionInterface
{
public function load(array $configs, ContainerBuilder $configuration)
{
$config = call_user_func_array('array_merge', $configs);
$configuration->setParameter('project.configs', $configs);
$configs = array_filter($configs);

if ($configs) {
$config = call_user_func_array('array_merge', $configs);
} else {
$config = array();
}

$configuration->setDefinition('project.service.bar', new Definition('FooClass'));
$configuration->setParameter('project.parameter.bar', isset($config['foo']) ? $config['foo'] : 'foobar');
Expand Down
@@ -0,0 +1 @@
project: ~
Expand Up @@ -207,6 +207,17 @@ public function testExtensions()
}
}

public function testExtensionWithNullConfig()
{
$container = new ContainerBuilder();
$container->registerExtension(new \ProjectExtension());
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('null_config.yml');
$container->compile();

$this->assertSame(array(null), $container->getParameter('project.configs'));
}

public function testSupports()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());
Expand Down

0 comments on commit 73cbb01

Please sign in to comment.