PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Cache/Frontend/Class.php

https://bitbucket.org/goldie/zend-framework1
PHP | 265 lines | 102 code | 23 blank | 140 comment | 16 complexity | d39f67badf5a481d53eda24a5693be7e 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_Cache
  17. * @subpackage Zend_Cache_Frontend
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Class.php 24032 2011-05-10 21:08:20Z mabe $
  21. */
  22. /**
  23. * @see Zend_Cache_Core
  24. */
  25. require_once 'Zend/Cache/Core.php';
  26. /**
  27. * @package Zend_Cache
  28. * @subpackage Zend_Cache_Frontend
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Cache_Frontend_Class extends Zend_Cache_Core
  33. {
  34. /**
  35. * Available options
  36. *
  37. * ====> (mixed) cached_entity :
  38. * - if set to a class name, we will cache an abstract class and will use only static calls
  39. * - if set to an object, we will cache this object methods
  40. *
  41. * ====> (boolean) cache_by_default :
  42. * - if true, method calls will be cached by default
  43. *
  44. * ====> (array) cached_methods :
  45. * - an array of method names which will be cached (even if cache_by_default = false)
  46. *
  47. * ====> (array) non_cached_methods :
  48. * - an array of method names which won't be cached (even if cache_by_default = true)
  49. *
  50. * @var array available options
  51. */
  52. protected $_specificOptions = array(
  53. 'cached_entity' => null,
  54. 'cache_by_default' => true,
  55. 'cached_methods' => array(),
  56. 'non_cached_methods' => array()
  57. );
  58. /**
  59. * Tags array
  60. *
  61. * @var array
  62. */
  63. private $_tags = array();
  64. /**
  65. * SpecificLifetime value
  66. *
  67. * false => no specific life time
  68. *
  69. * @var int
  70. */
  71. private $_specificLifetime = false;
  72. /**
  73. * The cached object or the name of the cached abstract class
  74. *
  75. * @var mixed
  76. */
  77. private $_cachedEntity = null;
  78. /**
  79. * The class name of the cached object or cached abstract class
  80. *
  81. * Used to differentiate between different classes with the same method calls.
  82. *
  83. * @var string
  84. */
  85. private $_cachedEntityLabel = '';
  86. /**
  87. * Priority (used by some particular backends)
  88. *
  89. * @var int
  90. */
  91. private $_priority = 8;
  92. /**
  93. * Constructor
  94. *
  95. * @param array $options Associative array of options
  96. * @throws Zend_Cache_Exception
  97. * @return void
  98. */
  99. public function __construct(array $options = array())
  100. {
  101. while (list($name, $value) = each($options)) {
  102. $this->setOption($name, $value);
  103. }
  104. if ($this->_specificOptions['cached_entity'] === null) {
  105. Zend_Cache::throwException('cached_entity must be set !');
  106. }
  107. $this->setCachedEntity($this->_specificOptions['cached_entity']);
  108. $this->setOption('automatic_serialization', true);
  109. }
  110. /**
  111. * Set a specific life time
  112. *
  113. * @param int $specificLifetime
  114. * @return void
  115. */
  116. public function setSpecificLifetime($specificLifetime = false)
  117. {
  118. $this->_specificLifetime = $specificLifetime;
  119. }
  120. /**
  121. * Set the priority (used by some particular backends)
  122. *
  123. * @param int $priority integer between 0 (very low priority) and 10 (maximum priority)
  124. */
  125. public function setPriority($priority)
  126. {
  127. $this->_priority = $priority;
  128. }
  129. /**
  130. * Public frontend to set an option
  131. *
  132. * Just a wrapper to get a specific behaviour for cached_entity
  133. *
  134. * @param string $name Name of the option
  135. * @param mixed $value Value of the option
  136. * @throws Zend_Cache_Exception
  137. * @return void
  138. */
  139. public function setOption($name, $value)
  140. {
  141. if ($name == 'cached_entity') {
  142. $this->setCachedEntity($value);
  143. } else {
  144. parent::setOption($name, $value);
  145. }
  146. }
  147. /**
  148. * Specific method to set the cachedEntity
  149. *
  150. * if set to a class name, we will cache an abstract class and will use only static calls
  151. * if set to an object, we will cache this object methods
  152. *
  153. * @param mixed $cachedEntity
  154. */
  155. public function setCachedEntity($cachedEntity)
  156. {
  157. if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
  158. Zend_Cache::throwException('cached_entity must be an object or a class name');
  159. }
  160. $this->_cachedEntity = $cachedEntity;
  161. $this->_specificOptions['cached_entity'] = $cachedEntity;
  162. if (is_string($this->_cachedEntity)){
  163. $this->_cachedEntityLabel = $this->_cachedEntity;
  164. } else {
  165. $ro = new ReflectionObject($this->_cachedEntity);
  166. $this->_cachedEntityLabel = $ro->getName();
  167. }
  168. }
  169. /**
  170. * Set the cache array
  171. *
  172. * @param array $tags
  173. * @return void
  174. */
  175. public function setTagsArray($tags = array())
  176. {
  177. $this->_tags = $tags;
  178. }
  179. /**
  180. * Main method : call the specified method or get the result from cache
  181. *
  182. * @param string $name Method name
  183. * @param array $parameters Method parameters
  184. * @return mixed Result
  185. */
  186. public function __call($name, $parameters)
  187. {
  188. $callback = array($this->_cachedEntity, $name);
  189. if (!is_callable($callback, false)) {
  190. Zend_Cache::throwException('Invalid callback');
  191. }
  192. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  193. $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
  194. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
  195. $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
  196. if (!$cache) {
  197. // We do not have not cache
  198. return call_user_func_array($callback, $parameters);
  199. }
  200. $id = $this->_makeId($name, $parameters);
  201. if ( ($rs = $this->load($id)) && isset($rs[0], $rs[1]) ) {
  202. // A cache is available
  203. $output = $rs[0];
  204. $return = $rs[1];
  205. } else {
  206. // A cache is not available (or not valid for this frontend)
  207. ob_start();
  208. ob_implicit_flush(false);
  209. try {
  210. $return = call_user_func_array($callback, $parameters);
  211. $output = ob_get_clean();
  212. $data = array($output, $return);
  213. $this->save($data, $id, $this->_tags, $this->_specificLifetime, $this->_priority);
  214. } catch (Exception $e) {
  215. ob_end_clean();
  216. throw $e;
  217. }
  218. }
  219. echo $output;
  220. return $return;
  221. }
  222. /**
  223. * ZF-9970
  224. *
  225. * @deprecated
  226. */
  227. private function _makeId($name, $args)
  228. {
  229. return $this->makeId($name, $args);
  230. }
  231. /**
  232. * Make a cache id from the method name and parameters
  233. *
  234. * @param string $name Method name
  235. * @param array $args Method parameters
  236. * @return string Cache id
  237. */
  238. public function makeId($name, array $args = array())
  239. {
  240. return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($args));
  241. }
  242. }