/Zend/Cache/Frontend/Class.php

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