1

app/config/config.yml我为我的包添加了一些自定义设置

acme:
    acme_services:    
      service_a:
        options: { name: I, id: X, type: F, error: E }
      service_b:
        options: { name: J, id: Z, type: F, error: E }

现在src/ACME/Bundle/ACMEBundle/DependencyInjection/Configuration.php如何设置默认值和/或检查service_a/ service_b

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('acme');

    $rootNode
        ->children()
            // also removed the ->end() for each arrayNode but then I get a Fatal Error 
            ->arrayNode('acme_services')->end()
            ->arrayNode('another')->end()
            ->arrayNode('more')->end()
            ->arrayNode('blah')->end()
        ->end();

    return $treeBuilder;
}

所以我需要拉取service_aandservice_b数组,但我得到and的Unrecognized options错误。service_aservice_b

期望的结果是我想同时拥有service_aservice_bacme_services数组中,这就是为什么我可以针对使用acme_services的任何服务验证数组,无论是service_a还是service_b

注意:在 PHP 中我会这样写:(不确定这是否正确,但这是一个示例)

$acme_services = array(
    'acme_services' =>
        'service_a' => array(
            'options' => array(
                'name' => 'I',
                'id'   => 'X',
                'type' => 'F',
                'error'=> 'E',
            )
        ),
        'service_b' => array(
            'options' => array(
                'name' => 'J',
                'id'   => 'Z',
                'type' => 'F',
                'error'=> 'E',
            )
        )
);
4

3 回答 3

2

您要使用的是Prototypes,例如:

$rootNode
    ->children()
       ->prototype('array')
          ->children()
              ->arrayNode('options')
                  ->children()
                  ->scalarNode('name')->end()
                  ->scalarNode('id')->end()
                  ->scalarNode('type')->end()
                  ->scalarNode('error')->end()
              ->end()
           ->end()
     ->end()
->end()

这样,只要遵循此模式,您就可以定义任意数量的服务。

于 2012-08-14T18:35:12.243 回答
1

你可以试试 :

public function getConfigTreeBuilder()
{
    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('acme');

    $rootNode
        ->children()
            // also removed the ->end() for each arrayNode but then I get a Fatal Error 
            ->arrayNode('acme_services')
                 ->children()
                      ->arrayNode('service_a')
                          ->children()
                              ->arrayNode('options')->end()
                      ->arrayNode('service_b')
                          ->children()
                              ->arrayNode('options')->end()
           ->end();

    return $treeBuilder;
}
于 2012-05-30T17:09:18.320 回答
0

试试这个:

$rootNode
   ->children()
        ->arrayNode('acme_services')
            ->prototype('array')
                ->children()
                    ->arrayNode('options')
                        ->children()
                            ->scalarNode('name')->end()
                            ->scalarNode('id')->end()
                            ->scalarNode('type')->end()
                            ->scalarNode('error')->end()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end()
->end();
于 2014-06-20T00:28:34.217 回答