很抱歉我找不到在 "symfony/config": "2.6.4" ConfigurationInterface 中添加默认值的方法!
所需的配置应该是这样的:
当然,我阅读了这个问题: 使用Symfony2配置类,如何定义一个子节点没有键的数组节点? 正如您所读到的,我的当前代码实现方式不起作用,并会抛出以下错误:
所需的配置应该是这样的:
X:
Y:
- test
- testing
默认为:
X:
Y:
- test
所谓 "默认" 是指:如果读取配置文件中未设置 Y 配置分支,则 $processor->processConfiguration 应将其添加(实际上也是这样的!如果我删除 ->prototype...)
以下是我的代码:
class Definition implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root("X");
$rootNode
->children()
->arrayNode("Y")
->addDefaultsIfNotSet()
->info("Multiple values can be used")
->cannotBeEmpty()
->addDefaultIfNotSet()
->defaultValue(array("test"))
->prototype("scalar")
->validate()
->ifNotInArray(array("test", "testing"))
->thenInvalid("Invalid value %s")
->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}
当然,我阅读了这个问题: 使用Symfony2配置类,如何定义一个子节点没有键的数组节点? 正如您所读到的,我的当前代码实现方式不起作用,并会抛出以下错误:
[Symfony\Component\Config\Definition\Exception\InvalidDefinitionException]
->addDefaultsIfNotSet() is not applicable to prototype nodes at path "X.Y"