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

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

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 225 lines | 86 code | 15 blank | 124 comment | 14 complexity | 773ed5f6efcebbc2b64a6c27d9e11116 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. * Constructor
  87. *
  88. * @param array $options Associative array of options
  89. * @throws Zend_Cache_Exception
  90. * @return void
  91. */
  92. public function __construct(array $options = array())
  93. {
  94. while (list($name, $value) = each($options)) {
  95. $this->setOption($name, $value);
  96. }
  97. if (is_null($this->_specificOptions['cached_entity'])) {
  98. Zend_Cache::throwException('cached_entity must be set !');
  99. }
  100. $this->setCachedEntity($this->_specificOptions['cached_entity']);
  101. $this->setOption('automatic_serialization', true);
  102. }
  103. /**
  104. * Set a specific life time
  105. *
  106. * @param int $specificLifetime
  107. * @return void
  108. */
  109. public function setSpecificLifetime($specificLifetime = false)
  110. {
  111. $this->_specificLifetime = $specificLifetime;
  112. }
  113. /**
  114. * Public frontend to set an option
  115. *
  116. * Just a wrapper to get a specific behaviour for cached_entity
  117. *
  118. * @param string $name Name of the option
  119. * @param mixed $value Value of the option
  120. * @throws Zend_Cache_Exception
  121. * @return void
  122. */
  123. public function setOption($name, $value)
  124. {
  125. if ($name == 'cached_entity') {
  126. $this->setCachedEntity($value);
  127. } else {
  128. parent::setOption($name, $value);
  129. }
  130. }
  131. /**
  132. * Specific method to set the cachedEntity
  133. *
  134. * if set to a class name, we will cache an abstract class and will use only static calls
  135. * if set to an object, we will cache this object methods
  136. *
  137. * @param mixed $cachedEntity
  138. */
  139. public function setCachedEntity($cachedEntity)
  140. {
  141. if (!is_string($cachedEntity) && !is_object($cachedEntity)) {
  142. Zend_Cache::throwException('cached_entity must be an object or a class name');
  143. }
  144. $this->_cachedEntity = $cachedEntity;
  145. $this->_specificOptions['cached_entity'] = $cachedEntity;
  146. if (is_string($this->_cachedEntity)){
  147. $this->_cachedEntityLabel = $this->_cachedEntity;
  148. } else {
  149. $ro = new ReflectionObject($this->_cachedEntity);
  150. $this->_cachedEntityLabel = $ro->getName();
  151. }
  152. }
  153. /**
  154. * Set the cache array
  155. *
  156. * @param array $tags
  157. * @return void
  158. */
  159. public function setTagsArray($tags = array())
  160. {
  161. $this->_tags = $tags;
  162. }
  163. /**
  164. * Main method : call the specified method or get the result from cache
  165. *
  166. * @param string $name Method name
  167. * @param array $parameters Method parameters
  168. * @return mixed Result
  169. */
  170. public function __call($name, $parameters)
  171. {
  172. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  173. $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
  174. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
  175. $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
  176. if (!$cache) {
  177. // We do not have not cache
  178. return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
  179. }
  180. $id = $this->_makeId($name, $parameters);
  181. if ($this->test($id)) {
  182. // A cache is available
  183. $result = $this->load($id);
  184. $output = $result[0];
  185. $return = $result[1];
  186. } else {
  187. // A cache is not available
  188. ob_start();
  189. ob_implicit_flush(false);
  190. $return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
  191. $output = ob_get_contents();
  192. ob_end_clean();
  193. $data = array($output, $return);
  194. $this->save($data, $id, $this->_tags, $this->_specificLifetime);
  195. }
  196. echo $output;
  197. return $return;
  198. }
  199. /**
  200. * Make a cache id from the method name and parameters
  201. *
  202. * @param string $name Method name
  203. * @param array $parameters Method parameters
  204. * @return string Cache id
  205. */
  206. private function _makeId($name, $parameters)
  207. {
  208. return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($parameters));
  209. }
  210. }