Repository navigation
Expand file tree
/
Copy pathMenuController.php
More file actions
69 lines (60 loc) · 1.96 KB
/
Copy pathMenuController.php
File metadata and controls
69 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace SyncEngine\Controller;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use SyncEngine\Attribute\MenuItem;
use SyncEngine\Service\Menu\MenuLoader;
class MenuController extends DefaultController
{
public function __construct(
private Security $security,
) {}
#[Route( '/json/menu/{menuName}', name: 'json_menu' )]
public function jsonMenu( string $menuName, MenuLoader $loader ): JsonResponse
{
return $this->json( $this->fetchMenuItems( $menuName, $loader ) );
}
public function renderMainMenu( string $currentPath, MenuLoader $loader ): Response
{
$items = $this->fetchMenuItems( 'main', $loader );
return $this->render(
'admin/_partials/menu.twig',
[
'groups' => [ 'dashboards', 'entities', 'system' ],
'items' => $items,
'currentPath' => $currentPath,
]
);
}
/**
* @return array{name: string, link: string, title: string, icon: string, parent: string, position: float}[]
*/
public function fetchMenuItems( string $menuName, MenuLoader $loader ): array
{
$items = [];
$isAdmin = $this->security->isGranted( 'ROLE_ADMIN' );
// @todo filter according to the security
foreach ( $loader->getMenuItems( $menuName )->all() as $item ) {
/** @var MenuItem $item */
try {
$link = $this->generateUrl( $item->getRoute(), $item->getParameters() );
} catch ( \Exception $e ) {
// @todo Log this error when debug is enabled?
$link = '';
}
if ( $link && ( $isAdmin || ! str_contains( $link, "system" ) ) ) {
$items[] = [
'name' => $item->getRoute(),
'link' => $link,
'title' => $this->trans( $item->getLabel() ),
'icon' => $item->getIcon(),
'parent' => $item->getParent() ?: 'system',
'position' => (float) ( is_numeric( $item->getPosition() ) ? $item->getPosition() : 10 ),
];
}
}
return $items;
}
}