bundles/sanity-bundle/EventListener/ResponseListener.php line 57

Open in your IDE?
  1. <?php
  2. namespace GC\SanityBundle\EventListener;
  3. use GC\SanityBundle\ESIAdapter;
  4. use GC\SanityBundle\ControllerResult;
  5. use GC\SanityBundle\Configuration\ExplicitHTTPCacheLayer;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\HttpKernel\Event\ViewEvent;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class ResponseListener implements EventSubscriberInterface
  13. {
  14.     protected $esi;
  15.     
  16.     protected $httpCacheLayer;
  17.     
  18.     protected $lastModified;
  19.     public function __constructESIAdapter $esi )
  20.     {
  21.         $this->esi $esi;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return array(
  26.             KernelEvents::VIEW => array('onKernelView'128),
  27.             KernelEvents::RESPONSE => array('onKernelResponse'),
  28.         );
  29.     }
  30.     /**
  31.      * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
  32.      */
  33.     public function onKernelView(ViewEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         $controllerResult $event->getControllerResult();
  37.         
  38.         $this->httpCacheLayer $request->attributes->get('_http_cache_layer');
  39.                 
  40.         if($controllerResult instanceOf ControllerResult){    
  41.         
  42.             $this->lastModified $controllerResult->getLatestDateTime();
  43.             
  44.             if( !$this->httpCacheLayer )
  45.                 $this->httpCacheLayer = new ExplicitHTTPCacheLayer([]);
  46.                 
  47.             if( !$controllerResult->hasResult() ){
  48.             
  49.                 if( $controllerResult->throwOnNoResult() ) 
  50.                     throw new NotFoundHttpException;
  51.                     
  52.                 if( $controllerResult->emptyResponseOnNoResult() )
  53.                     $event->setResponse( new Response );
  54.                     
  55.                 return;
  56.             }
  57.             
  58.         } else if ($this->httpCacheLayer) { // just a twig template, use git date or default
  59.         
  60.             $this->lastModified $this->esi->getBaseLastModified();
  61.         
  62.         }
  63.         
  64.         if( $this->lastModified ) {
  65.             
  66.             if( $this->esi->getProductionMode() ){
  67.                 
  68.                 $response = new Response;
  69.                 $response->setLastModified$this->lastModified );
  70.                 
  71.                 if ($response->isNotModified$request )){
  72.                     $event->setResponse$response );
  73.                     return;
  74.                 }    
  75.             } 
  76.             
  77.             if($controllerResult instanceOf ControllerResult){ // fetch and hydrate the objects
  78.     
  79.                 $result $controllerResult->getResult();
  80.                 
  81.                 $event->setControllerResult($result + [
  82.                     '_controller_result' => [
  83.                         'name' => $controllerResult->getName(),
  84.                         'last_modified' => $this->lastModified,
  85.                         'result' => $result
  86.                     ]
  87.                 ]);    
  88.             }
  89.         }        
  90.     }
  91.      
  92.     /**
  93.      * @param FilterResponseEvent $event A FilterResponseEvent instance
  94.      */
  95.     public function onKernelResponse(ResponseEvent $event)
  96.     {
  97.         if($this->lastModified && $this->httpCacheLayer){
  98.             
  99.             $response $event->getResponse();
  100.             $response->setLastModified$this->lastModified );
  101.             $response->headers->set('x-esi-last-modified'$this->lastModified->format('r'));
  102.             
  103.             if( $this->esi->getProductionMode() ){
  104.                 
  105.                 $maxAge $this->httpCacheLayer->getMaxAge();
  106.                 
  107.                 if(!is_null($maxAge))
  108.                     $response->setMaxAge$maxAge );
  109.                 
  110.                 $response->setSharedMaxAge$this->httpCacheLayer->getSharedMaxAge() );
  111.             }
  112.         }
  113.     }
  114. }