How to define a dynamic configuration using TreeBuilder?

I want to configure the package to allow different actions for different companies. The configuration structure inside them will be the same.

My config.ymlwill look like this:

bunde_namespace:
    company:
        company_1:
            foo: bar
            baz: poit
        company_2:
            foo: bar
            baz: poit
        company_3:
            ...

When I refer to $config, I expect the array to look something like this:

$config['company'] = [
    'company_one' => [
        'foo' => 'bar'
        'baz' => 'poit'
    ],
    'company_two' => [
        'foo' => 'bar'
        'baz' => 'poit'
    ],
    ...
];

However, I have no experience with TreeBuilder and configuring as described in the docs , and it still doesn't let me know how to configure my configuration, so that it treats the children companyas arrays with keys.

What I have achieved so far is to configure for one company, for example:

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;
    }
}

And the simplified one config.ymlwill look like this:

bundle_namespace:
    company:
        foo: bar
        baz: poit

But that is not what I want.

, useAttributeAsKey, .

:

    $rootNode
        ->children()
        ->arrayNode('company')
            ->prototype('array')
            ->useAttributeAsKey('name')
            ->children()
                    ->scalarNode('foo')->end()
                    ->scalarNode('baz')->end()
                ->end()
           ->end()
        ->end()
    ->end();

:

[Symfony\Component\Config\\Exception\InvalidDefinitionException] → useAttributeAsKey() "bundle_namespace."

?

+2
1

, , , useAttributeAsKey , ArrayNodeDefinition, ->arrayNode(...). , .

, , , :

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

, , .

+2

Source: https://habr.com/ru/post/1620443/


All Articles