PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/library/Zend/Controller/Action/Helper/Cache.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 289 lines | 139 code | 22 blank | 128 comment | 24 complexity | 409bb11d66c909133cd0afbcbcc0297e MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Cache.php 24853 2012-05-31 23:19:27Z adamlundrigan $
  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-2012 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. * Indexed map of Extensions by Controller and Action
  62. *
  63. * @var array
  64. */
  65. protected $_extensions = array();
  66. /**
  67. * Track output buffering condition
  68. */
  69. protected $_obStarted = false;
  70. /**
  71. * Tell the helper which actions are cacheable and under which
  72. * tags (if applicable) they should be recorded with
  73. *
  74. * @param array $actions
  75. * @param array $tags
  76. * @return void
  77. */
  78. public function direct(array $actions, array $tags = array(), $extension = null)
  79. {
  80. $controller = $this->getRequest()->getControllerName();
  81. $actions = array_unique($actions);
  82. if (!isset($this->_caching[$controller])) {
  83. $this->_caching[$controller] = array();
  84. }
  85. if (!empty($tags)) {
  86. $tags = array_unique($tags);
  87. if (!isset($this->_tags[$controller])) {
  88. $this->_tags[$controller] = array();
  89. }
  90. }
  91. foreach ($actions as $action) {
  92. $this->_caching[$controller][] = $action;
  93. if (!empty($tags)) {
  94. $this->_tags[$controller][$action] = array();
  95. foreach ($tags as $tag) {
  96. $this->_tags[$controller][$action][] = $tag;
  97. }
  98. }
  99. }
  100. if ($extension) {
  101. if (!isset($this->_extensions[$controller])) {
  102. $this->_extensions[$controller] = array();
  103. }
  104. foreach ($actions as $action) {
  105. $this->_extensions[$controller][$action] = $extension;
  106. }
  107. }
  108. }
  109. /**
  110. * Remove a specific page cache static file based on its
  111. * relative URL from the application's public directory.
  112. * The file extension is not required here; usually matches
  113. * the original REQUEST_URI that was cached.
  114. *
  115. * @param string $relativeUrl
  116. * @param bool $recursive
  117. * @return mixed
  118. */
  119. public function removePage($relativeUrl, $recursive = false)
  120. {
  121. $cache = $this->getCache(Zend_Cache_Manager::PAGECACHE);
  122. $encodedCacheId = $this->_encodeCacheId($relativeUrl);
  123. if ($recursive) {
  124. $backend = $cache->getBackend();
  125. if (($backend instanceof Zend_Cache_Backend)
  126. && method_exists($backend, 'removeRecursively')
  127. ) {
  128. $result = $backend->removeRecursively($encodedCacheId);
  129. if (is_null($result) ) {
  130. $result = $backend->removeRecursively($relativeUrl);
  131. }
  132. return $result;
  133. }
  134. }
  135. $result = $cache->remove($encodedCacheId);
  136. if (is_null($result) ) {
  137. $result = $cache->remove($relativeUrl);
  138. }
  139. return $result;
  140. }
  141. /**
  142. * Remove a specific page cache static file based on its
  143. * relative URL from the application's public directory.
  144. * The file extension is not required here; usually matches
  145. * the original REQUEST_URI that was cached.
  146. *
  147. * @param array $tags
  148. * @return mixed
  149. */
  150. public function removePagesTagged(array $tags)
  151. {
  152. return $this->getCache(Zend_Cache_Manager::PAGECACHE)
  153. ->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
  154. }
  155. /**
  156. * Commence page caching for any cacheable actions
  157. *
  158. * @return void
  159. */
  160. public function preDispatch()
  161. {
  162. $controller = $this->getRequest()->getControllerName();
  163. $action = $this->getRequest()->getActionName();
  164. $stats = ob_get_status(true);
  165. foreach ($stats as $status) {
  166. if ($status['name'] == 'Zend_Cache_Frontend_Page::_flush'
  167. || $status['name'] == 'Zend_Cache_Frontend_Capture::_flush') {
  168. $obStarted = true;
  169. }
  170. }
  171. if (!isset($obStarted) && isset($this->_caching[$controller]) &&
  172. in_array($action, $this->_caching[$controller])) {
  173. $reqUri = $this->getRequest()->getRequestUri();
  174. $tags = array();
  175. if (isset($this->_tags[$controller][$action])
  176. && !empty($this->_tags[$controller][$action])) {
  177. $tags = array_unique($this->_tags[$controller][$action]);
  178. }
  179. $extension = null;
  180. if (isset($this->_extensions[$controller][$action])) {
  181. $extension = $this->_extensions[$controller][$action];
  182. }
  183. $this->getCache(Zend_Cache_Manager::PAGECACHE)
  184. ->start($this->_encodeCacheId($reqUri), $tags, $extension);
  185. }
  186. }
  187. /**
  188. * Encode a Cache ID as hexadecimal. This is a workaround because Backend ID validation
  189. * is trapped in the Frontend classes. Will try to get this reversed for ZF 2.0
  190. * because it's a major annoyance to have IDs so restricted!
  191. *
  192. * @return string
  193. * @param string $requestUri
  194. */
  195. protected function _encodeCacheId($requestUri)
  196. {
  197. return bin2hex($requestUri);
  198. }
  199. /**
  200. * Set an instance of the Cache Manager for this helper
  201. *
  202. * @param Zend_Cache_Manager $manager
  203. * @return void
  204. */
  205. public function setManager(Zend_Cache_Manager $manager)
  206. {
  207. $this->_manager = $manager;
  208. return $this;
  209. }
  210. /**
  211. * Get the Cache Manager instance or instantiate the object if not
  212. * exists. Attempts to load from bootstrap if available.
  213. *
  214. * @return Zend_Cache_Manager
  215. */
  216. public function getManager()
  217. {
  218. if ($this->_manager !== null) {
  219. return $this->_manager;
  220. }
  221. $front = Zend_Controller_Front::getInstance();
  222. if ($front->getParam('bootstrap')
  223. && $front->getParam('bootstrap')->getResource('CacheManager')) {
  224. return $front->getParam('bootstrap')
  225. ->getResource('CacheManager');
  226. }
  227. $this->_manager = new Zend_Cache_Manager;
  228. return $this->_manager;
  229. }
  230. /**
  231. * Return a list of actions for the current Controller marked for
  232. * caching
  233. *
  234. * @return array
  235. */
  236. public function getCacheableActions()
  237. {
  238. return $this->_caching;
  239. }
  240. /**
  241. * Return a list of tags set for all cacheable actions
  242. *
  243. * @return array
  244. */
  245. public function getCacheableTags()
  246. {
  247. return $this->_tags;
  248. }
  249. /**
  250. * Proxy non-matched methods back to Zend_Cache_Manager where
  251. * appropriate
  252. *
  253. * @param string $method
  254. * @param array $args
  255. * @return mixed
  256. */
  257. public function __call($method, $args)
  258. {
  259. if (method_exists($this->getManager(), $method)) {
  260. return call_user_func_array(
  261. array($this->getManager(), $method), $args
  262. );
  263. }
  264. throw new Zend_Controller_Action_Exception('Method does not exist:'
  265. . $method);
  266. }
  267. }