PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Cache/Frontend/Class.php

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