我有以下配置:
my_project:
options:
key1: value1
key2: value2
key3: value3
key4: [sub1, sub2, sub3, sub4]
我的扩展程序不知道这些选项,我想获得一个关联数组:
array(
"key1"=>"value1",
"key"=>"value2",
"key3"=>"value3",
"key4"=>array("sub1","sub2","sub3","sub4")
);
我的树看起来像:
$rootNode
->addDefaultsIfNotSet()
->children()
->arrayNode('options')
->useAttributeAsKey('key')
->treatNullLike(array())
->prototype('scalar')->end()
->end()
->end();
问题是,使用键/值“key4”,我有一个例外(这是正常的,因为原型是'数组')。
所以我的问题是:我如何混合标量和数组选项?
答案 0 :(得分:0)
感谢@lackovic的帮助!
这是我的解决方案:
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
...
}
此解决方案还有助于包含子级别的配置:
my_project:
options:
key1: value1
key2: value2
key3: value3
key4: [value1, value2, value3, value4]
key5: 123
key6:
subkey1 : [value5, value6, value7]
返回:
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"
)
)
)
)