|
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\Metadata\AdminResource; |
|
10
|
|
|
use LAG\AdminBundle\Metadata\OperationInterface; |
|
11
|
|
|
use LAG\AdminBundle\Request\Extractor\ParametersExtractorInterface; |
|
12
|
|
|
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; |
|
13
|
|
|
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
16
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
17
|
|
|
|
|
18
|
|
|
class ContextualMenuBuilder extends AbstractMenuBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
public function __construct( |
|
21
|
|
|
private ParametersExtractorInterface $parametersExtractor, |
|
22
|
|
|
private ResourceRegistryInterface $registry, |
|
23
|
|
|
private RequestStack $requestStack, |
|
24
|
|
|
private RouteNameGeneratorInterface $routeNameGenerator, |
|
25
|
|
|
FactoryInterface $factory, |
|
26
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
27
|
|
|
) { |
|
28
|
|
|
parent::__construct($factory, $eventDispatcher); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getName(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return 'contextual'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function buildMenu(ItemInterface $menu): void |
|
37
|
|
|
{ |
|
38
|
|
|
$request = $this->requestStack->getMainRequest(); |
|
39
|
|
|
|
|
40
|
|
|
if (!$this->parametersExtractor->supports($request)) { |
|
|
|
|
|
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
$resource = $this->getResource($request); |
|
|
|
|
|
|
44
|
|
|
$operation = $this->getOperation($resource, $request); |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
foreach ($operation->getContextualActions() as $action) { |
|
47
|
|
|
$contextualResource = $resource; |
|
48
|
|
|
|
|
49
|
|
|
if ($action->getResourceName() !== $resource->getName()) { |
|
50
|
|
|
$contextualResource = $this->registry->get($action->getResourceName()); |
|
51
|
|
|
} |
|
52
|
|
|
$contextualOperation = $contextualResource->getOperation($action->getOperationName()); |
|
53
|
|
|
$options = []; |
|
54
|
|
|
|
|
55
|
|
|
if ($action->getUrl()) { |
|
56
|
|
|
$options['url'] = $action->getUrl(); |
|
57
|
|
|
} elseif ($action->getRoute()) { |
|
58
|
|
|
$options['route'] = $action->getRoute(); |
|
59
|
|
|
} else { |
|
60
|
|
|
$options['route'] = $this |
|
61
|
|
|
->routeNameGenerator |
|
62
|
|
|
->generateRouteName($resource, $contextualOperation) |
|
63
|
|
|
; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($action->getIcon()) { |
|
67
|
|
|
$options['extras']['icon'] = $action->getIcon(); |
|
68
|
|
|
} |
|
69
|
|
|
$menu->addChild($action->getLabel(), $options); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function getResource(Request $request): AdminResource |
|
74
|
|
|
{ |
|
75
|
|
|
$resourceName = $this->parametersExtractor->getResourceName($request); |
|
76
|
|
|
|
|
77
|
|
|
return $this->registry->get($resourceName); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function getOperation(AdminResource $resource, Request $request): OperationInterface |
|
81
|
|
|
{ |
|
82
|
|
|
$operationName = $this->parametersExtractor->getOperationName($request); |
|
83
|
|
|
|
|
84
|
|
|
return $resource->getOperation($operationName); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|