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

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (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\ShipmentsBundle\EventSubscriber;
  9. use App\Event\ConfigureMenuEvent;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ConfigureMenuSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var RequestStack
  18.      */
  19.     private RequestStack $requestStack;
  20.     /**
  21.      * @var TranslatorInterface
  22.      */
  23.     private TranslatorInterface $tr;
  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 array
  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__SHIPMENTS__READ_ONLY')) {
  58.             return;
  59.         }
  60.         $menu $event->getMenu();
  61.         $menuShipmentsChilds = [
  62.             [
  63.                 'key' => 'orders',
  64.                 'label' => $this->tr->trans('Orders', [], 'ShipmentsBundle'),
  65.                 'route' => 'shipments_bundle__order_list',
  66.                 'matchPath' => 'bundles/shipments-bundle/order/'
  67.             ],
  68.             [
  69.                 'key' => 'bursts',
  70.                 'label' => $this->tr->trans('Bursts', [], 'ShipmentsBundle'),
  71.                 'route' => 'shipments_bundle__burst_list',
  72.                 'matchPath' => 'bundles/shipments-bundle/bursts/'
  73.             ],
  74.             [
  75.                 'key' => 'labels',
  76.                 'label' => $this->tr->trans('Labels', [], 'ShipmentsBundle'),
  77.                 'route' => 'shipments_bundle__label_list',
  78.                 'matchPath' => 'bundles/shipments-bundle/labels/'
  79.             ]
  80.         ];
  81.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  82.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  83.         $parentName $this->tr->trans('Shipments', [], 'ShipmentsBundle');
  84.         $menu->addChild('ShipmentsMenu')
  85.             ->setLabel($parentName)
  86.             ->setExtra('icon''fas fa-shipping-fast');
  87.         foreach ($menuShipmentsChilds as $menuShipmentsChild) {
  88.             $current $menuShipmentsChild['route'] == $currentRoute
  89.                 || strpos($currentPath$menuShipmentsChild['matchPath']) !== false
  90.             ;
  91.             $menu['ShipmentsMenu']->addChild(
  92.                 $menuShipmentsChild['key'],
  93.                 [
  94.                     'route' => $menuShipmentsChild['route'],
  95.                     'label' => $menuShipmentsChild['label'],
  96.                     'current' => $current
  97.                 ]
  98.             );
  99.         }
  100.     }
  101. }