我创建了一个配置类并构建了一个在该类中定义我的配置的树,但我想它非常难看,那么我的问题是找到一个解决方案来简化我的类?
<?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()”之后的类末尾使用自定义函数,但在我的配置中我使用它在“原型()”里面我也有很多重复的代码,但是孩子里面的孩子里面的孩子......所以很难正确地最小化它......