HttpCache.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. abstract class HttpCache extends BaseHttpCache
  23. {
  24. protected $cacheDir;
  25. protected $kernel;
  26. /**
  27. * @param KernelInterface $kernel A KernelInterface instance
  28. * @param string $cacheDir The cache directory (default used if null)
  29. */
  30. public function __construct(KernelInterface $kernel, $cacheDir = null)
  31. {
  32. $this->kernel = $kernel;
  33. $this->cacheDir = $cacheDir;
  34. parent::__construct($kernel, $this->createStore(), $this->createSurrogate(), array_merge(array('debug' => $kernel->isDebug()), $this->getOptions()));
  35. }
  36. /**
  37. * Forwards the Request to the backend and returns the Response.
  38. *
  39. * @param Request $request A Request instance
  40. * @param bool $raw Whether to catch exceptions or not
  41. * @param Response $entry A Response instance (the stale entry if present, null otherwise)
  42. *
  43. * @return Response A Response instance
  44. */
  45. protected function forward(Request $request, $raw = false, Response $entry = null)
  46. {
  47. $this->getKernel()->boot();
  48. $container = $this->getKernel()->getContainer();
  49. $container->set('cache', $this);
  50. $container->set($this->getSurrogate()->getName(), $this->getSurrogate());
  51. return parent::forward($request, $raw, $entry);
  52. }
  53. /**
  54. * Returns an array of options to customize the Cache configuration.
  55. *
  56. * @return array An array of options
  57. */
  58. protected function getOptions()
  59. {
  60. return array();
  61. }
  62. protected function createSurrogate()
  63. {
  64. return new Esi();
  65. }
  66. /**
  67. * Creates new ESI instance.
  68. *
  69. * @return Esi
  70. *
  71. * @deprecated since version 2.6, to be removed in 3.0. Use createSurrogate() instead
  72. */
  73. protected function createEsi()
  74. {
  75. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.6 and will be removed in 3.0. Use createSurrogate() instead.', E_USER_DEPRECATED);
  76. return $this->createSurrogate();
  77. }
  78. protected function createStore()
  79. {
  80. return new Store($this->cacheDir ?: $this->kernel->getCacheDir().'/http_cache');
  81. }
  82. }