为什么不在symfony2中为测试环境覆盖我的配置?

时间:2022-10-16 14:01:04

I have a symfony2 application that requires a different config settings for certain environments, e.g. test.

我有一个symfony2应用程序,需要为某些环境设置不同的配置,例如测试。

I overwrite my config.yml for the test envioronment like so:

我覆盖了我的config.yml for test envioronment,如下所示:

AppKernel.php:

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');
}

In my config.yml I define:

在我的config.yml中我定义:

default_airports:
    cun:
        de:
            - FRA

and in my config_test.yml I define the following to overwrite the value:

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

default_airports:
    cun:
        de:
            - HAM

My ConfigTreeBuilder looks for the config looks like

我的ConfigTreeBuilder查找配置看起来像

$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()

Yet the config is not properly overwritten, making the test run fail.

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

What is going on here? I have succesfully rewritten other config entries using this strategy.

这里发生了什么?我使用这种策略成功地重写了其他配置条目。

1 个解决方案

#1


1  

When comparing default_airports in the files for each environment:

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

  • app/cache/dev/appDevDebugProjectContainer.php
  • 应用程序/缓存的/ dev / appDevDebugProjectContainer.php
  • app/cache/test/appTestDebugProjectContainer.php
  • 应用程序/缓存/测试/ appTestDebugProjectContainer.php

the array lost its key, instead of the expected

数组丢失了它的键,而不是预期的

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

there was a 0-indexed array.

有一个0索引的数组。

So the relevant part of the ConfigTreeBuilder in the Configuration.php has to look like:

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

->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()

#1


1  

When comparing default_airports in the files for each environment:

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

  • app/cache/dev/appDevDebugProjectContainer.php
  • 应用程序/缓存的/ dev / appDevDebugProjectContainer.php
  • app/cache/test/appTestDebugProjectContainer.php
  • 应用程序/缓存/测试/ appTestDebugProjectContainer.php

the array lost its key, instead of the expected

数组丢失了它的键,而不是预期的

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

there was a 0-indexed array.

有一个0索引的数组。

So the relevant part of the ConfigTreeBuilder in the Configuration.php has to look like:

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

->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()