1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
4
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Event\Events; |
6
|
|
|
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent; |
7
|
|
|
use LAG\AdminBundle\Exception\Exception; |
8
|
|
|
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
|
11
|
|
|
class MenuSubscriber implements EventSubscriberInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $menuEnabled; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ResourceRegistryInterface |
20
|
|
|
*/ |
21
|
|
|
private $registry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $menuConfigurations; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
4 |
|
public static function getSubscribedEvents() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
4 |
|
Events::MENU_CONFIGURATION => 'defineMenuConfiguration', |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
6 |
|
public function __construct(bool $menuEnabled, ResourceRegistryInterface $registry, array $menuConfigurations = []) |
39
|
|
|
{ |
40
|
6 |
|
$this->menuEnabled = $menuEnabled; |
41
|
6 |
|
$this->registry = $registry; |
42
|
6 |
|
$this->menuConfigurations = $menuConfigurations; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
4 |
|
public function defineMenuConfiguration(MenuConfigurationEvent $event) |
46
|
|
|
{ |
47
|
4 |
|
if (!$this->menuEnabled || !key_exists($event->getMenuName(), $this->menuConfigurations)) { |
48
|
2 |
|
return; |
49
|
|
|
} |
50
|
2 |
|
$menuConfiguration = $this->menuConfigurations[$event->getMenuName()]; |
51
|
|
|
|
52
|
2 |
|
if (!is_array($menuConfiguration)) { |
53
|
2 |
|
$menuConfiguration = []; |
54
|
|
|
} |
55
|
2 |
|
$menuConfiguration = array_merge_recursive($menuConfiguration, $event->getMenuConfiguration()); |
56
|
2 |
|
$resourceNames = $this->registry->keys(); |
57
|
|
|
|
58
|
2 |
|
if (!key_exists('children', $menuConfiguration) || !is_array($menuConfiguration['children'])) { |
59
|
2 |
|
$menuConfiguration['children'] = []; |
60
|
|
|
|
61
|
2 |
|
if ('left' === $event->getMenuName()) { |
62
|
2 |
|
foreach ($resourceNames as $resourceName) { |
63
|
2 |
|
$menuConfiguration['children'][$resourceName] = []; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
foreach ($menuConfiguration['children'] as $itemName => $itemConfiguration) { |
69
|
2 |
|
if (null === $itemConfiguration) { |
70
|
|
|
$itemConfiguration = []; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// When an url is set, nothing to add, the item menu can be build |
74
|
2 |
|
if (key_exists('url', $itemConfiguration)) { |
75
|
|
|
$menuConfiguration[$itemName] = $itemConfiguration; |
76
|
|
|
|
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// If the key "admin' is missing, we try to find an admin resource with the same name |
81
|
2 |
|
if (!key_exists('admin', $itemConfiguration) && in_array($itemName, $resourceNames)) { |
82
|
2 |
|
$itemConfiguration['admin'] = $itemName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// The default admins action is list |
86
|
2 |
|
if (key_exists('admin', $itemConfiguration) && !key_exists('action', $itemConfiguration)) { |
87
|
2 |
|
$itemConfiguration['action'] = 'list'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// At this point, an pair admin/action or an url or an admin should be defined |
91
|
2 |
|
if (!key_exists('admin', $itemConfiguration)) { |
92
|
|
|
throw new Exception(sprintf( |
93
|
|
|
'The configuration of the children "%s" in the menu "%s" is invalid: no admin/action nor url configured, and no admin with the name "%s" exists', |
94
|
|
|
$itemName, |
95
|
|
|
$event->getMenuName(), |
96
|
|
|
$itemName |
97
|
|
|
)); |
98
|
|
|
} |
99
|
2 |
|
$menuConfiguration['children'][$itemName] = $itemConfiguration; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// Set defaults menu configuration to be build |
103
|
2 |
|
$event->setMenuConfiguration($menuConfiguration); |
104
|
2 |
|
} |
105
|
|
|
} |
106
|
|
|
|