sfCacheFilter.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 13564 2008-11-30 21:34:25Z fabien $
  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. // register our cache configuration
  65. $this->cacheManager->registerConfiguration($this->context->getModuleName());
  66. $uri = $this->routing->getCurrentInternalUri();
  67. if (is_null($uri))
  68. {
  69. return true;
  70. }
  71. // page cache
  72. $cacheable = $this->cacheManager->isCacheable($uri);
  73. if ($cacheable && $this->cacheManager->withLayout($uri))
  74. {
  75. $inCache = $this->cacheManager->getPageCache($uri);
  76. $this->cache[$uri] = $inCache;
  77. if ($inCache)
  78. {
  79. // page is in cache, so no need to run execution filter
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. /**
  86. * Executes this filter.
  87. */
  88. public function executeBeforeRendering()
  89. {
  90. // cache only 200 HTTP status
  91. if (200 != $this->response->getStatusCode())
  92. {
  93. return;
  94. }
  95. $uri = $this->routing->getCurrentInternalUri();
  96. // save page in cache
  97. if (isset($this->cache[$uri]) && false === $this->cache[$uri])
  98. {
  99. $this->setCacheExpiration($uri);
  100. $this->setCacheValidation($uri);
  101. // set Vary headers
  102. foreach ($this->cacheManager->getVary($uri, 'page') as $vary)
  103. {
  104. $this->response->addVaryHttpHeader($vary);
  105. }
  106. $this->cacheManager->setPageCache($uri);
  107. }
  108. // cache validation
  109. $this->checkCacheValidation();
  110. }
  111. /**
  112. * Sets cache expiration headers.
  113. *
  114. * @param string An internal URI
  115. */
  116. protected function setCacheExpiration($uri)
  117. {
  118. // don't add cache expiration (Expires) if
  119. // * the client lifetime is not set
  120. // * the response already has a cache validation (Last-Modified header)
  121. // * the Expires header has already been set
  122. if (!$lifetime = $this->cacheManager->getClientLifeTime($uri, 'page'))
  123. {
  124. return;
  125. }
  126. if ($this->response->hasHttpHeader('Last-Modified'))
  127. {
  128. return;
  129. }
  130. if (!$this->response->hasHttpHeader('Expires'))
  131. {
  132. $this->response->setHttpHeader('Expires', $this->response->getDate(time() + $lifetime), false);
  133. $this->response->addCacheControlHttpHeader('max-age', $lifetime);
  134. }
  135. }
  136. /**
  137. * Sets cache validation headers.
  138. *
  139. * @param string An internal URI
  140. */
  141. protected function setCacheValidation($uri)
  142. {
  143. // don't add cache validation (Last-Modified) if
  144. // * the client lifetime is set (cache.yml)
  145. // * the response already has a Last-Modified header
  146. if ($this->cacheManager->getClientLifeTime($uri, 'page'))
  147. {
  148. return;
  149. }
  150. if (!$this->response->hasHttpHeader('Last-Modified'))
  151. {
  152. $this->response->setHttpHeader('Last-Modified', $this->response->getDate(time()), false);
  153. }
  154. if (sfConfig::get('sf_etag'))
  155. {
  156. $etag = '"'.md5($this->response->getContent()).'"';
  157. $this->response->setHttpHeader('ETag', $etag);
  158. }
  159. }
  160. /**
  161. * Checks cache validation headers.
  162. */
  163. protected function checkCacheValidation()
  164. {
  165. // Etag support
  166. if (sfConfig::get('sf_etag'))
  167. {
  168. $etag = '"'.md5($this->response->getContent()).'"';
  169. if ($this->request->getHttpHeader('IF_NONE_MATCH') == $etag)
  170. {
  171. $this->response->setStatusCode(304);
  172. $this->response->setHeaderOnly(true);
  173. if (sfConfig::get('sf_logging_enabled'))
  174. {
  175. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('ETag matches If-None-Match (send 304)')));
  176. }
  177. }
  178. }
  179. // conditional GET support
  180. // never in debug mode
  181. if ($this->response->hasHttpHeader('Last-Modified') && !sfConfig::get('sf_debug'))
  182. {
  183. $lastModified = $this->response->getHttpHeader('Last-Modified');
  184. $lastModified = $lastModified[0];
  185. if ($this->request->getHttpHeader('IF_MODIFIED_SINCE') == $lastModified)
  186. {
  187. $this->response->setStatusCode(304);
  188. $this->response->setHeaderOnly(true);
  189. if (sfConfig::get('sf_logging_enabled'))
  190. {
  191. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Last-Modified matches If-Modified-Since (send 304)')));
  192. }
  193. }
  194. }
  195. }
  196. }