4

I believe my configuration to be correct but I want defaults for my redis port and scheme configurations option but they are coming out as nulls?

Can anyone see what the issue is?

enter image description here

Here is my configuration.

/**
 * {@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;
}

And here is my parameters.yml file

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

Console output:

$ 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

I want the scheme and port to use default values but what's causing them to be null?

1 Answer 1

3

I know this is an old question but I stumbled upon it while Googling a different issue and saw that it was unanswered.

The issue is that you are only specifying how null should be treated for the entire redis array, not the scheme and port values. You specify their default values, but because you're setting those individual keys as null you need to specify how null should be treated for each one:

/**
 * {@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;
}

There's also a typo in your parameters file, it should be scheme, not schema

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.