PageRenderTime 24ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/4.6/includes/PEAR/Cache/Lite/Function.php

http://miacms.googlecode.com/
PHP | 214 lines | 98 code | 17 blank | 99 comment | 20 complexity | 904d56f7b56c4ec6884774b3828a97d6 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * This class extends Cache_Lite and can be used to cache the result and output of functions/methods
  4. *
  5. * This class is completly inspired from Sebastian Bergmann's
  6. * PEAR/Cache_Function class. This is only an adaptation to
  7. * Cache_Lite
  8. *
  9. * There are some examples in the 'docs/examples' file
  10. * Technical choices are described in the 'docs/technical' file
  11. *
  12. * @package Cache_Lite
  13. * @version $Id: Function.php,v 1.11 2006/12/14 12:59:43 cweiske Exp $
  14. * @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
  15. * @author Fabien MARTY <fab@php.net>
  16. */
  17. /** ensure this file is being included by a parent file */
  18. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  19. require_once( $mosConfig_absolute_path . '/includes/PEAR/Cache/Lite.php' );
  20. class Cache_Lite_Function extends Cache_Lite
  21. {
  22. // --- Private properties ---
  23. /**
  24. * Default cache group for function caching
  25. *
  26. * @var string $_defaultGroup
  27. */
  28. var $_defaultGroup = 'Cache_Lite_Function';
  29. /**
  30. * Don't cache the method call when its output contains the string "NOCACHE"
  31. *
  32. * if set to true, the output of the method will never be displayed (because the output is used
  33. * to control the cache)
  34. *
  35. * @var boolean $_dontCacheWhenTheOutputContainsNOCACHE
  36. */
  37. var $_dontCacheWhenTheOutputContainsNOCACHE = false;
  38. /**
  39. * Don't cache the method call when its result is false
  40. *
  41. * @var boolean $_dontCacheWhenTheResultIsFalse
  42. */
  43. var $_dontCacheWhenTheResultIsFalse = false;
  44. /**
  45. * Don't cache the method call when its result is null
  46. *
  47. * @var boolean $_dontCacheWhenTheResultIsNull
  48. */
  49. var $_dontCacheWhenTheResultIsNull = false;
  50. /**
  51. * Debug the Cache_Lite_Function caching process
  52. *
  53. * @var boolean $_debugCacheLiteFunction
  54. */
  55. var $_debugCacheLiteFunction = false;
  56. // --- Public methods ----
  57. /**
  58. * Constructor
  59. *
  60. * $options is an assoc. To have a look at availables options,
  61. * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
  62. *
  63. * Comparing to Cache_Lite constructor, there is another option :
  64. * $options = array(
  65. * (...) see Cache_Lite constructor
  66. * 'debugCacheLiteFunction' => (bool) debug the caching process,
  67. * 'defaultGroup' => default cache group for function caching (string),
  68. * 'dontCacheWhenTheOutputContainsNOCACHE' => (bool) don't cache when the function output contains "NOCACHE",
  69. * 'dontCacheWhenTheResultIsFalse' => (bool) don't cache when the function result is false,
  70. * 'dontCacheWhenTheResultIsNull' => (bool don't cache when the function result is null
  71. * );
  72. *
  73. * @param array $options options
  74. * @access public
  75. */
  76. function Cache_Lite_Function($options = array(NULL))
  77. {
  78. $availableOptions = array('debugCacheLiteFunction', 'defaultGroup', 'dontCacheWhenTheOutputContainsNOCACHE', 'dontCacheWhenTheResultIsFalse', 'dontCacheWhenTheResultIsNull');
  79. while (list($name, $value) = each($options)) {
  80. if (in_array($name, $availableOptions)) {
  81. $property = '_'.$name;
  82. $this->$property = $value;
  83. }
  84. }
  85. reset($options);
  86. $this->Cache_Lite($options);
  87. }
  88. /**
  89. * Calls a cacheable function or method (or not if there is already a cache for it)
  90. *
  91. * Arguments of this method are read with func_get_args. So it doesn't appear
  92. * in the function definition. Synopsis :
  93. * call('functionName', $arg1, $arg2, ...)
  94. * (arg1, arg2... are arguments of 'functionName')
  95. *
  96. * @return mixed result of the function/method
  97. * @access public
  98. */
  99. function call()
  100. {
  101. $arguments = func_get_args();
  102. $id = $this->_makeId($arguments);
  103. $data = $this->get($id, $this->_defaultGroup);
  104. if ($data !== false) {
  105. if ($this->_debugCacheLiteFunction) {
  106. echo "Cache hit !\n";
  107. }
  108. $array = unserialize($data);
  109. $output = $array['output'];
  110. $result = $array['result'];
  111. } else {
  112. if ($this->_debugCacheLiteFunction) {
  113. echo "Cache missed !\n";
  114. }
  115. ob_start();
  116. ob_implicit_flush(false);
  117. $target = array_shift($arguments);
  118. if (is_array($target)) {
  119. // in this case, $target is for example array($obj, 'method')
  120. $object = $target[0];
  121. $method = $target[1];
  122. $result = call_user_func_array(array(&$object, $method), $arguments);
  123. } else {
  124. if (strstr($target, '::')) { // classname::staticMethod
  125. list($class, $method) = explode('::', $target);
  126. $result = call_user_func_array(array($class, $method), $arguments);
  127. } else if (strstr($target, '->')) { // object->method
  128. // use a stupid name ($objet_123456789 because) of problems where the object
  129. // name is the same as this var name
  130. list($object_123456789, $method) = explode('->', $target);
  131. global $$object_123456789;
  132. $result = call_user_func_array(array($$object_123456789, $method), $arguments);
  133. } else { // function
  134. $result = call_user_func_array($target, $arguments);
  135. }
  136. }
  137. $output = ob_get_contents();
  138. ob_end_clean();
  139. if ($this->_dontCacheWhenTheResultIsFalse) {
  140. if ((is_bool($result)) && (!($result))) {
  141. echo($output);
  142. return $result;
  143. }
  144. }
  145. if ($this->_dontCacheWhenTheResultIsNull) {
  146. if (is_null($result)) {
  147. echo($output);
  148. return $result;
  149. }
  150. }
  151. if ($this->_dontCacheWhenTheOutputContainsNOCACHE) {
  152. if (strpos($output, 'NOCACHE') > -1) {
  153. return $result;
  154. }
  155. }
  156. $array['output'] = $output;
  157. $array['result'] = $result;
  158. $this->save(serialize($array), $id, $this->_defaultGroup);
  159. }
  160. echo($output);
  161. return $result;
  162. }
  163. /**
  164. * Drop a cache file
  165. *
  166. * Arguments of this method are read with func_get_args. So it doesn't appear
  167. * in the function definition. Synopsis :
  168. * remove('functionName', $arg1, $arg2, ...)
  169. * (arg1, arg2... are arguments of 'functionName')
  170. *
  171. * @return boolean true if no problem
  172. * @access public
  173. */
  174. function drop()
  175. {
  176. $id = $this->_makeId(func_get_args());
  177. return $this->remove($id, $this->_defaultGroup);
  178. }
  179. /**
  180. * Make an id for the cache
  181. *
  182. * @var array result of func_get_args for the call() or the remove() method
  183. * @return string id
  184. * @access private
  185. */
  186. function _makeId($arguments)
  187. {
  188. $id = serialize($arguments); // Generate a cache id
  189. if (!$this->_fileNameProtection) {
  190. $id = md5($id);
  191. // if fileNameProtection is set to false, then the id has to be hashed
  192. // because it's a very bad file name in most cases
  193. }
  194. return $id;
  195. }
  196. }
  197. ?>