Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested prototypes in Symfony2 Configuration

With the Symfony2 TreeBuilder, its possible to configure prototypes for each node inside an array node, e.g. to support a configuration like

foo:
    xxx: { foo: bar}
    yyy: { foo: bar}

Where there are any number of xxx, yyy and so on (any value at all) nodes using a structure like

$rootNode
    ->arrayNode('foo')
    ->prototype('array')
        ->children()
            ->scalarNode('foo')

I would like to create another level of the same thing without introducing another named node in between, e.g. to support a configuration like

foo:
    xxx:
        aaa: { foo: bar}
        bbb: { foo: bar}
    yyy:
        ccc: { foo: bar}
        ddd: { foo: bar}

where there can be any number of xxx, yyy and so on nodes, each of which can contain any number of other aaa, bbb etc. nodes.

Is there any way to achieve this? Calling ->prototype() directly under the ->children() of another prototype call doesn't work, as the prototypes don't seem to support nested prototypes. As a result I'm having to hard code specific values for the xxx and yyy, limiting the flexibility of the bundle I'm writing.

like image 834
El Yobo Avatar asked Dec 14 '12 08:12

El Yobo


1 Answers

Checked in symfony 2.2, nesting works.

Don't use ->children() between nested ->prototype():

$rootNode->children()
   ->arrayNode('foo')
      ->prototype('array')
         ->prototype('array')
             ->children()
                ->scalarNode('foo');
like image 83
dr.scre Avatar answered Oct 15 '22 09:10

dr.scre