douwei9973 2014-08-06 18:19
浏览 20
已采纳

如何使用bot数组和标量原型创建配置

I have the following configuration:

my_project:
    options:
        key1: value1
        key2: value2
        key3: value3
        key4: [sub1, sub2, sub3, sub4]

These options are not known by my extension, I would like to get an associative array:

array(
    "key1"=>"value1",
    "key"=>"value2",
    "key3"=>"value3",
    "key4"=>array("sub1","sub2","sub3","sub4") 
);

My tree looks like:

$rootNode
    ->addDefaultsIfNotSet()
    ->children()
        ->arrayNode('options')
            ->useAttributeAsKey('key')
            ->treatNullLike(array())
            ->prototype('scalar')->end()
        ->end()
    ->end();

The problem is that with the key/value "key4" I have an exception (this is normal as the prototype is 'array').

So my question is : How can I mix both scalar and array options?

  • 写回答

1条回答 默认 最新

  • douyin9987 2014-08-08 15:16
    关注

    Thanks for the help @lackovic!

    Here is my solution:

    public function load(array $configs, ContainerBuilder $container)
    {
        $processor     = new Processor();
        $configuration = new Configuration($this->getAlias());
    
        $options = array();
        if(isset($configs[0]['options']))
        {
            $options = $configs[0]['options'];
            unset($configs[0]['options']);
        }
        $config = $processor->processConfiguration($configuration, $configs);
    
    
        // Now $options contains all the options and $config the configuration of my bundle
        ...
    }
    

    This solution help also to include sub-levels of configuration:

    my_project:
        options:
            key1: value1
            key2: value2
            key3: value3
            key4: [value1, value2, value3, value4]
            key5: 123
            key6: 
                subkey1 : [value5, value6, value7]
    

    That returns:

    array
    (
        "key1" => "value1"
        "key2" => "value2"
        "key3" => "value3"
        "key4" => array(
                0 => "value1"
                1 => "value2"
                2 => "value3"
                3 => "value4"
        )
        "key5" => 123
        "key6" => array(
            "value1" => array(
                0 => "value2"
                1 => "value3"
                2 => "value4"
            )
        )
    )
    

    )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 三菱FX系列PLC串口指令
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型