vendor/bluue/customers-bundle/src/EventSubscriber/ConfigureMenuSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\CustomersBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class ConfigureMenuSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var TranslatorInterface
  18.      */
  19.     private TranslatorInterface $tr;
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     protected RequestStack $requestStack;
  24.     /**
  25.      * @var Security
  26.      */
  27.     private Security $security;
  28.     /**
  29.      * @param RequestStack $requestStack
  30.      * @param TranslatorInterface $tr
  31.      * @param Security $security
  32.      */
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         TranslatorInterface $tr,
  36.         Security $security
  37.     ) {
  38.         $this->requestStack $requestStack;
  39.         $this->tr $tr;
  40.         $this->security $security;
  41.     }
  42.     /**
  43.      * @return string[]
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             ConfigureMenuEvent::NAME => 'onLoad'
  49.         ];
  50.     }
  51.     /**
  52.      * @param ConfigureMenuEvent $event
  53.      * @return void
  54.      */
  55.     public function onLoad(ConfigureMenuEvent $event): void
  56.     {
  57.         if (!$this->security->isGranted('ROLE__CUSTOMERS__READ_ONLY')) {
  58.             return;
  59.         }
  60.         $menu $event->getMenu();
  61.         $menuCustomersChilds = [
  62.             [
  63.                 'key' => 'customers',
  64.                 'label' => $this->tr->trans('Customers', [], 'CustomersBundle'),
  65.                 'route' => 'customers_bundle__customer_list',
  66.                 'matchPath' => [
  67.                     'bundles/customers-bundle/view',
  68.                     'bundles/customers-bundle/edit',
  69.                     'bundles/customers-bundle/new'
  70.                 ]
  71.             ]
  72.         ];
  73.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  74.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  75.         $parentName $this->tr->trans('Customers', [], 'CustomersBundle');
  76.         $menu->addChild('CustomersMenu')
  77.             ->setLabel($parentName)
  78.             ->setExtra('icon''fas fa-users');
  79.         foreach ($menuCustomersChilds as $menuCustomersChild) {
  80.             $current false;
  81.             if ($menuCustomersChild['route'] == $currentRoute) {
  82.                 $current true;
  83.             } else {
  84.                 foreach ($menuCustomersChild['matchPath'] as $matchPath) {
  85.                     if (strpos($currentPath$matchPath) !== false) {
  86.                         $current true;
  87.                     }
  88.                 }
  89.             }
  90.             $menu['CustomersMenu']->addChild(
  91.                 $menuCustomersChild['key'],
  92.                 [
  93.                     'route' => $menuCustomersChild['route'],
  94.                     'label' => $menuCustomersChild['label'],
  95.                     'current' => $current
  96.                 ]
  97.             );
  98.         }
  99.     }
  100. }