vendor/sulu/sulu/src/Sulu/Bundle/HttpCacheBundle/CacheLifetime/CacheLifetimeEnhancer.php line 81

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\HttpCacheBundle\CacheLifetime;
  11. use Sulu\Bundle\HttpCacheBundle\Cache\SuluHttpCache;
  12. use Sulu\Component\Content\Compat\PageInterface;
  13. use Sulu\Component\Content\Compat\StructureInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. class CacheLifetimeEnhancer implements CacheLifetimeEnhancerInterface
  16. {
  17.     /**
  18.      * @var CacheLifetimeResolverInterface
  19.      */
  20.     private $cacheLifetimeResolver;
  21.     /**
  22.      * @var int
  23.      */
  24.     private $maxAge;
  25.     /**
  26.      * @var int
  27.      */
  28.     private $sharedMaxAge;
  29.     /**
  30.      * @var CacheLifetimeRequestStore
  31.      */
  32.     private $cacheLifetimeRequestStore;
  33.     public function __construct(
  34.         CacheLifetimeResolverInterface $cacheLifetimeResolver,
  35.         $maxAge,
  36.         $sharedMaxAge,
  37.         CacheLifetimeRequestStore $cacheLifetimeRequestStore
  38.     ) {
  39.         $this->cacheLifetimeResolver $cacheLifetimeResolver;
  40.         $this->maxAge $maxAge;
  41.         $this->sharedMaxAge $sharedMaxAge;
  42.         $this->cacheLifetimeRequestStore $cacheLifetimeRequestStore;
  43.     }
  44.     public function enhance(Response $responseStructureInterface $structure)
  45.     {
  46.         if (!$structure instanceof PageInterface) {
  47.             return;
  48.         }
  49.         $cacheLifetimeData $structure->getCacheLifeTime();
  50.         $cacheLifetime $this->cacheLifetimeResolver->resolve(
  51.             $cacheLifetimeData['type'],
  52.             $cacheLifetimeData['value']
  53.         );
  54.         $requestCacheLifetime $this->cacheLifetimeRequestStore->getCacheLifetime();
  55.         if (null !== $requestCacheLifetime && $requestCacheLifetime $cacheLifetime) {
  56.             $cacheLifetime $requestCacheLifetime;
  57.         }
  58.         // when structure cache-lifetime disabled - return
  59.         if (=== $cacheLifetime) {
  60.             return;
  61.         }
  62.         $response->setPublic();
  63.         $response->setMaxAge($this->maxAge);
  64.         $response->setSharedMaxAge($this->sharedMaxAge);
  65.         // set reverse-proxy TTL (Symfony HttpCache, Varnish, ...) to avoid caching of intermediate proxies
  66.         $response->headers->set(SuluHttpCache::HEADER_REVERSE_PROXY_TTL$cacheLifetime);
  67.     }
  68. }