app/src/EventSubscriber/AclSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Psr\Cache\CacheItemPoolInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class AclSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var AbstractAdapter
  12.      */
  13.     private $cacheAdapter;
  14.     public function __construct(CacheItemPoolInterface $cacheAdapter)
  15.     {
  16.         $this->cacheAdapter $cacheAdapter;
  17.     }
  18.     public function onKernelController(ControllerEvent $event): void
  19.     {
  20.         $controller $event->getController();
  21.         /*
  22.          * $controller passed can be either a class or a Closure.
  23.          * This is not usual in Symfony but it may happen.
  24.          * If it is a class, it comes in array format
  25.          */
  26.         if (!is_array($controller)
  27.             || $controller[0] instanceof \MainlyCode\Zf1WrapperBundle\Controller\DefaultController
  28.             || $controller[0] instanceof \Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController) {
  29.             return;
  30.         }
  31.         //$cache = $event->get('app.cache');
  32.         $controllerName $this->getControllerName($event);
  33.         $actionName $this->getActionName($event);
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [KernelEvents::CONTROLLER => 'onKernelController'];
  38.     }
  39.     private function getControllerName($event)
  40.     {
  41.         $pattern "#Controller\\\([a-zA-Z]*)Controller#";
  42.         $matches = [];
  43.         preg_match($pattern, (string) $event->getRequest()->get('_controller'), $matches);
  44.         if (isset($matches[1])) {
  45.             return $matches[1];
  46.         }
  47.     }
  48.     private function getActionName($event)
  49.     {
  50.         $params explode('::', (string) $event->getRequest()->attributes->get('_controller'));
  51.         if (isset($params[1])) {
  52.             return substr($params[1],0,-6);
  53.         }
  54.     }
  55. }