Symfony2 yaml数组选项

时间:2012-05-30 15:25:00

标签: php dependency-injection symfony yaml

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_aservice_b数组,但Unrecognized optionsservice_a出现service_b错误。

我想在service_a数组中同时拥有service_bacme_services,这就是为什么我可以针对任何服务验证acme_services数组的原因使用service_aservice_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',
            )
        )
);

3 个答案:

答案 0 :(得分: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()

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

答案 1 :(得分: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;
}

答案 2 :(得分: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();