【发布时间】:2013-11-03 11:12:10
【问题描述】:
我正在尝试创建一个包来管理可以通过配置文件配置的菜单。
所以我在class DependencyInjection\Configuration 中写了一些配置约束。
所需的配置是必须添加到菜单中的项目数组。每个项目可以有 3 种不同的类型(link、link_notification、widget)。并且对于每种类型,item 都需要其他属性(如route、label 等)。
配置示例:
menu:
utilities:
- { type: link, icon: icon_name, label: text, route: { name: route_name, params: {} } }
- { type: link_notification, notification: notification_text }
- { type: widget, controller: controller_name }
我被困住了,因为我找不到如何为每种类型定义不同的数组约束。
我找不到翻译条件的方法:
IF type == "link" THEN scalarNode "icon" IS REQUIRED AND scalarNode "label" IS REQUIRED ...
配置文件如下所示:
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('aiomedia_menu');
$rootNode
->children()
->arrayNode('utilities')
->prototype('array')
->children()
->enumNode('type')
->values(array ('link', 'link_notification', 'widget'))
->isRequired()
->end()
->end()
->end()
->end()
->end();
return $treeBuilder;
}
我在 Symfony2 文档中看到了方法 ->ifXXX() [...] ->then(),但我不知道如何在这种情况下使用它们。
【问题讨论】:
标签: symfony configuration tree