vendor/symfony/framework-bundle/HttpCache/HttpCache.php line 70

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bundle\FrameworkBundle\HttpCache;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\HttpCache\Esi;
  14. use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
  15. use Symfony\Component\HttpKernel\HttpCache\Store;
  16. use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
  17. use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
  18. use Symfony\Component\HttpKernel\KernelInterface;
  19. /**
  20.  * Manages HTTP cache objects in a Container.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  */
  24. class HttpCache extends BaseHttpCache
  25. {
  26.     protected $cacheDir;
  27.     protected $kernel;
  28.     private $store;
  29.     private $surrogate;
  30.     private $options;
  31.     /**
  32.      * @param string|StoreInterface $cache The cache directory (default used if null) or the storage instance
  33.      */
  34.     public function __construct(KernelInterface $kernel$cache nullSurrogateInterface $surrogate null, array $options null)
  35.     {
  36.         $this->kernel $kernel;
  37.         $this->surrogate $surrogate;
  38.         $this->options $options ?? [];
  39.         if ($cache instanceof StoreInterface) {
  40.             $this->store $cache;
  41.         } elseif (null !== $cache && !\is_string($cache)) {
  42.             throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a string or a SurrogateInterface, "%s" given.'__METHOD__get_debug_type($cache)));
  43.         } else {
  44.             $this->cacheDir $cache;
  45.         }
  46.         if (null === $options && $kernel->isDebug()) {
  47.             $this->options = ['debug' => true];
  48.         }
  49.         if ($this->options['debug'] ?? false) {
  50.             $this->options += ['stale_if_error' => 0];
  51.         }
  52.         parent::__construct($kernel$this->createStore(), $this->createSurrogate(), array_merge($this->options$this->getOptions()));
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     protected function forward(Request $requestbool $catch falseResponse $entry null)
  58.     {
  59.         $this->getKernel()->boot();
  60.         $this->getKernel()->getContainer()->set('cache'$this);
  61.         return parent::forward($request$catch$entry);
  62.     }
  63.     /**
  64.      * Returns an array of options to customize the Cache configuration.
  65.      *
  66.      * @return array
  67.      */
  68.     protected function getOptions()
  69.     {
  70.         return [];
  71.     }
  72.     /**
  73.      * @return SurrogateInterface
  74.      */
  75.     protected function createSurrogate()
  76.     {
  77.         return $this->surrogate ?? new Esi();
  78.     }
  79.     /**
  80.      * @return StoreInterface
  81.      */
  82.     protected function createStore()
  83.     {
  84.         return $this->store ?? new Store($this->cacheDir ?: $this->kernel->getCacheDir().'/http_cache');
  85.     }
  86. }