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

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\KernelInterface;
  17. /**
  18.  * Manages HTTP cache objects in a Container.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  */
  22. class HttpCache extends BaseHttpCache
  23. {
  24.     protected $cacheDir;
  25.     protected $kernel;
  26.     /**
  27.      * @param string $cacheDir The cache directory (default used if null)
  28.      */
  29.     public function __construct(KernelInterface $kernelstring $cacheDir null)
  30.     {
  31.         $this->kernel $kernel;
  32.         $this->cacheDir $cacheDir;
  33.         $isDebug $kernel->isDebug();
  34.         $options = ['debug' => $isDebug];
  35.         if ($isDebug) {
  36.             $options['stale_if_error'] = 0;
  37.         }
  38.         parent::__construct($kernel$this->createStore(), $this->createSurrogate(), array_merge($options$this->getOptions()));
  39.     }
  40.     /**
  41.      * Forwards the Request to the backend and returns the Response.
  42.      *
  43.      * @param bool     $raw   Whether to catch exceptions or not
  44.      * @param Response $entry A Response instance (the stale entry if present, null otherwise)
  45.      *
  46.      * @return Response A Response instance
  47.      */
  48.     protected function forward(Request $request$raw falseResponse $entry null)
  49.     {
  50.         $this->getKernel()->boot();
  51.         $this->getKernel()->getContainer()->set('cache'$this);
  52.         return parent::forward($request$raw$entry);
  53.     }
  54.     /**
  55.      * Returns an array of options to customize the Cache configuration.
  56.      *
  57.      * @return array An array of options
  58.      */
  59.     protected function getOptions()
  60.     {
  61.         return [];
  62.     }
  63.     protected function createSurrogate()
  64.     {
  65.         return new Esi();
  66.     }
  67.     protected function createStore()
  68.     {
  69.         return new Store($this->cacheDir ?: $this->kernel->getCacheDir().'/http_cache');
  70.     }
  71. }