vendor/sonata-project/block-bundle/src/Cache/HttpCacheHandler.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sonata Project package.
  4.  *
  5.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Sonata\BlockBundle\Cache;
  11. use Sonata\BlockBundle\Block\BlockContextInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  14. class HttpCacheHandler implements HttpCacheHandlerInterface
  15. {
  16.     /**
  17.      * @var int|null
  18.      */
  19.     protected $currentTtl null;
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     public function alterResponse(Response $response)
  24.     {
  25.         if (!$response->isCacheable()) {
  26.             // the controller flags the response as private so we keep it private!
  27.             return;
  28.         }
  29.         // no block has been rendered
  30.         if (null === $this->currentTtl) {
  31.             return;
  32.         }
  33.         // a block has a lower ttl that the current response, so we update the ttl to match
  34.         // the one provided in the block
  35.         if ($this->currentTtl $response->getTtl()) {
  36.             $response->setTtl($this->currentTtl);
  37.         }
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function updateMetadata(Response $responseBlockContextInterface $blockContext null)
  43.     {
  44.         if (null === $this->currentTtl) {
  45.             $this->currentTtl $response->getTtl();
  46.         }
  47.         if (null !== $response->isCacheable() && $response->getTtl() < $this->currentTtl) {
  48.             $this->currentTtl $response->getTtl();
  49.         }
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function onKernelResponse(FilterResponseEvent $event)
  55.     {
  56.         $this->alterResponse($event->getResponse());
  57.     }
  58. }