如何合并而不使用Symfony / Config~2.6覆盖附加的配置部分

时间:2015-03-25 12:46:22

标签: php symfony symfony-components

我有2 + Symfony\Component\Console\Command,每个都返回一段配置Symfony\Component\Config\Definition\Builder\TreeBuilder

class ProjectFooCommand extends Command {
public function getConfigTree()
{
    return (new TreeBuilder())
        ->root('project')
            ->children()
                ->arrayNode('foo')
                    ->children()
                        // specific
                    ->end()
                ->end()
            ->end()

    ;
}
}
class ProjectBarCommand extends Command {
public function getConfigTree()
{
    return (new TreeBuilder())
        ->root('project')
            ->children()
                ->arrayNode('bar')
                    ->children()
                        // specific
                    ->end()
                ->end()
            ->end()

    ;
}
}

使用ArrayNodeDefinition::append()

将它们合并为一个配置
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
    $builder = new TreeBuilder();

    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('codio');

    $rootNode
        ->children()
        ->scalarNode('lorem')->defaultValue('ABC')->end()
        ->scalarNode('ipsum')->defaultValue('123')->end()
        ->end()
    ;

    foreach ($this->application->all() as $command)
    {
        $rootNode->append($command->getConfigTree());
    }

    return $treeBuilder;
}
}

当它被添加到只有一个命令的配置时,一切正常。当我尝试添加第二个时,它会覆盖之前添加的。我该如何解决这个问题?

应该是:

codio:
    lorem: ABC
    ipsum: 123
    project:
        foo:
            ...
        bar:
            ...

电流:

codio:
    lorem: ABC
    ipsum: 123
    project:
        bar:
            ...

1 个答案:

答案 0 :(得分:0)