1

Configurationクラスを作成し、そのクラスの構成を定義するツリーを構築しましたが、非常に醜いと思います。私の質問は、クラスを単純化するための解決策を見つけることです。

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

私の構成は正常に機能しますが、非常に大きいので、最小化して単純化し、コードの繰り返しを停止します。

編集

OK、私はこの解決策を知っていますが、私の構成には適用できないと思います。たとえば、Twig構成ファイルでは、「-> end()」の後のクラスの最後でカスタム関数を使用しますが、私の構成ではそれを使用します「prototype()」の内部にもコードの繰り返しがたくさんありますが、子の内部に子があります...したがって、正しく最小化するのは困難です...

4

1 に答える 1

1

Twig 設定ファイルのように: メソッドを使用:

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

次にaddMyCustomSectionメソッドで:

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

参照

于 2012-09-21T17:50:41.530 に答える