Symfony2为bundle指定未声明的配置

时间:2015-02-17 21:19:54

标签: symfony

我想在我的包中注入配置。我的配置如下所示:

osc_storage:
    storages:
        image:
            adapter: aws_s3
            options:
                option1: 'test'
                option2: 'test'
                ...

基本上,我想要的是不定义options字段将包含的内容并简单地注入它。在我的configTreeBuilder中,我有以下内容:

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('osc_storage');


    // Here you should define the parameters that are allowed to
    // configure your bundle. See the documentation linked above for
    // more information on that topic.

    $rootNode
        ->children()
            ->arrayNode('storages')
                ->prototype('array')
                    ->children()
                        ->scalarNode('adapter')->end()
                        ->arrayNode('options')
                            ->prototype('array')
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();

    return $treeBuilder;
}

但是,我收到以下错误:

Invalid type for path "osc_storage.storages.image.options.option1". Expected array, but got string

有没有办法指定您知道这是一个关联数组,但又不想指定每个节点?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

 ->arrayNode('storages')
     ->prototype('array')
         ->children()
             ->scalarNode('adapter')->end()
             ->variableNode('options')->end()
         ->end()
     ->end()
 ->end()