src/EventSubscriber/MenuBuilderSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the AdminLTE-Bundle demo.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace App\EventSubscriber;
  9. use KevinPapst\AdminLTEBundle\Event\BreadcrumbMenuEvent;
  10. use KevinPapst\AdminLTEBundle\Event\SidebarMenuEvent;
  11. use KevinPapst\AdminLTEBundle\Model\MenuItemModel;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. /**
  15.  * Class MenuBuilder configures the main navigation.
  16.  */
  17. class MenuBuilderSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var AuthorizationCheckerInterface
  21.      */
  22.     private $security;
  23.     /**
  24.      * @param AuthorizationCheckerInterface $security
  25.      */
  26.     public function __construct(AuthorizationCheckerInterface $security)
  27.     {
  28.         $this->security $security;
  29.     }
  30.     /**
  31.      * @return array
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             SidebarMenuEvent::class => ['onSetupNavbar'100],
  37.             BreadcrumbMenuEvent::class => ['onSetupNavbar'100],
  38.         ];
  39.     }
  40.     /**
  41.      * Generate the main menu.
  42.      *
  43.      * @param SidebarMenuEvent $event
  44.      */
  45.     public function onSetupNavbar(SidebarMenuEvent $event)
  46.     {
  47.         $event->addItem(
  48.             new MenuItemModel('homepage''menu.homepage''homepage', [], 'fas fa-tachometer-alt')
  49.         );
  50.         $event->addItem(
  51.             (new MenuItemModel('forms''menu.form''forms', [], 'fab fa-wpforms'))->setBadge(1)->setBadgeColor('red')
  52.         );
  53.         $event->addItem(
  54.             new MenuItemModel('context''AdminLTE context''context', [], 'fas fa-code')
  55.         );
  56.         $demo = new MenuItemModel('demo''Demo'null, [], 'far fa-arrow-alt-circle-right');
  57.         $demo->setBadge(2);
  58.         $demo->addChild(
  59.             new MenuItemModel('sub-demo''Form - Horizontal''forms2', [], 'far fa-arrow-alt-circle-down')
  60.         )->addChild(
  61.             new MenuItemModel('sub-demo2''Form - Sidebar''forms3', [], 'far fa-arrow-alt-circle-up')
  62.         );
  63.         $event->addItem($demo);
  64.         if ($this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  65.             $event->addItem(
  66.                 new MenuItemModel('logout''menu.logout''fos_user_security_logout', [], 'fas fa-sign-out-alt')
  67.             );
  68.         } else {
  69.             $event->addItem(
  70.                 new MenuItemModel('login''menu.login''fos_user_security_login', [], 'fas fa-sign-in-alt')
  71.             );
  72.         }
  73.         $this->activateByRoute(
  74.             $event->getRequest()->get('_route'),
  75.             $event->getItems()
  76.         );
  77.     }
  78.     /**
  79.      * @param string $route
  80.      * @param MenuItemModel[] $items
  81.      */
  82.     protected function activateByRoute($route$items)
  83.     {
  84.         foreach ($items as $item) {
  85.             if ($item->hasChildren()) {
  86.                 $this->activateByRoute($route$item->getChildren());
  87.             } elseif ($item->getRoute() == $route) {
  88.                 $item->setIsActive(true);
  89.             }
  90.         }
  91.     }
  92. }