1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\MenuBundle\Menu; |
13
|
|
|
|
14
|
|
|
use Knp\Menu\FactoryInterface; |
15
|
|
|
use Knp\Menu\ItemInterface; |
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
17
|
|
|
|
18
|
|
|
final class ConfigBuilder implements ConfigBuilderInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var FactoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $factory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var TranslatorInterface |
27
|
|
|
*/ |
28
|
|
|
private $translator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $defaultOptions; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FactoryInterface $factory |
37
|
|
|
* @param TranslatorInterface $translator |
38
|
|
|
*/ |
39
|
|
|
public function __construct(FactoryInterface $factory, TranslatorInterface $translator) |
40
|
|
|
{ |
41
|
|
|
$this->factory = $factory; |
42
|
|
|
$this->translator = $translator; |
43
|
|
|
|
44
|
|
|
$this->defaultOptions = [ |
45
|
|
|
'attributes' => [ |
46
|
|
|
'class' => 'dropdown', |
47
|
|
|
], |
48
|
|
|
'childrenAttributes' => [ |
49
|
|
|
'class' => 'dropdown-menu', |
50
|
|
|
], |
51
|
|
|
'linkAttributes' => [ |
52
|
|
|
'class' => 'dropdown-toggle', |
53
|
|
|
'data-toggle' => 'dropdown', |
54
|
|
|
'data-target' => '#', |
55
|
|
|
], |
56
|
|
|
'extras' => [ |
57
|
|
|
'safe_label' => true, |
58
|
|
|
'translation_domain' => false, |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function buildMenu(array $menu, array $options): ItemInterface |
67
|
|
|
{ |
68
|
|
|
$menuOptions = array_merge_recursive([ |
69
|
|
|
'attributes' => [ |
70
|
|
|
'class' => 'nav', |
71
|
|
|
], |
72
|
|
|
'childrenAttributes' => [ |
73
|
|
|
'class' => 'nav nav-pills', |
74
|
|
|
], |
75
|
|
|
], $options); |
76
|
|
|
|
77
|
|
|
if (\array_key_exists('attributes', $menu)) { |
78
|
|
|
$menuOptions = array_merge($menuOptions, [ |
79
|
|
|
'childrenAttributes' => $menu['attributes'], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$menuItem = $this->factory->createItem('main', $menuOptions); |
84
|
|
|
|
85
|
|
|
if (\array_key_exists('items', $menu)) { |
86
|
|
|
$this->buildSubMenu($menuItem, $menu['items']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $menuItem; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ItemInterface $menu |
94
|
|
|
* @param array $configItems |
95
|
|
|
* @param array $baseMenuOptions |
96
|
|
|
* |
97
|
|
|
* @return ItemInterface |
98
|
|
|
*/ |
99
|
|
|
private function buildSubMenu(ItemInterface $menu, array $configItems, array $baseMenuOptions = []): ItemInterface |
100
|
|
|
{ |
101
|
|
|
foreach ($configItems as $item) { |
102
|
|
|
$this->createItem($menu, $baseMenuOptions, $item); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $menu; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $id |
110
|
|
|
* @param array $parameters |
111
|
|
|
* @param false|string|null $domain |
112
|
|
|
* @param string|null $locale |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
private function trans(string $id, array $parameters = [], $domain = null, $locale = null): string |
117
|
|
|
{ |
118
|
|
|
if (false === $domain) { |
119
|
|
|
return $id; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->translator->trans($id, $parameters, $domain, $locale); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param ItemInterface $menu |
127
|
|
|
* @param array $baseMenuOptions |
128
|
|
|
* @param array $itemDefinition |
129
|
|
|
*/ |
130
|
|
|
private function createItem(ItemInterface $menu, array $baseMenuOptions, array $itemDefinition): void |
131
|
|
|
{ |
132
|
|
|
$label = $this->trans($itemDefinition['label'], [], $itemDefinition['label_catalogue']); |
133
|
|
|
|
134
|
|
|
if (!empty($itemDefinition['icon'])) { |
135
|
|
|
$label = '<i class="'.$itemDefinition['icon'].'"></i> '.$label; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$menuOptions = self::getOptions($baseMenuOptions, $itemDefinition); |
139
|
|
|
|
140
|
|
|
if (\count($itemDefinition['children']) > 0) { |
141
|
|
|
$label .= ' <b class="caret caret-menu"></b>'; |
142
|
|
|
$menuOptions = array_merge($menuOptions, $this->defaultOptions, [ |
143
|
|
|
'label' => $label, |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$subMenu = $this->factory->createItem($label, $menuOptions); |
148
|
|
|
$menu->addChild($subMenu); |
149
|
|
|
|
150
|
|
|
if (\count($itemDefinition['children']) > 0) { |
151
|
|
|
$this->buildSubMenu($subMenu, $itemDefinition['children']); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $baseMenuOptions |
157
|
|
|
* @param array $itemDefinition |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
private static function getOptions(array $baseMenuOptions, array $itemDefinition): array |
162
|
|
|
{ |
163
|
|
|
return array_merge($baseMenuOptions, [ |
164
|
|
|
'route' => $itemDefinition['route'], |
165
|
|
|
'routeParameters' => $itemDefinition['routeParams'], |
166
|
|
|
'linkAttributes' => ['class' => $itemDefinition['class']], |
167
|
|
|
'extras' => [ |
168
|
|
|
'safe_label' => true, |
169
|
|
|
'translation_domain' => false, |
170
|
|
|
], |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|