I have the following configuration:
my_project:
options:
key1: value1
key2: value2
key3: value3
key4: [sub1, sub2, sub3, sub4]
These options are not known by my extension, I would like to get an associative array:
array(
"key1"=>"value1",
"key"=>"value2",
"key3"=>"value3",
"key4"=>array("sub1","sub2","sub3","sub4")
);
My tree looks like:
$rootNode
->addDefaultsIfNotSet()
->children()
->arrayNode('options')
->useAttributeAsKey('key')
->treatNullLike(array())
->prototype('scalar')->end()
->end()
->end();
The problem is that with the key/value "key4" I have an exception (this is normal as the prototype is 'array').
So my question is : How can I mix both scalar and array options?
Thanks for the help @lackovic!
Here is my solution:
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = new Configuration($this->getAlias());
$options = array();
if(isset($configs[0]['options']))
{
$options = $configs[0]['options'];
unset($configs[0]['options']);
}
$config = $processor->processConfiguration($configuration, $configs);
// Now $options contains all the options and $config the configuration of my bundle
...
}
This solution help also to include sub-levels of configuration:
my_project:
options:
key1: value1
key2: value2
key3: value3
key4: [value1, value2, value3, value4]
key5: 123
key6:
subkey1 : [value5, value6, value7]
That returns:
array
(
"key1" => "value1"
"key2" => "value2"
"key3" => "value3"
"key4" => array(
0 => "value1"
1 => "value2"
2 => "value3"
3 => "value4"
)
"key5" => 123
"key6" => array(
"value1" => array(
0 => "value2"
1 => "value3"
2 => "value4"
)
)
)
)