HttpCache.php 3.2 KB

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