vendor/bluue/products-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\ProductsBundle\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 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 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__CATALOG__READ_ONLY')) {
  58.             return;
  59.         }
  60.         $menu $event->getMenu();
  61.         $menuCatalogChilds = [
  62.             [
  63.                 'key' => 'brands',
  64.                 'label' => $this->tr->trans('Brands', [], 'ProductsBundle'),
  65.                 'route' => 'products_bundle__brand_list',
  66.             ],
  67.             [
  68.                 'key' => 'attribute_groups',
  69.                 'label' => $this->tr->trans('Attribute groups', [], 'ProductsBundle'),
  70.                 'route' => 'products_bundle__attribute_group_list',
  71.                 'matchPath' => 'bundles/products-bundle/attribute'
  72.             ],
  73.             [
  74.                 'key' => 'feature',
  75.                 'label' => $this->tr->trans('Features', [], 'ProductsBundle'),
  76.                 'route' => 'products_bundle__feature_list',
  77.                 'matchPath' => 'bundles/products-bundle/feature'
  78.             ],
  79.             [
  80.                 'key' => 'product',
  81.                 'label' => $this->tr->trans('Products', [], 'ProductsBundle'),
  82.                 'route' => 'products_bundle__product_list',
  83.                 'matchPath' => 'bundles/products-bundle/product'
  84.             ]
  85.         ];
  86.         $currentPath $this->requestStack->getCurrentRequest()->getPathInfo();
  87.         $currentRoute $this->requestStack->getCurrentRequest()->attributes->get('_route');
  88.         $catalogTranslate $this->tr->trans('Catalog', [], 'ProductsBundle');
  89.         $menu->addChild('CatalogMenu')
  90.             ->setLabel($catalogTranslate)
  91.             ->setExtra('icon''fas fa-store');
  92.         foreach ($menuCatalogChilds as $menuCatalogChild) {
  93.             $current $menuCatalogChild['route'] == $currentRoute
  94.                 || (
  95.                     !empty($menuCatalogChild['matchPath'])
  96.                     && strpos($currentPath$menuCatalogChild['matchPath']) !== false
  97.                 )
  98.             ;
  99.             $menu['CatalogMenu']->addChild(
  100.                 $menuCatalogChild['key'],
  101.                 [
  102.                     'route' => $menuCatalogChild['route'],
  103.                     'label' => $menuCatalogChild['label'],
  104.                     'current' => $current
  105.                 ]
  106.             );
  107.         }
  108.     }
  109. }