PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Controller/Action/Helper/Cache.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 279 lines | 130 code | 21 blank | 128 comment | 22 complexity | a6efd74954cf0421bff398e8b6d7fe7c 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. if ($recursive) {
  123. $backend = $cache->getBackend();
  124. if (($backend instanceof Zend_Cache_Backend)
  125. && method_exists($backend, 'removeRecursively')
  126. ) {
  127. return $backend->removeRecursively($relativeUrl);
  128. }
  129. }
  130. return $cache->remove($relativeUrl);
  131. }
  132. /**
  133. * Remove a specific page cache static file based on its
  134. * relative URL from the application's public directory.
  135. * The file extension is not required here; usually matches
  136. * the original REQUEST_URI that was cached.
  137. *
  138. * @param array $tags
  139. * @return mixed
  140. */
  141. public function removePagesTagged(array $tags)
  142. {
  143. return $this->getCache(Zend_Cache_Manager::PAGECACHE)
  144. ->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
  145. }
  146. /**
  147. * Commence page caching for any cacheable actions
  148. *
  149. * @return void
  150. */
  151. public function preDispatch()
  152. {
  153. $controller = $this->getRequest()->getControllerName();
  154. $action = $this->getRequest()->getActionName();
  155. $stats = ob_get_status(true);
  156. foreach ($stats as $status) {
  157. if ($status['name'] == 'Zend_Cache_Frontend_Page::_flush'
  158. || $status['name'] == 'Zend_Cache_Frontend_Capture::_flush') {
  159. $obStarted = true;
  160. }
  161. }
  162. if (!isset($obStarted) && isset($this->_caching[$controller]) &&
  163. in_array($action, $this->_caching[$controller])) {
  164. $reqUri = $this->getRequest()->getRequestUri();
  165. $tags = array();
  166. if (isset($this->_tags[$controller][$action])
  167. && !empty($this->_tags[$controller][$action])) {
  168. $tags = array_unique($this->_tags[$controller][$action]);
  169. }
  170. $extension = null;
  171. if (isset($this->_extensions[$controller][$action])) {
  172. $extension = $this->_extensions[$controller][$action];
  173. }
  174. $this->getCache(Zend_Cache_Manager::PAGECACHE)
  175. ->start($this->_encodeCacheId($reqUri), $tags, $extension);
  176. }
  177. }
  178. /**
  179. * Encode a Cache ID as hexadecimal. This is a workaround because Backend ID validation
  180. * is trapped in the Frontend classes. Will try to get this reversed for ZF 2.0
  181. * because it's a major annoyance to have IDs so restricted!
  182. *
  183. * @return string
  184. * @param string $requestUri
  185. */
  186. protected function _encodeCacheId($requestUri)
  187. {
  188. return bin2hex($requestUri);
  189. }
  190. /**
  191. * Set an instance of the Cache Manager for this helper
  192. *
  193. * @param Zend_Cache_Manager $manager
  194. * @return void
  195. */
  196. public function setManager(Zend_Cache_Manager $manager)
  197. {
  198. $this->_manager = $manager;
  199. return $this;
  200. }
  201. /**
  202. * Get the Cache Manager instance or instantiate the object if not
  203. * exists. Attempts to load from bootstrap if available.
  204. *
  205. * @return Zend_Cache_Manager
  206. */
  207. public function getManager()
  208. {
  209. if ($this->_manager !== null) {
  210. return $this->_manager;
  211. }
  212. $front = Zend_Controller_Front::getInstance();
  213. if ($front->getParam('bootstrap')
  214. && $front->getParam('bootstrap')->getResource('CacheManager')) {
  215. return $front->getParam('bootstrap')
  216. ->getResource('CacheManager');
  217. }
  218. $this->_manager = new Zend_Cache_Manager;
  219. return $this->_manager;
  220. }
  221. /**
  222. * Return a list of actions for the current Controller marked for
  223. * caching
  224. *
  225. * @return array
  226. */
  227. public function getCacheableActions()
  228. {
  229. return $this->_caching;
  230. }
  231. /**
  232. * Return a list of tags set for all cacheable actions
  233. *
  234. * @return array
  235. */
  236. public function getCacheableTags()
  237. {
  238. return $this->_tags;
  239. }
  240. /**
  241. * Proxy non-matched methods back to Zend_Cache_Manager where
  242. * appropriate
  243. *
  244. * @param string $method
  245. * @param array $args
  246. * @return mixed
  247. */
  248. public function __call($method, $args)
  249. {
  250. if (method_exists($this->getManager(), $method)) {
  251. return call_user_func_array(
  252. array($this->getManager(), $method), $args
  253. );
  254. }
  255. throw new Zend_Controller_Action_Exception('Method does not exist:'
  256. . $method);
  257. }
  258. }