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

/library/Zend/Cache/Frontend/ClassFrontend.php

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