sfCacheFilter.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfCacheFilter deals with page caching and action caching.
  11. *
  12. * @package symfony
  13. * @subpackage filter
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfCacheFilter.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  16. */
  17. class sfCacheFilter extends sfFilter
  18. {
  19. protected
  20. $cacheManager = null,
  21. $request = null,
  22. $response = null,
  23. $routing = null,
  24. $cache = array();
  25. /**
  26. * Initializes this Filter.
  27. *
  28. * @param sfContext $context The current application context
  29. * @param array $parameters An associative array of initialization parameters
  30. *
  31. * @return bool true, if initialization completes successfully, otherwise false
  32. *
  33. * @throws <b>sfInitializationException</b> If an error occurs while initializing this Filter
  34. */
  35. public function initialize($context, $parameters = array())
  36. {
  37. parent::initialize($context, $parameters);
  38. $this->cacheManager = $context->getViewCacheManager();
  39. $this->request = $context->getRequest();
  40. $this->response = $context->getResponse();
  41. $this->routing = $context->getRouting();
  42. }
  43. /**
  44. * Executes this filter.
  45. *
  46. * @param sfFilterChain $filterChain A sfFilterChain instance
  47. */
  48. public function execute($filterChain)
  49. {
  50. // execute this filter only once, if cache is set and no GET or POST parameters
  51. if (!sfConfig::get('sf_cache'))
  52. {
  53. $filterChain->execute();
  54. return;
  55. }
  56. if ($this->executeBeforeExecution())
  57. {
  58. $filterChain->execute();
  59. }
  60. $this->executeBeforeRendering();
  61. }
  62. public function executeBeforeExecution()
  63. {
  64. $uri = $this->routing->getCurrentInternalUri();
  65. if (is_null($uri))
  66. {
  67. return true;
  68. }
  69. // page cache
  70. $cacheable = $this->cacheManager->isCacheable($uri);
  71. if ($cacheable && $this->cacheManager->withLayout($uri))
  72. {
  73. $inCache = $this->cacheManager->getPageCache($uri);
  74. $this->cache[$uri] = $inCache;
  75. if ($inCache)
  76. {
  77. // page is in cache, so no need to run execution filter
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. /**
  84. * Executes this filter.
  85. */
  86. public function executeBeforeRendering()
  87. {
  88. // cache only 200 HTTP status
  89. if (200 != $this->response->getStatusCode())
  90. {
  91. return;
  92. }
  93. $uri = $this->routing->getCurrentInternalUri();
  94. // save page in cache
  95. if (isset($this->cache[$uri]) && false === $this->cache[$uri])
  96. {
  97. // set some headers that deals with cache
  98. if ($lifetime = $this->cacheManager->getClientLifeTime($uri, 'page'))
  99. {
  100. $this->response->setHttpHeader('Last-Modified', $this->response->getDate(time()), false);
  101. $this->response->setHttpHeader('Expires', $this->response->getDate(time() + $lifetime), false);
  102. $this->response->addCacheControlHttpHeader('max-age', $lifetime);
  103. }
  104. // set Vary headers
  105. foreach ($this->cacheManager->getVary($uri, 'page') as $vary)
  106. {
  107. $this->response->addVaryHttpHeader($vary);
  108. }
  109. $this->cacheManager->setPageCache($uri);
  110. }
  111. // remove PHP automatic Cache-Control and Expires headers if not overwritten by application or cache
  112. if ($this->response->hasHttpHeader('Last-Modified') || sfConfig::get('sf_etag'))
  113. {
  114. // FIXME: these headers are set by PHP sessions (see session_cache_limiter())
  115. $this->response->setHttpHeader('Cache-Control', null, false);
  116. $this->response->setHttpHeader('Expires', null, false);
  117. $this->response->setHttpHeader('Pragma', null, false);
  118. }
  119. // Etag support
  120. if (sfConfig::get('sf_etag'))
  121. {
  122. $etag = '"'.md5($this->response->getContent()).'"';
  123. $this->response->setHttpHeader('ETag', $etag);
  124. if ($this->request->getHttpHeader('IF_NONE_MATCH') == $etag)
  125. {
  126. $this->response->setStatusCode(304);
  127. $this->response->setHeaderOnly(true);
  128. if (sfConfig::get('sf_logging_enabled'))
  129. {
  130. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('ETag matches If-None-Match (send 304)')));
  131. }
  132. }
  133. }
  134. // conditional GET support
  135. // never in debug mode
  136. if ($this->response->hasHttpHeader('Last-Modified') && !sfConfig::get('sf_debug'))
  137. {
  138. $last_modified = $this->response->getHttpHeader('Last-Modified');
  139. $last_modified = $last_modified[0];
  140. if ($this->request->getHttpHeader('IF_MODIFIED_SINCE') == $last_modified)
  141. {
  142. $this->response->setStatusCode(304);
  143. $this->response->setHeaderOnly(true);
  144. if (sfConfig::get('sf_logging_enabled'))
  145. {
  146. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Last-Modified matches If-Modified-Since (send 304)')));
  147. }
  148. }
  149. }
  150. }
  151. }