vendor/sulu/sulu/src/Sulu/Bundle/HttpCacheBundle/Cache/SuluHttpCache.php line 84

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\Cache;
  11. use FOS\HttpCache\SymfonyCache\CacheInvalidation;
  12. use FOS\HttpCache\SymfonyCache\CleanupCacheTagsListener;
  13. use FOS\HttpCache\SymfonyCache\CustomTtlListener;
  14. use FOS\HttpCache\SymfonyCache\DebugListener;
  15. use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
  16. use FOS\HttpCache\SymfonyCache\PurgeListener;
  17. use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
  18. use FOS\HttpCache\TagHeaderFormatter\TagHeaderFormatter;
  19. use Sulu\Bundle\WebsiteBundle\EventListener\SegmentCacheListener;
  20. use Sulu\Component\HttpKernel\SuluKernel;
  21. use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
  26. use Symfony\Component\HttpKernel\HttpKernelInterface;
  27. use Toflar\Psr6HttpCacheStore\Psr6Store;
  28. /**
  29.  * Abstract class to extend from when using Symfony cache.
  30.  * Add needed subscriber in the constructor.
  31.  */
  32. class SuluHttpCache extends HttpCache implements CacheInvalidation
  33. {
  34.     use EventDispatchingHttpCache;
  35.     public const HEADER_REVERSE_PROXY_TTL 'X-Reverse-Proxy-TTL';
  36.     /**
  37.      * @param string $cacheDir
  38.      */
  39.     public function __construct(HttpKernelInterface $kernel$cacheDir null)
  40.     {
  41.         if (!$cacheDir && $kernel instanceof SuluKernel) {
  42.             $cacheDir $kernel->getCommonCacheDir() . \DIRECTORY_SEPARATOR 'http_cache';
  43.         }
  44.         parent::__construct($kernel$cacheDir);
  45.         foreach ($this->getSubscribers() as $subscriber) {
  46.             $this->addSubscriber($subscriber);
  47.         }
  48.     }
  49.     /**
  50.      * @return EventSubscriberInterface[]
  51.      */
  52.     protected function getSubscribers(): array
  53.     {
  54.         $subscribers = [
  55.             CustomTtlListener::class => new CustomTtlListener(static::HEADER_REVERSE_PROXY_TTL),
  56.             PurgeListener::class => new PurgeListener(),
  57.             PurgeTagsListener::class => new PurgeTagsListener(),
  58.             SegmentCacheListener::class => new SegmentCacheListener(),
  59.         ];
  60.         if ($this->kernel->isDebug()) {
  61.             $subscribers[DebugListener::class] = new DebugListener();
  62.         } else {
  63.             $subscribers[CleanupCacheTagsListener::class] = new CleanupCacheTagsListener();
  64.         }
  65.         return $subscribers;
  66.     }
  67.     /**
  68.      * Made public to allow event listeners to do refresh operations.
  69.      *
  70.      * {@inheritdoc}
  71.      */
  72.     public function fetch(Request $request$catch false): Response
  73.     {
  74.         return parent::fetch($request$catch);
  75.     }
  76.     protected function createStore(): StoreInterface
  77.     {
  78.         return new Psr6Store([
  79.             'cache_directory' => $this->cacheDir,
  80.             'cache_tags_header' => TagHeaderFormatter::DEFAULT_HEADER_NAME,
  81.         ]);
  82.     }
  83. }