1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Menu\Factory; |
6
|
|
|
|
7
|
|
|
use Knp\Menu\FactoryInterface; |
8
|
|
|
use Knp\Menu\ItemInterface; |
9
|
|
|
use LAG\AdminBundle\Event\Events\MenuEvent; |
10
|
|
|
use LAG\AdminBundle\Event\MenuEvents; |
11
|
|
|
use LAG\AdminBundle\Factory\Configuration\ConfigurationFactoryInterface; |
12
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
class MenuFactory implements MenuFactoryInterface |
15
|
|
|
{ |
16
|
|
|
private EventDispatcherInterface $eventDispatcher; |
17
|
|
|
private FactoryInterface $factory; |
18
|
|
|
private ConfigurationFactoryInterface $configurationFactory; |
19
|
|
|
private MenuItemFactoryInterface $menuItemFactory; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
EventDispatcherInterface $eventDispatcher, |
23
|
|
|
FactoryInterface $factory, |
24
|
|
|
ConfigurationFactoryInterface $configurationFactory, |
25
|
|
|
MenuItemFactoryInterface $menuItemFactory |
26
|
|
|
) { |
27
|
|
|
$this->eventDispatcher = $eventDispatcher; |
28
|
|
|
$this->factory = $factory; |
29
|
|
|
$this->configurationFactory = $configurationFactory; |
30
|
|
|
$this->menuItemFactory = $menuItemFactory; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function create(string $name, array $options = []): ItemInterface |
34
|
|
|
{ |
35
|
|
|
$menuConfiguration = $this->configurationFactory->createMenuConfiguration($name, $options)->toArray(); |
36
|
|
|
$menu = $this->factory->createItem('root', [ |
37
|
|
|
'attributes' => $menuConfiguration['attributes'], |
38
|
|
|
'extras' => $menuConfiguration['extras'], |
39
|
|
|
]); |
40
|
|
|
$event = new MenuEvent($name, $menu); |
41
|
|
|
$this->eventDispatcher->dispatch($event, MenuEvents::MENU_CREATE); |
42
|
|
|
$this->eventDispatcher->dispatch($event, sprintf(MenuEvents::MENU_CREATE_SPECIFIC, $name)); |
43
|
|
|
$children = $menuConfiguration['children'] ?? []; |
44
|
|
|
|
45
|
|
|
foreach ($children as $child) { |
46
|
|
|
$child = $this->menuItemFactory->create($child['text'], $child); |
47
|
|
|
$menu->addChild($child); |
48
|
|
|
} |
49
|
|
|
$this->eventDispatcher->dispatch($event, MenuEvents::MENU_CREATED); |
50
|
|
|
$this->eventDispatcher->dispatch($event, sprintf(MenuEvents::MENU_CREATED_SPECIFIC, $name)); |
51
|
|
|
|
52
|
|
|
return $menu; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|