如何在Symfony Configuration.php中定义标量数组的数组?

问题描述

我想解析一个如下所示的YAML配置:

pageRoles:
  Report1: [abc,xyz,def]
  Report2: [fgh,xxx,yyy,rrr]

我希望生成的配置数组如下所示:

'pageRoles':
  'Report1':
    [
      'abc','xyz','def'
    ],'Report2': 
    [
      'fgh','xxx','yyy','rrr'
    ]

我现在有这个:

    ->arrayNode( 'pageRoles' )
      ->prototype( 'array' )
        ->useAttributeAsKey( 'name' )
        ->prototype( 'array' )
          ->prototype( 'scalar' )->end()
        ->end()
      ->end() // array prototype
    ->end() // pageRoles

并收到此错误

路径“ site.pageRoles.ActivityReport.0”的类型无效。预期 数组,但是有字符串

我想念什么?

解决方法

制作Symfony配置树。我最喜欢的消磨无聊一天的方式。这似乎可行:

    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder('my');
        $treeBuilder->getRootNode()
            ->children()
                ->arrayNode('pageRoles')
                    ->useAttributeAsKey('name')
                     ->arrayPrototype()->scalarPrototype()->end()->end()
                ->end() // pageRoles
            ->end() // root node
        ;
        return $treeBuilder;
    }

处理后,我得到:

array:1 [
  "pageRoles" => array:2 [
    "Report1" => array:3 [
      0 => "abc"
      1 => "xyz"
      2 => "def"
    ]
    "Report2" => array:4 [
      0 => "fgh"
      1 => "xxx"
      2 => "yyy"
      3 => "rrr"
    ]
  ]
]