Cache.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Controller_Action_Helper_Abstract
  23. */
  24. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  25. /**
  26. * @see Zend_Controller_Action_Exception
  27. */
  28. require_once 'Zend/Controller/Action/Exception.php';
  29. /**
  30. * @see Zend_Cache_Manager
  31. */
  32. require_once 'Zend/Cache/Manager.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Controller
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Controller_Action_Helper_Cache
  40. extends Zend_Controller_Action_Helper_Abstract
  41. {
  42. /**
  43. * Local Cache Manager object used by Helper
  44. *
  45. * @var Zend_Cache_Manager
  46. */
  47. protected $_manager = null;
  48. /**
  49. * Indexed map of Actions to attempt Page caching on by Controller
  50. *
  51. * @var array
  52. */
  53. protected $_caching = array();
  54. /**
  55. * Indexed map of Tags by Controller and Action
  56. *
  57. * @var array
  58. */
  59. protected $_tags = array();
  60. /**
  61. * Track output buffering condition
  62. */
  63. protected $_obStarted = false;
  64. /**
  65. * Tell the helper which actions are cacheable and under which
  66. * tags (if applicable) they should be recorded with
  67. *
  68. * @param array $actions
  69. * @param array $tags
  70. * @return void
  71. */
  72. public function direct(array $actions, array $tags = array())
  73. {
  74. $controller = $this->getRequest()->getControllerName();
  75. $actions = array_unique($actions);
  76. if (!isset($this->_caching[$controller])) {
  77. $this->_caching[$controller] = array();
  78. }
  79. if (!empty($tags)) {
  80. $tags = array_unique($tags);
  81. if (!isset($this->_tags[$controller])) {
  82. $this->_tags[$controller] = array();
  83. }
  84. }
  85. foreach ($actions as $action) {
  86. $this->_caching[$controller][] = $action;
  87. if (!empty($tags)) {
  88. $this->_tags[$controller][$action] = array();
  89. foreach ($tags as $tag) {
  90. $this->_tags[$controller][$action][] = $tag;
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Remove a specific page cache static file based on its
  97. * relative URL from the application's public directory.
  98. * The file extension is not required here; usually matches
  99. * the original REQUEST_URI that was cached.
  100. *
  101. * @param string $relativeUrl
  102. * @param bool $recursive
  103. * @return mixed
  104. */
  105. public function removePage($relativeUrl, $recursive = false)
  106. {
  107. if ($recursive) {
  108. return $this->getCache('page')->removeRecursive($relativeUrl);
  109. } else {
  110. return $this->getCache('page')->remove($relativeUrl);
  111. }
  112. }
  113. /**
  114. * Remove a specific page cache static file based on its
  115. * relative URL from the application's public directory.
  116. * The file extension is not required here; usually matches
  117. * the original REQUEST_URI that was cached.
  118. *
  119. * @param array $tags
  120. * @return mixed
  121. */
  122. public function removePagesTagged(array $tags)
  123. {
  124. return $this->getCache('page')
  125. ->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
  126. }
  127. /**
  128. * Commence page caching for any cacheable actions
  129. *
  130. * @return void
  131. */
  132. public function preDispatch()
  133. {
  134. $controller = $this->getRequest()->getControllerName();
  135. $action = $this->getRequest()->getActionName();
  136. $stats = ob_get_status(true);
  137. foreach ($stats as $status) {
  138. if ($status['name'] == 'Zend_Cache_Frontend_Page::_flush'
  139. || $status['name'] == 'Zend_Cache_Frontend_Capture::_flush') {
  140. $obStarted = true;
  141. }
  142. }
  143. if (!isset($obStarted) && isset($this->_caching[$controller]) &&
  144. in_array($action, $this->_caching[$controller])) {
  145. $reqUri = $this->getRequest()->getRequestUri();
  146. $tags = array();
  147. if (isset($this->_tags[$controller][$action])
  148. && !empty($this->_tags[$controller][$action])) {
  149. $tags = array_unique($this->_tags[$controller][$action]);
  150. }
  151. $this->getCache('page')->start($reqUri, $tags);
  152. }
  153. }
  154. /**
  155. * Set an instance of the Cache Manager for this helper
  156. *
  157. * @param Zend_Cache_Manager $manager
  158. * @return void
  159. */
  160. public function setManager(Zend_Cache_Manager $manager)
  161. {
  162. $this->_manager = $manager;
  163. }
  164. /**
  165. * Get the Cache Manager instance or instantiate the object if not
  166. * exists. Attempts to load from bootstrap if available.
  167. *
  168. * @return Zend_Cache_Manager
  169. */
  170. public function getManager()
  171. {
  172. if (!is_null($this->_manager)) {
  173. return $this->_manager;
  174. }
  175. $front = Zend_Controller_Front::getInstance();
  176. if ($front->getParam('bootstrap')
  177. && $front->getParam('bootstrap')->getResource('CacheManager')) {
  178. return $front->getParam('bootstrap')
  179. ->getResource('CacheManager');
  180. }
  181. $this->_manager = new Zend_Cache_Manager;
  182. return $this->_manager;
  183. }
  184. /**
  185. * Return a list of actions for the current Controller marked for
  186. * caching
  187. *
  188. * @return array
  189. */
  190. public function getCacheableActions()
  191. {
  192. return $this->_caching;
  193. }
  194. /**
  195. * Return a list of tags set for all cacheable actions
  196. *
  197. * @return array
  198. */
  199. public function getCacheableTags()
  200. {
  201. return $this->_tags;
  202. }
  203. /**
  204. * Proxy non-matched methods back to Zend_Cache_Manager where
  205. * appropriate
  206. *
  207. * @param string $method
  208. * @param array $args
  209. * @return mixed
  210. */
  211. public function __call($method, $args)
  212. {
  213. if (method_exists($this->getManager(), $method)) {
  214. return call_user_func_array(
  215. array($this->getManager(), $method), $args
  216. );
  217. }
  218. throw new Zend_Controller_Action_Exception('Method does not exist:'
  219. . $method);
  220. }
  221. }