为什么我的配置没有被覆盖在symfony2中的测试环境中


Why isn't my configuraton overwritten for the test environment in symfony2?

我有一个symfony2应用程序,它需要针对某些环境进行不同的配置设置,例如 test .

我像这样为测试环境覆盖我的 config.yml:

AppKernel.php

public function registerContainerConfiguration(LoaderInterface $loader)
{
    foreach ($this->getBundles() as $bundle) {
        if (false === strpos($bundle->getName(), 'Dreamlines')) {
            continue;
        }
        $configFile = $bundle->getPath() . '/Resources/config/config.yml';
        if (!file_exists($configFile)) {
            continue;
        }
        $loader->load($configFile);
    }
    $loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
}

在我的config.yml中,我定义了:

default_airports:
    cun:
        de:
            - FRA

在我的 config_test.yml 中,我定义了以下内容来覆盖该值:

default_airports:
    cun:
        de:
            - HAM

我的配置树生成器查找的配置看起来像

$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder
->root('le_bundle');
    $rootNode
    ...
    ->arrayNode('default_airports')
       ->isRequired()
       ->requiresAtLeastOneElement()
        ->useAttributeAsKey('name')
        ->prototype('array')
            ->prototype('array')
                ->prototype('scalar')->end()
            ->end()
        ->end()
    ->end()

然而,配置未被正确覆盖,导致测试运行失败。

这是怎么回事?我已经使用此策略成功重写了其他配置条目。

比较每个环境的文件中的default_airports时:

  • app/cache/dev/appDevDebugProjectContainer.php
  • app/cache/test/appTestDebugProjectContainer.php

阵列丢失了其密钥,而不是预期的

`de` => arrray(0 => 'HAM')

有一个 0 索引数组。

因此,Configuration.phpConfigTreeBuilder的相关部分必须如下所示:

->arrayNode('default_airports')
   ->isRequired()
   ->requiresAtLeastOneElement()
    ->useAttributeAsKey('name')
    ->prototype('array')
        ->useAttributeAsKey('name') // FIXES LOST KEY IN CONFIG FOR TEST ENV
        ->prototype('array')
            ->useAttributeAsKey('name')
            ->prototype('scalar')->end()
        ->end()
    ->end()
->end()