1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Subscriber; |
4
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfiguration; |
6
|
|
|
use LAG\AdminBundle\Configuration\ApplicationConfigurationStorage; |
7
|
|
|
use LAG\AdminBundle\Event\AdminEvents; |
8
|
|
|
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent; |
9
|
|
|
use LAG\AdminBundle\Event\MenuEvent; |
10
|
|
|
use LAG\AdminBundle\Factory\ConfigurationFactory; |
11
|
|
|
use LAG\AdminBundle\Factory\MenuFactory; |
12
|
|
|
use LAG\AdminBundle\Resource\ResourceCollection; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
|
16
|
|
|
class MenuSubscriber implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ApplicationConfiguration |
20
|
|
|
*/ |
21
|
|
|
private $applicationConfiguration; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var MenuFactory |
25
|
|
|
*/ |
26
|
|
|
private $menuFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ResourceCollection |
30
|
|
|
*/ |
31
|
|
|
private $resourceCollection; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ConfigurationFactory |
35
|
|
|
*/ |
36
|
|
|
private $configurationFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $adminMenuConfigurations; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var EventDispatcherInterface |
45
|
|
|
*/ |
46
|
|
|
private $eventDispatcher; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public static function getSubscribedEvents() |
52
|
|
|
{ |
53
|
|
|
return [ |
54
|
|
|
AdminEvents::MENU => 'buildMenus', |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* MenuSubscriber constructor. |
60
|
|
|
* |
61
|
|
|
* @param ApplicationConfigurationStorage $storage |
62
|
|
|
* @param MenuFactory $menuFactory |
63
|
|
|
* @param ConfigurationFactory $configurationFactory |
64
|
|
|
* @param ResourceCollection $resourceCollection |
65
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
66
|
|
|
* @param array $adminMenuConfigurations |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
69
|
|
|
ApplicationConfigurationStorage $storage, |
70
|
|
|
MenuFactory $menuFactory, |
71
|
|
|
ConfigurationFactory $configurationFactory, |
72
|
|
|
ResourceCollection $resourceCollection, |
73
|
|
|
EventDispatcherInterface $eventDispatcher, |
74
|
|
|
array $adminMenuConfigurations = [] |
75
|
|
|
) { |
76
|
|
|
$this->applicationConfiguration = $storage->getConfiguration(); |
77
|
|
|
$this->menuFactory = $menuFactory; |
78
|
|
|
$this->resourceCollection = $resourceCollection; |
79
|
|
|
$this->configurationFactory = $configurationFactory; |
80
|
|
|
$this->adminMenuConfigurations = $adminMenuConfigurations; |
81
|
|
|
$this->eventDispatcher = $eventDispatcher; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Build menus according to the given configuration. |
86
|
|
|
* |
87
|
|
|
* @param MenuEvent $event |
88
|
|
|
*/ |
89
|
|
|
public function buildMenus(MenuEvent $event) |
90
|
|
|
{ |
91
|
|
|
if (!$this->applicationConfiguration->getParameter('enable_menus')) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
$menuConfigurations = array_merge_recursive( |
95
|
|
|
$this->adminMenuConfigurations, |
96
|
|
|
$event->getMenuConfigurations() |
97
|
|
|
); |
98
|
|
|
$configurationEvent = new MenuConfigurationEvent($menuConfigurations); |
99
|
|
|
|
100
|
|
|
// Dispatch a pre-menu build event to allow dynamic configuration modifications |
101
|
|
|
$this |
102
|
|
|
->eventDispatcher |
103
|
|
|
->dispatch(AdminEvents::MENU_CONFIGURATION, $configurationEvent) |
104
|
|
|
; |
105
|
|
|
$menuConfigurations = $configurationEvent->getMenuConfigurations(); |
106
|
|
|
|
107
|
|
|
foreach ($menuConfigurations as $name => $menuConfiguration) { |
108
|
|
|
$this->menuFactory->create($name, $menuConfiguration); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.