Symfony配置默认值

时间:2016-02-08 11:19:57

标签: php configuration symfony

我相信我的配置是正确的,但我想要我的redis端口和方案配置选项的默认值,但它们是以空值形式出现的?

任何人都可以看到问题所在吗?

enter image description here

这是我的配置。

/**
 * {@inheritdoc}
 */
public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();

    $rootNode = $treeBuilder->root('company_name');
    $rootNode
        ->children()
            ->arrayNode('cache')
                ->children()
                    ->arrayNode('redis')
                        ->addDefaultsIfNotSet()
                        ->treatNullLike([
                            'scheme' => 'tcp',
                            'port' => 6379,
                        ])
                        ->children()
                            ->scalarNode('scheme')
                                ->defaultValue('tcp')
                            ->end()
                            ->scalarNode('host')
                                ->isRequired()
                                ->cannotBeEmpty()
                            ->end()
                            ->integerNode('port')
                                ->defaultValue(6379)
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();

    return $treeBuilder;
}

这是我的parameters.yml文件

parameters:
    company_name:
        cache:
            redis:
                host: dev-sessionstore.companyname.com
                schema: ~
                port: ~

控制台输出:

$ php bin/console config:dump-reference CompanyNameCacheBundle
# Default configuration for "CompanyNameCacheBundle"
company_name:
    cache:
        redis:
            namespace:            apps
            scheme:               tcp
            host:                 ~ # Required
            port:                 6379
        apcu:
            namespace:            phpcache

我希望方案和端口使用默认值,但是导致它们为空的是什么?

1 个答案:

答案 0 :(得分:3)

我知道这是一个老问题,但我在谷歌搜索一个不同的问题时偶然发现它并发现它没有得到答复。

问题是您只是指定应该如何处理整个srand((unsigned)time(0)); HANDLE tFantasma = CreateThread(NULL, 0, fantasma, NULL, 0, NULL); --->fantasma() { mov = rand() % 4; } srand((unsigned)time(0)); // reseeded; same time(); REMOVE THIS LINE HANDLE tFantasma2 = CreateThread(NULL, 0, fantasma, NULL, 0, NULL); --->fantasma() { mov = rand() % 4; // sequnce restarted: same move } 数组的null,而不是redisscheme值。您指定了它们的默认值,但是因为您要将这些单独的键设置为null,您需要指定应如何处理每个键的空值:

port

参数文件中还有一个拼写错误,它应该是/** * {@inheritdoc} */ public function getConfigTreeBuilder() { // Using an array so the values only need to be changed in one place $redisDefaults = [ 'scheme' => 'tcp', 'port' => 6379, ]; $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('company_name'); $rootNode ->children() ->arrayNode('cache') ->children() ->arrayNode('redis') ->addDefaultsIfNotSet() ->treatNullLike($redisDefaults) ->children() ->scalarNode('scheme') ->defaultValue($redisDefaults['scheme']) ->treatNullLike($redisDefaults['scheme']) // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->end() ->scalarNode('host') ->isRequired() ->cannotBeEmpty() ->end() ->integerNode('port') ->defaultValue($redisDefaults['port']) ->treatNullLike($redisDefaults['port']) // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ->end() ->end() ->end() ->end() ->end() ->end(); return $treeBuilder; } ,而不是scheme