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

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\StructureInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class CacheLifetimeEnhancer implements CacheLifetimeEnhancerInterface
  15. {
  16.     /**
  17.      * @var CacheLifetimeResolverInterface
  18.      */
  19.     private $cacheLifetimeResolver;
  20.     /**
  21.      * @var int
  22.      */
  23.     private $maxAge;
  24.     /**
  25.      * @var int
  26.      */
  27.     private $sharedMaxAge;
  28.     /**
  29.      * @var CacheLifetimeRequestStore
  30.      */
  31.     private $cacheLifetimeRequestStore;
  32.     public function __construct(
  33.         CacheLifetimeResolverInterface $cacheLifetimeResolver,
  34.         $maxAge,
  35.         $sharedMaxAge,
  36.         CacheLifetimeRequestStore $cacheLifetimeRequestStore
  37.     ) {
  38.         $this->cacheLifetimeResolver $cacheLifetimeResolver;
  39.         $this->maxAge $maxAge;
  40.         $this->sharedMaxAge $sharedMaxAge;
  41.         $this->cacheLifetimeRequestStore $cacheLifetimeRequestStore;
  42.     }
  43.     public function enhance(Response $responseStructureInterface $structure)
  44.     {
  45.         if (!\method_exists($structure'getCacheLifeTime')) {
  46.             return;
  47.         }
  48.         $cacheLifetimeData $structure->getCacheLifeTime();
  49.         $cacheLifetime $this->cacheLifetimeResolver->resolve(
  50.             $cacheLifetimeData['type'],
  51.             $cacheLifetimeData['value']
  52.         );
  53.         $requestCacheLifetime $this->cacheLifetimeRequestStore->getCacheLifetime();
  54.         if (null !== $requestCacheLifetime && $requestCacheLifetime $cacheLifetime) {
  55.             $cacheLifetime $requestCacheLifetime;
  56.         }
  57.         // when structure cache-lifetime disabled - return
  58.         if (=== $cacheLifetime) {
  59.             return;
  60.         }
  61.         $response->setPublic();
  62.         $response->setMaxAge($this->maxAge);
  63.         $response->setSharedMaxAge($this->sharedMaxAge);
  64.         // set reverse-proxy TTL (Symfony HttpCache, Varnish, ...) to avoid caching of intermediate proxies
  65.         $response->headers->set(SuluHttpCache::HEADER_REVERSE_PROXY_TTL$cacheLifetime);
  66.     }
  67. }