|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Menu\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Knp\Menu\FactoryInterface; |
|
6
|
|
|
use Knp\Menu\ItemInterface; |
|
7
|
|
|
use LAG\AdminBundle\Admin\Behaviors\TranslationKeyTrait; |
|
8
|
|
|
use LAG\AdminBundle\Admin\Factory\AdminFactory; |
|
9
|
|
|
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory; |
|
10
|
|
|
use LAG\AdminBundle\Menu\Configuration\MenuConfiguration; |
|
11
|
|
|
use LAG\AdminBundle\Menu\Configuration\MenuItemConfiguration; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create KNP menus from given options. |
|
17
|
|
|
*/ |
|
18
|
|
|
class MenuFactory |
|
19
|
|
|
{ |
|
20
|
|
|
use TranslationKeyTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var AdminFactory |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $adminFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var FactoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $menuFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ConfigurationFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $configurationFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var TranslatorInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $translator; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* MenuBuilder constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param FactoryInterface $menuFactory |
|
46
|
|
|
* @param AdminFactory $adminFactory |
|
47
|
|
|
* @param ConfigurationFactory $configurationFactory |
|
48
|
|
|
* @param TranslatorInterface $translator |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
FactoryInterface $menuFactory, |
|
52
|
|
|
AdminFactory $adminFactory, |
|
53
|
|
|
ConfigurationFactory $configurationFactory, |
|
54
|
|
|
TranslatorInterface $translator |
|
55
|
|
|
) { |
|
56
|
|
|
$this->adminFactory = $adminFactory; |
|
57
|
|
|
$this->menuFactory = $menuFactory; |
|
58
|
|
|
$this->configurationFactory = $configurationFactory; |
|
59
|
|
|
$this->translator = $translator; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Create a menu according to the given configuration. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param array $configuration |
|
67
|
|
|
* @return ItemInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
public function create($name, array $configuration) |
|
70
|
|
|
{ |
|
71
|
|
|
$resolver = new OptionsResolver(); |
|
72
|
|
|
$menuConfiguration = new MenuConfiguration(); |
|
73
|
|
|
$menuConfiguration->configureOptions($resolver); |
|
74
|
|
|
$menuConfiguration->setParameters($resolver->resolve($configuration)); |
|
75
|
|
|
|
|
76
|
|
|
$menu = $this |
|
77
|
|
|
->menuFactory |
|
78
|
|
|
->createItem($name, [ |
|
79
|
|
|
'childrenAttributes' => $menuConfiguration->getParameter('attr') |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
|
|
/** @var MenuItemConfiguration $itemConfiguration */ |
|
83
|
|
|
foreach ($menuConfiguration->getParameter('items') as $itemName => $itemConfiguration) { |
|
84
|
|
|
$text = $this->guessItemText($itemName, $itemConfiguration); |
|
85
|
|
|
|
|
86
|
|
|
if ($adminName = $itemConfiguration->getParameter('admin')) { |
|
87
|
|
|
// retrieve an existing admin |
|
88
|
|
|
$admin = $this |
|
89
|
|
|
->adminFactory |
|
90
|
|
|
->getAdmin($adminName); |
|
91
|
|
|
|
|
92
|
|
|
$route = $admin->generateRouteName($itemConfiguration->getParameter('action')); |
|
93
|
|
|
} else { |
|
94
|
|
|
$route = $itemConfiguration->getParameter('route'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$menu->addChild($text, [ |
|
98
|
|
|
'childrenAttributes' => $itemConfiguration->getParameter('attr'), |
|
99
|
|
|
'route' => $route, |
|
100
|
|
|
'routeParameters' => $itemConfiguration->getParameter('parameters'), |
|
101
|
|
|
'attributes' => [ |
|
102
|
|
|
'icon' => $itemConfiguration->getParameter('icon'), |
|
103
|
|
|
] |
|
104
|
|
|
]); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $menu; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Guess an item text according to the configuration. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $name |
|
114
|
|
|
* @param MenuItemConfiguration $itemConfiguration |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function guessItemText($name, MenuItemConfiguration $itemConfiguration) |
|
118
|
|
|
{ |
|
119
|
|
|
$text = $itemConfiguration->getParameter('text'); |
|
120
|
|
|
|
|
121
|
|
|
if ($text) { |
|
122
|
|
|
return $text; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// if an admin is defined, we get the text using the translation pattern and the admin action's name |
|
126
|
|
|
if ($admin = $itemConfiguration->getParameter('admin')) { |
|
127
|
|
|
$translationPattern = $this |
|
128
|
|
|
->configurationFactory |
|
129
|
|
|
->getApplicationConfiguration() |
|
130
|
|
|
->getParameter('translation')['pattern']; |
|
131
|
|
|
$action = $itemConfiguration->getParameter('action'); |
|
132
|
|
|
|
|
133
|
|
|
$text = $this->getTranslationKey($translationPattern, $action, $admin); |
|
134
|
|
|
} else { |
|
135
|
|
|
$text = $name; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $text; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|