Symfony 配置:数组节点或 null
Symfony Config: Array node or null
我的捆绑包的某些功能是可选的。为了配置它们,我想有以下两个选项。如果我想启用该功能:
foo:
feature:
mandatory1: 'bar'
mandatory2: 'bar2'
但是,如果我决定不使用此特定功能,我想将 feature
设置为空:
foo:
feature: ~
我目前有以下代码:
// ...
->arrayNode('feature')
->defaultNull()
->children()
->scalarNode('mandatory1')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('mandatory2')
->isRequired()
->cannotBeEmpty()
->end()
->end()
->end()
但是,defaultNull()
调用是不允许的。有什么办法可以实现这种行为吗?
其实我自己找到了答案。复制自 Symfony docs:
If you have entire sections which are optional and can be enabled/disabled, you can take advantage of the shortcut canBeEnabled()
and canBeDisabled()
methods.
我的捆绑包的某些功能是可选的。为了配置它们,我想有以下两个选项。如果我想启用该功能:
foo:
feature:
mandatory1: 'bar'
mandatory2: 'bar2'
但是,如果我决定不使用此特定功能,我想将 feature
设置为空:
foo:
feature: ~
我目前有以下代码:
// ...
->arrayNode('feature')
->defaultNull()
->children()
->scalarNode('mandatory1')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('mandatory2')
->isRequired()
->cannotBeEmpty()
->end()
->end()
->end()
但是,defaultNull()
调用是不允许的。有什么办法可以实现这种行为吗?
其实我自己找到了答案。复制自 Symfony docs:
If you have entire sections which are optional and can be enabled/disabled, you can take advantage of the shortcut
canBeEnabled()
andcanBeDisabled()
methods.