<?php
namespace App\EventSubscriber;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class AclSubscriber implements EventSubscriberInterface
{
/**
* @var AbstractAdapter
*/
private $cacheAdapter;
public function __construct(CacheItemPoolInterface $cacheAdapter)
{
$this->cacheAdapter = $cacheAdapter;
}
public function onKernelController(ControllerEvent $event): void
{
$controller = $event->getController();
/*
* $controller passed can be either a class or a Closure.
* This is not usual in Symfony but it may happen.
* If it is a class, it comes in array format
*/
if (!is_array($controller)
|| $controller[0] instanceof \MainlyCode\Zf1WrapperBundle\Controller\DefaultController
|| $controller[0] instanceof \Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController) {
return;
}
//$cache = $event->get('app.cache');
$controllerName = $this->getControllerName($event);
$actionName = $this->getActionName($event);
}
public static function getSubscribedEvents(): array
{
return [KernelEvents::CONTROLLER => 'onKernelController'];
}
private function getControllerName($event)
{
$pattern = "#Controller\\\([a-zA-Z]*)Controller#";
$matches = [];
preg_match($pattern, (string) $event->getRequest()->get('_controller'), $matches);
if (isset($matches[1])) {
return $matches[1];
}
}
private function getActionName($event)
{
$params = explode('::', (string) $event->getRequest()->attributes->get('_controller'));
if (isset($params[1])) {
return substr($params[1],0,-6);
}
}
}