【问题标题】:symfony/config arrayNode prototype addDefaultsIfNotSetsymfony/config arrayNode 原型 addDefaultsIfNotSet
【发布时间】:2015-03-13 07:44:52
【问题描述】:

很抱歉,我找不到将默认值添加到“symfony/config”的方法:“2.6.4”ConfigurationInterface!

我们想要的是这种类型的配置:

X:
    Y:
        - test
        - testing

默认为:

X:
    Y:
        - test

“默认”是指:如果未在读取的配置文件上设置 Y config 分支,则 $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;
    }
}

当然,我读过这个问题Using the Symfony2 configuration class, how do I define an array node whose children don't have keys?

我当前的代码实现了你可以阅读的这种方式,但它不起作用,我的代码抛出:

[Symfony\Component\Config\Definition\Exception\InvalidDefinitionException]
  ->addDefaultsIfNotSet() is not applicable to prototype nodes at path "X.Y"

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    作为参考,我终于通过一个 variableNode 得到它,就在我用头撞墙前一分钟;)

    class Definition implements ConfigurationInterface
    {
        /**
         * {@inheritdoc}
         */
        public function getConfigTreeBuilder()
        {
            $treeBuilder = new TreeBuilder();
            $rootNode = $treeBuilder->root("X");
            $rootNode
                ->children()
                    ->variableNode("Y")
                        ->info("Multiple values can be used")
                        ->cannotBeEmpty()
                        ->defaultValue(array("test"))
                        ->validate()
                            ->always(function ($values) {
                                foreach ((array) $values as $value) {
                                    if (! in_array($value, array("test", "testing"))) {
                                        throw new \Symfony\Component\Config\Definition\Exception\InvalidTypeException("Invalid value ".$value);
                                    }
                                }
                                return (array) $values;
                            })
                        ->end()
                    ->end()
                ->end()
            ;
    
            return $treeBuilder;
        }
    }
    

    似乎没有办法用 arrayNode 做到这一点!但如果有人找到方法,请毫不犹豫地回答,我很乐意使用 arrayNode 接受答案,因为集成验证可能比我的更好......

    【讨论】:

      【解决方案2】:

      怎么样:

      <?php
          $rootNode
              ->children()
                  ->arrayNode("Y")
                      ->defaultValue(array("test"))
                      ->prototype("scalar")
                          ->validate()
                          ->ifNotInArray(array("test", "testing"))
                              ->thenInvalid("Invalid value %s")
                          ->end()
                      ->end()
                  ->end()
              ->end()
          ;
      

      另一个问题是,为什么不重构配置。您需要testtest, testingtesting 的列表。你为什么不像这样配置它:

      X:
        Y:
          test: true
          testing: false
      

      默认使用X.Y.test = trueX.Y.testing = false?那么你的Configuration 就很简单了。

      【讨论】:

        【解决方案3】:

        你可以这样做:

        $node
            ->fixXmlConfig('driver')
            ->children()
                ->arrayNode('drivers')
                    ->scalarPrototype()->end()
                ->end()
            ->end()
        ;
        

        这允许:

        drivers: ['mysql', 'sqlite']
        

        更多信息在这里 https://symfony.com/doc/current/components/config/definition.html#array-node-options

        【讨论】:

          猜你喜欢
          • 2022-01-17
          • 2012-03-14
          • 1970-01-01
          • 1970-01-01
          • 2020-09-16
          • 1970-01-01
          • 2021-10-08
          • 2017-08-20
          • 1970-01-01
          相关资源
          最近更新 更多