vendor/simpledot/cms-bundle/AWCmsBundle/EventListener/CacheListener.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: albert
  5.  * Date: 28/08/18
  6.  * Time: 16:33
  7.  */
  8. namespace AWCmsBundle\EventListener;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class CacheListener implements EventSubscriberInterface
  13. {
  14.     const NO_AUTO_CACHE_CONTROL_HEADER 'Symfony-Session-NoAutoCacheControl';
  15.     private $private;
  16.     private $public;
  17.     private $maxAge;
  18.     private $mustRevalidate;
  19.     /**
  20.      * Returns an array of event names this subscriber wants to listen to.
  21.      *
  22.      * The array keys are event names and the value can be:
  23.      *
  24.      *  * The method name to call (priority defaults to 0)
  25.      *  * An array composed of the method name to call and the priority
  26.      *  * An array of arrays composed of the method names to call and respective
  27.      *    priorities, or 0 if unset
  28.      *
  29.      * For instance:
  30.      *
  31.      *  * array('eventName' => 'methodName')
  32.      *  * array('eventName' => array('methodName', $priority))
  33.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  34.      *
  35.      * @return array The event names to listen to
  36.      */
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             KernelEvents::RESPONSE => [array('saveCacheData', -999),array('putCacheData', -1001)]
  41.         ];
  42.     }
  43.     /**
  44.      * @param FilterResponseEvent $event
  45.      */
  46.     public function saveCacheData(FilterResponseEvent $event)
  47.     {
  48.         $response $event->getResponse();
  49.         if ($response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER)) {
  50.             $this->private $response->headers->getCacheControlDirective('private');
  51.             $this->public $response->headers->getCacheControlDirective('public');
  52.             $this->maxAge $response->headers->getCacheControlDirective('max-age');
  53.             $this->mustRevalidate $response->headers->getCacheControlDirective('must-revalidate');
  54.         }
  55.     }
  56.     /**
  57.      * @param FilterResponseEvent $event
  58.      */
  59.     public function putCacheData(FilterResponseEvent $event)
  60.     {
  61.         $response $event->getResponse();
  62.         if ($response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER)) {
  63.             if ($this->public || $this->private === false || $this->private === null) {
  64.                 $response->setPublic();
  65.             }
  66.             if($this->maxAge !== null) {
  67.                 $response->setMaxAge($this->maxAge);
  68.             }
  69.             if($this->mustRevalidate !== null && $this->mustRevalidate !== true) {
  70.                 $response->headers->removeCacheControlDirective('must-revalidate');
  71.             }
  72.             $response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
  73.         }
  74.     }
  75. }