yml文件的Treebuilder验证

时间:2012-09-21 17:12:03

标签: symfony symfony-2.1

我创建了一个Configuration类并构建了一个树,用于定义该类中的配置,但我想这非常难看,那么我的问题是找到一个简化我的calss的解决方案?

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

我的配置运行正常,但它非常大,所以我最小化它以简化它并停止重复代码......

修改

好的我知道这个解决方案,但我想我不能在我的配置中应用它,例如在Twig配置文件中,他们在“ - &gt; end()”之后在类的末尾使用自定义函数但是在我的配置中我使用它在“prototype()”里面也有很多重复的代码,但孩子在孩子里面的孩子...所以很难正确地把它最小化......

1 个答案:

答案 0 :(得分:1)

Twig configuration file类似:使用方法:

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

然后在addMyCustomSection方法中:

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

Reference