allow null values for root nodes in YAML configs

This commit is contained in:
Christian Flothmann 2018-01-22 21:35:04 +01:00
parent 34b6bdc8d0
commit 52dd825bf1
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());