|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Bridge\KnpMenu\Builder; |
|
6
|
|
|
|
|
7
|
|
|
use Knp\Menu\FactoryInterface; |
|
8
|
|
|
use Knp\Menu\ItemInterface; |
|
9
|
|
|
use LAG\AdminBundle\Event\Events\MenuCreatedEvent; |
|
10
|
|
|
use LAG\AdminBundle\Event\Events\MenuCreateEvent; |
|
11
|
|
|
use LAG\AdminBundle\Event\MenuEvents; |
|
12
|
|
|
use LAG\AdminBundle\Metadata\Index; |
|
13
|
|
|
use LAG\AdminBundle\Request\Extractor\ParametersExtractorInterface; |
|
14
|
|
|
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; |
|
15
|
|
|
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
17
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
18
|
|
|
|
|
19
|
|
|
class TopMenuBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
private ParametersExtractorInterface $parametersExtractor, |
|
23
|
|
|
private ResourceRegistryInterface $registry, |
|
24
|
|
|
private RequestStack $requestStack, |
|
25
|
|
|
private FactoryInterface $factory, |
|
26
|
|
|
private RouteNameGeneratorInterface $routeNameGenerator, |
|
27
|
|
|
private EventDispatcherInterface $eventDispatcher, |
|
28
|
|
|
) { |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function createMenu(array $options = []): ItemInterface |
|
32
|
|
|
{ |
|
33
|
|
|
$menu = $this->factory->createItem('root', $options); |
|
34
|
|
|
$request = $this->requestStack->getMainRequest(); |
|
35
|
|
|
|
|
36
|
|
|
if (!$this->parametersExtractor->supports($request)) { |
|
|
|
|
|
|
37
|
|
|
return $menu; |
|
38
|
|
|
} |
|
39
|
|
|
$resourceName = $this->parametersExtractor->getResourceName($request); |
|
|
|
|
|
|
40
|
|
|
$operationName = $this->parametersExtractor->getOperationName($request); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
$resource = $this->registry->get($resourceName); |
|
43
|
|
|
$operation = $resource->getOperation($operationName); |
|
44
|
|
|
|
|
45
|
|
|
$this->eventDispatcher->dispatch($event = new MenuCreateEvent($menu), MenuEvents::MENU_CREATE); |
|
46
|
|
|
$this->eventDispatcher->dispatch($event = new MenuCreateEvent($event->getMenu()), sprintf( |
|
47
|
|
|
MenuEvents::NAMED_EVENT_PATTERN, |
|
48
|
|
|
'top', |
|
49
|
|
|
)); |
|
50
|
|
|
$menu = $event->getMenu(); |
|
51
|
|
|
|
|
52
|
|
|
if (!$operation instanceof Index) { |
|
53
|
|
|
return $menu; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
foreach ($operation->getListActions() as $listAction) { |
|
|
|
|
|
|
57
|
|
|
$route = $listAction->getRouteName(); |
|
58
|
|
|
|
|
59
|
|
|
if ($route === null) { |
|
60
|
|
|
$route = $this |
|
61
|
|
|
->routeNameGenerator |
|
62
|
|
|
->generateRouteName($resource, $resource->getOperation($listAction->getOperationName())) |
|
63
|
|
|
; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$menu->addChild($listAction->getLabel(), [ |
|
67
|
|
|
'route' => $route, |
|
68
|
|
|
]); |
|
69
|
|
|
} |
|
70
|
|
|
$this->eventDispatcher->dispatch(new MenuCreatedEvent($menu), MenuEvents::MENU_CREATED); |
|
71
|
|
|
$this->eventDispatcher->dispatch(new MenuCreatedEvent($menu), sprintf( |
|
72
|
|
|
MenuEvents::NAMED_EVENT_PATTERN, |
|
73
|
|
|
'top', |
|
74
|
|
|
)); |
|
75
|
|
|
|
|
76
|
|
|
return $menu; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|