src/EventListener/RequestListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Utils\CacheProvider;
  4. use App\Utils\Constants;
  5. use App\Utils\Lib;
  6. use \Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\HttpFoundation\Response;
  9. class RequestListener
  10. {
  11.     protected $container;
  12.     public function __construct(ContainerInterface $container)
  13.     {
  14.         $this->container $container;
  15.     }
  16.     public function onKernelRequest(GetResponseEvent $event)
  17.     {
  18.         if (!$event->isMasterRequest()) {
  19.             // don't do anything if it's not the master request
  20.             return;
  21.         }
  22.         $request $event->getRequest();
  23.         $url $request->getRequestUri();
  24.         if (!Lib::checkValidPageUrl($url)) {
  25.             return;
  26.         }
  27.         $cacheConfig $this->container->getParameter('cache_config');
  28.         // If don't allow cache page
  29.         if ($cacheConfig['allow_cache_page'] !== 1) {
  30.             return;
  31.         }
  32.         // Get cache page
  33.         $serverCache CacheProvider::createInstance(
  34.             $request,
  35.             Constants::SERVER_CACHE_FULL_PAGE,
  36.             $cacheConfig,
  37.             Constants::SERVER_CACHE_ARTICLE_TYPE
  38.         );
  39.         $cachekey $url;
  40.         $htmlContent $serverCache->getString($cachekey);
  41.         if ($htmlContent) {
  42.             $response = new Response($htmlContent);
  43.             $response->headers->set('Content-Encoding''gzip');
  44.             $response->headers->set('X-Cache-Page''1');
  45.             $event->setResponse($response);
  46.         }
  47.     }
  48. }