【问题标题】:Treebuilder Validation for yml fileyml 文件的 Treebuilder 验证
【发布时间】:2012-09-21 17:12:03
【问题描述】:

我创建了一个配置类并构建了一个树来定义我在该类中的配置,但我想它很丑那么我的问题是找到一个解决方案来简化我的类?

<?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 配置文件中,他们在“->end()”之后的类末尾使用自定义函数,但在我的配置中我在“prototype()”中使用它,我也有很多重复的代码,但是 child inside child inside child...所以很难正确地最小化它...

【问题讨论】:

    标签: symfony symfony-2.1


    【解决方案1】:

    点赞Twig configuration file:使用方法:

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

    然后在addMyCustomSection方法中:

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

    Reference.

    【讨论】:

    • 好的,我知道这个解决方案,但我想我不能在我的配置中应用它,例如在 Twig 配置文件中,他们在“->end()”之后的类末尾使用自定义函数,但在我的配置我在“prototype()”中使用它,我也有很多重复的代码,但孩子在孩子里面的孩子......所以很难正确地最小化它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2012-09-13
    相关资源
    最近更新 更多