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 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的