bug #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 | #25760
| License       | MIT
| Doc PR        |

Commits
-------

52dd825bf1 allow null values for root nodes in YAML configs
This commit is contained in:
Fabien Potencier 2018-01-23 07:36:50 +01:00
commit 73cbb01953
5 changed files with 26 additions and 3 deletions

View File

@ -271,12 +271,16 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* @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;

View File

@ -425,7 +425,7 @@ class YamlFileLoader extends FileLoader
continue;
}
if (!is_array($values)) {
if (!is_array($values) && null !== $values) {
$values = array();
}

View File

@ -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');

View File

@ -207,6 +207,17 @@ class YamlFileLoaderTest extends TestCase
}
}
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());