src/Listener/CacheListener.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Entity\User;
  4. use App\Services\CacheService;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\KernelEvent;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  11. use Symfony\Component\Security\Core\Security;
  12. class CacheListener
  13. {
  14.     /**
  15.      * @var CacheService
  16.      */
  17.     private $cacheService;
  18.     /**
  19.      * @var array
  20.      */
  21.     private $config;
  22.     /**
  23.      * @var Security
  24.      */
  25.     private $security;
  26.     /**
  27.      * @var array
  28.      */
  29.     private $cacheable = [];
  30.     /**
  31.      * CacheListener constructor.
  32.      * @param CacheService $cacheService
  33.      * @param array $config
  34.      * @param Security $security
  35.      */
  36.     public function __construct(CacheService $cacheService, array $configSecurity $security)
  37.     {
  38.         $this->cacheService $cacheService;
  39.         $this->config $config;
  40.         $this->security $security;
  41.         foreach ($this->config as $key => $item) {
  42.             if ($item === null) {
  43.                 $this->config[$key] = [];
  44.             }
  45.         }
  46.     }
  47.     /**
  48.      * Overwrites principal
  49.      * @param string $route
  50.      * @return array
  51.      */
  52.     private function getRouteConfig(string $route)
  53.     {
  54.         return array_merge($this->config['config'], $this->config[$route]);
  55.     }
  56.     public function onKernelRequest(RequestEvent $event)
  57.     {
  58.         $route $event->getRequest()->get('_route');
  59.         if (isset($this->config[$route])) {
  60.             $_config $this->getRouteConfig($route);
  61.             $user $this->security->getUser();
  62.             if (isset($_config['roles']) || isset($_config['reverse_roles'])) {
  63.                 $roles = ($user instanceof User) ? $user->getRoles() : [];
  64.                 $inclusions $_config['roles'] ?? [];
  65.                 $exclusions $_config['reverse_roles'] ?? [];
  66.                 //if the route has an inclusive roles list or an exclusive roles list
  67.                 //if inclusions are detected and the role is not in the list
  68.                 //or if it has exclusions and the role is in the list
  69.                 if ((!empty($inclusions) && empty(array_intersect($roles$inclusions))) ||
  70.                     (!empty($exclusions) && !empty(array_intersect($roles$exclusions)))) {
  71.                     return;
  72.                 }
  73.             }
  74.             $content $this->cacheService->getCacheRoute($route$_config$event->getRequest());
  75.             if (!empty($content)) {
  76.                 $response = new JsonResponse($content200, [], true);
  77.                 $event->setResponse($response);
  78.                 $event->stopPropagation();
  79.             } else {
  80.                 $this->cacheable[$route] = $_config;
  81.             }
  82.         }
  83.     }
  84.     public function onKernelResponse(ResponseEvent $event)
  85.     {
  86.         $route $event->getRequest()->get('_route''empty');
  87.         if (isset($this->cacheable[$route])) {
  88.             $this->cacheService->cacheRoute($route$this->cacheable[$route], $event->getRequest(), $event->getResponse());
  89.         }
  90.     }
  91. }