configuration - Symfony2 树生成器 - 方法 canBeUnset() 做什么?

标签 configuration symfony

$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/

相关文章:

c# - .NET ConfigurationManager app.config 困惑

configuration - 使用 env 变量配置 Jetty 应用程序

php - 将 Excel 流附加到 swiftmailer 消息中?

php - Symfony Ajax 进度条

Laravel - 根据环境进行设置

qt - 在 QT 中将配置设置保存到 XML 文件?

php - Symfony2 表单事件和模型转换器

mongodb - 教义ODM : Embedding multiple GridFS documents exception

hadoop - Flume:HDFSEventSink - 如何动态多路复用?

symfony - gedmo 学说可翻译扩展。检索所有翻译或特定翻译(不同于当前语言环境)