Search code examples
symfonysymfony-2.1

Treebuilder Validation for yml file


I created a Configuration class and build a tree that defines my configuration in that class,but I guess it's very ugly then my question is to find a solution to simplify my class?

<?php
  namespace Myapp\Mybundle\DependencyInjection;

  use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  use Symfony\Component\Config\Definition\ConfigurationInterface;


class Configuration implements ConfigurationInterface
{
/**
 * {@inheritDoc}
 */

public function getConfigTreeBuilder()
{

    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('em_profession');

    $rootNode
        ->children()
                 ->arrayNode('region')
         ->isRequired()
                 ->requiresAtLeastOneElement()
                 ->useAttributeAsKey('id')
                            ->prototype('array')
                                       ->children()
                                             ->scalarNode('label')
                                             ->isRequired()
                                             ->cannotBeEmpty()->defaultValue('em_profession_label')->end()
                                             ->arrayNode('childrens') 
                                             ->isRequired()
                                             ->requiresAtLeastOneElement()
                                             ->useAttributeAsKey('id')
                                                     ->prototype('array')
                                                           ->children()
                                                                 ->scalarNode('label')->end()
                                                                 ->arrayNode('childrens')
                                                                 ->isRequired()
                                                                 ->requiresAtLeastOneElement()
                                                                 ->useAttributeAsKey('id')
                                                                      ->prototype('array')
                                                                            ->children()
                                                                                 ->scalarNode('label')->end()
                                                                                 ->arrayNode('childrens')
                                                                                 ->isRequired()
                                                                                 ->requiresAtLeastOneElement()
                                                                                 ->useAttributeAsKey('id')
                                                                                      ->prototype('array')
                                                                                            ->children()
                                                                                                 ->scalarNode('label')->end()
                                                                                                 ->arrayNode('childrens')
                                                                                                 ->isRequired()
                                                                                                 ->requiresAtLeastOneElement()
                                                                                                 ->useAttributeAsKey('id')
                                                                                                     ->prototype('array')
                                                                                                           ->children()
                                                                                                                ->scalarNode('label')->end()
                                                                                                                ->arrayNode('childrens')
                                                                                                                ->isRequired()
                                                                                                                ->requiresAtLeastOneElement()
                                                                                                                ->useAttributeAsKey('id')
                                                                                                                      ->prototype('array')
                                                                                                                       ->end()
                                                                                                                ->end()
                                                                                                           ->end()

                                                                                                     ->end()

                                                                                                  ->end()
                                                                                            ->end()
                                                                                       ->end()
                                                                                 ->end()
                                                                            ->end()

                                                                       ->end()

                                                                  ->end()
                                                         ->end()

                                                     ->end()

                                             ->end()
            
                                      ->end()
                             ->end()
         ->end()
        ->end()
    ;

    return $treeBuilder;
}

My configuration works fine but it's very big so I which to minimize it to simplify it and to stop the repetition of code.

edit

OK, I know this solution but I guess I can't apply it in my configuration, for example in Twig configuration file they use a custom function in the end of class after "->end()" but in my configuration I use it inside "prototype()" also I have a lot of repetition of code but child inside child inside child... so it's difficult to minimize it correctly...


Solution

  • Like Twig configuration file: using methods:

    $rootNode = $treeBuilder->root('em_profession');
    $this->addMyCustomSection($rootNode);
    

    Then in addMyCustomSection method:

    private function addMyCustomSection(ArrayNodeDefinition $rootNode)
    {
        // Continue modifying $rootNode
    }
    

    Reference.