$rootNode
->children()
->arrayNode('form')
->info('form configuration')
->canBeUnset()
->treatNullLike(array('enabled' => true))
->treatTrueLike(array('enabled' => true))
->children()
->booleanNode('enabled')->defaultTrue()->end()
->end()
->end()
来自
Symfony\Bundle\FrameworkBundle\DependencyInjection\Configuration
的上述片段的第 5 行使用方法canBeUnset()
.我不知道这是做什么的,因为如果我删除它,它似乎没有任何作用。我正在理解我自己的包的语义配置。
最佳答案
按照代码,您可以在 Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition
中找到此方法的定义。类(class)。
/**
* Sets whether the node can be unset.
*
* @param Boolean $allow
*
* @return ArrayNodeDefinition
*/
public function canBeUnset($allow = true)
{
$this->merge()->allowUnset($allow);
return $this;
}
这被传递给处理配置合并的 MergeBuilder (
Symfony/Component/Config/Definition/Builder/MergeBuilder
)。/**
* Sets whether the node can be unset.
*
* @param Boolean $allow
*
* @return MergeBuilder
*/
public function allowUnset($allow = true)
{
$this->allowFalse = $allow;
return $this;
}
所以我的理解是,这个方法定义了,如果你的配置值可以在合并配置时取消设置,以防覆盖的配置不支持该值。不过,我必须进行测试,以找出不允许取消设置的行为,但我想它会抛出一个关于缺少配置值的异常,就像
isRequired
.
关于configuration - Symfony2 树生成器 - 方法 canBeUnset() 做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12168185/