0

I’m setting up bundle’s configuration, I’m using Configuration class. I’m having my_node node which has a list child and I want to set a default value for it.

Thing is that I want to set value from parameter. When I’m trying to call defaultValue with parameter name it throws InvalidArgumentException with the default value of an array node has to be an array.

->arrayNode(‘my_node’)
    ->children()
        ->arrayNode('list')
            ->prototype('scalar')->end()
            ->defaultValue([‘foo’, ‘bar’])   //this works
            ->defaultValue(‘%some_parameter%’)  //this does not
        ->end()
     ->end()
->end()

Any ideas how can I achieve that?

0

1 Answer 1

1

As you are defining the node as an array node, defaultValue() will assert that the value is an array. At this stage (building a configuration tree), there is no notion of parameters, therefor the value will not be resolved but seen as a scalar string to the Configuration builder.

To get around this, you can set the default value to null. In your container extension class, as you are reading out the config array and configuring the container with it, you can then replace the null value with the parameter, which will then be resolved later on at container compilation.

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

3 Comments

Your extension does have access to the container builder object which means you could pull the parameter out and then pass it to the configuration object. But I think that sort of defeats the purpose of the configuration system.
@Cerad, you dont need to pull the parameter value, it will be resolved later on.
True but basically that means you have to check for null and then do your parameter thing later on in the cycle. But I guess my main point is that I don't see the need for a paramerter default value. The default value should be set in the tree builder and then the user can override it in config.yml if they so chose. Of course I could be misunderstanding the use case completely.

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.