如何使用 TreeBuilder 定义动态配置?
How to define dynamic configuration using the TreeBuilder?
我想配置一个包以允许不同公司的不同行为。它们中的配置结构将是相同的。
我的 config.yml
应该是这样的:
bunde_namespace:
company:
company_1:
foo: bar
baz: poit
company_2:
foo: bar
baz: poit
company_3:
...
当我访问 $config
时,我希望数组看起来像这样:
$config['company'] = [
'company_one' => [
'foo' => 'bar'
'baz' => 'poit'
],
'company_two' => [
'foo' => 'bar'
'baz' => 'poit'
],
...
];
然而我没有使用 TreeBuilder 和 setting up the configuration as described in the docs 的经验,我仍然不知道如何设置我的配置以便它将 company
的子项视为键控数组。
到目前为止,我所做的是为一家公司设置配置,如下所示:
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('dreamlines_booking_service_fibos');
$rootNode
->children()
->arrayNode('company')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
简化后的 config.yml
看起来像这样:
bundle_namespace:
company:
foo: bar
baz: poit
然而这不是我想要的
我假设我需要使用 useAttributeAsKey
但我无法使用它。
这失败了:
$rootNode
->children()
->arrayNode('company')
->prototype('array')
->useAttributeAsKey('name')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end()
->end();
说明:
[Symfony\Component\Config\Definition\Exception\InvalidDefinitionException]
->useAttributeAsKey() is not applicable to concrete nodes at path "bundle_namespace."
我哪里错了?
你得到的错误是当你尝试在原型上应用 useAttributeAsKey
时引起的,但由于该方法是 ArrayNodeDefinition 的一部分,因此需要在 [=14] 之后立即添加=].这样试一下,错误就会消失。
现在,如果我正确理解你的问题,下面的输出就是你的目标:
Array
(
[company] => Array
(
[company_1] => Array
(
[foo] => bar
[baz] => baz
)
[company_2] => Array
(
[foo] => bar
[baz] => baz
)
)
)
您可以使用以下结构实现:
$rootNode
->children()
->arrayNode('company')
->prototype('array')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end()
->end()
;
加载的配置:
app:
company:
company_1:
foo: bar
baz: baz
company_2:
foo: bar
baz: baz
如果我误解了你的问题,请发表评论。
我想配置一个包以允许不同公司的不同行为。它们中的配置结构将是相同的。
我的 config.yml
应该是这样的:
bunde_namespace:
company:
company_1:
foo: bar
baz: poit
company_2:
foo: bar
baz: poit
company_3:
...
当我访问 $config
时,我希望数组看起来像这样:
$config['company'] = [
'company_one' => [
'foo' => 'bar'
'baz' => 'poit'
],
'company_two' => [
'foo' => 'bar'
'baz' => 'poit'
],
...
];
然而我没有使用 TreeBuilder 和 setting up the configuration as described in the docs 的经验,我仍然不知道如何设置我的配置以便它将 company
的子项视为键控数组。
到目前为止,我所做的是为一家公司设置配置,如下所示:
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('dreamlines_booking_service_fibos');
$rootNode
->children()
->arrayNode('company')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end();
return $treeBuilder;
}
}
简化后的 config.yml
看起来像这样:
bundle_namespace:
company:
foo: bar
baz: poit
然而这不是我想要的
我假设我需要使用 useAttributeAsKey
但我无法使用它。
这失败了:
$rootNode
->children()
->arrayNode('company')
->prototype('array')
->useAttributeAsKey('name')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end()
->end();
说明:
[Symfony\Component\Config\Definition\Exception\InvalidDefinitionException] ->useAttributeAsKey() is not applicable to concrete nodes at path "bundle_namespace."
我哪里错了?
你得到的错误是当你尝试在原型上应用 useAttributeAsKey
时引起的,但由于该方法是 ArrayNodeDefinition 的一部分,因此需要在 [=14] 之后立即添加=].这样试一下,错误就会消失。
现在,如果我正确理解你的问题,下面的输出就是你的目标:
Array
(
[company] => Array
(
[company_1] => Array
(
[foo] => bar
[baz] => baz
)
[company_2] => Array
(
[foo] => bar
[baz] => baz
)
)
)
您可以使用以下结构实现:
$rootNode
->children()
->arrayNode('company')
->prototype('array')
->children()
->scalarNode('foo')->end()
->scalarNode('baz')->end()
->end()
->end()
->end()
->end()
;
加载的配置:
app:
company:
company_1:
foo: bar
baz: baz
company_2:
foo: bar
baz: baz
如果我误解了你的问题,请发表评论。