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

https://bitbucket.org/baruffaldi/website-insaneminds · PHP · 193 lines · 74 code · 13 blank · 106 comment · 12 complexity · 4b8462fb2f507455cbf0b821248b25cf 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($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. } else {
  100. if (!is_string($this->_specificOptions['cached_entity']) && !is_object($this->_specificOptions['cached_entity'])) {
  101. Zend_Cache::throwException('cached_entity must be an object or a class name');
  102. }
  103. }
  104. $this->_cachedEntity = $this->_specificOptions['cached_entity'];
  105. if(is_string($this->_cachedEntity)){
  106. $this->_cachedEntityLabel = $this->_cachedEntity;
  107. } else {
  108. $ro = new ReflectionObject($this->_cachedEntity);
  109. $this->_cachedEntityLabel = $ro->getName();
  110. }
  111. $this->setOption('automatic_serialization', true);
  112. }
  113. /**
  114. * Set a specific life time
  115. *
  116. * @param int $specificLifetime
  117. * @return void
  118. */
  119. public function setSpecificLifetime($specificLifetime = false)
  120. {
  121. $this->_specificLifetime = $specificLifetime;
  122. }
  123. /**
  124. * Set the cache array
  125. *
  126. * @param array $tags
  127. * @return void
  128. */
  129. public function setTagsArray($tags = array())
  130. {
  131. $this->_tags = $tags;
  132. }
  133. /**
  134. * Main method : call the specified method or get the result from cache
  135. *
  136. * @param string $name Method name
  137. * @param array $parameters Method parameters
  138. * @return mixed Result
  139. */
  140. public function __call($name, $parameters)
  141. {
  142. $cacheBool1 = $this->_specificOptions['cache_by_default'];
  143. $cacheBool2 = in_array($name, $this->_specificOptions['cached_methods']);
  144. $cacheBool3 = in_array($name, $this->_specificOptions['non_cached_methods']);
  145. $cache = (($cacheBool1 || $cacheBool2) && (!$cacheBool3));
  146. if (!$cache) {
  147. // We do not have not cache
  148. return call_user_func_array(array($this->_cachedEntity, $name), $parameters);
  149. }
  150. $id = $this->_makeId($name, $parameters);
  151. if ($this->test($id)) {
  152. // A cache is available
  153. $result = $this->load($id);
  154. $output = $result[0];
  155. $return = $result[1];
  156. } else {
  157. // A cache is not available
  158. ob_start();
  159. ob_implicit_flush(false);
  160. $return = call_user_func_array(array($this->_cachedEntity, $name), $parameters);
  161. $output = ob_get_contents();
  162. ob_end_clean();
  163. $data = array($output, $return);
  164. $this->save($data, $id, $this->_tags, $this->_specificLifetime);
  165. }
  166. echo $output;
  167. return $return;
  168. }
  169. /**
  170. * Make a cache id from the method name and parameters
  171. *
  172. * @param string $name Method name
  173. * @param array $parameters Method parameters
  174. * @return string Cache id
  175. */
  176. private function _makeId($name, $parameters)
  177. {
  178. return md5($this->_cachedEntityLabel . '__' . $name . '__' . serialize($parameters));
  179. }
  180. }