Function.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Sebastian Bergmann <sb@sebastian-bergmann.de> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Function.php,v 1.6 2003/01/04 11:54:45 mj Exp $
  19. require_once 'Cache.php';
  20. /**
  21. * Function_Cache
  22. *
  23. * Purpose:
  24. *
  25. * Caching the result and output of functions.
  26. *
  27. * Example:
  28. *
  29. * require_once 'Cache/Function.php';
  30. *
  31. * class foo {
  32. * function bar($test) {
  33. * echo "foo::bar($test)<br>";
  34. * }
  35. * }
  36. *
  37. * class bar {
  38. * function foobar($object) {
  39. * echo '$'.$object.'->foobar('.$object.')<br>';
  40. * }
  41. * }
  42. *
  43. * $bar = new bar;
  44. *
  45. * function foobar() {
  46. * echo 'foobar()';
  47. * }
  48. *
  49. * $cache = new Cache_Function();
  50. *
  51. * $cache->call('foo::bar', 'test');
  52. * $cache->call('bar->foobar', 'bar');
  53. * $cache->call('foobar');
  54. *
  55. * Note:
  56. *
  57. * You cannot cache every function. You should only cache
  58. * functions that only depend on their arguments and don't use
  59. * global or static variables, don't rely on database queries or
  60. * files, and so on.
  61. *
  62. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  63. * @module Function_Cache
  64. * @modulegroup Function_Cache
  65. * @package Cache
  66. * @version $Revision: 1.6 $
  67. * @access public
  68. */
  69. class Cache_Function extends Cache {
  70. var $expires;
  71. /**
  72. * Constructor
  73. *
  74. * @param string Name of container class
  75. * @param array Array with container class options
  76. * @param integer Number of seconds for which to cache
  77. */
  78. function Cache_Function($container = 'file',
  79. $container_options = array('cache_dir' => '.',
  80. 'filename_prefix' => 'cache_'
  81. ),
  82. $expires = 3600
  83. )
  84. {
  85. $this->Cache($container, $container_options);
  86. $this->expires = $expires;
  87. }
  88. /**
  89. * PEAR-Deconstructor
  90. * Call deconstructor of parent
  91. */
  92. function _Cache_Function() {
  93. $this->_Cache();
  94. }
  95. /**
  96. * Calls a cacheable function or method.
  97. *
  98. * @return mixed $result
  99. * @access public
  100. */
  101. function call() {
  102. // get arguments
  103. $arguments = func_get_args();
  104. // generate cache id
  105. $id = md5(serialize($arguments));
  106. // query cache
  107. $cached_object = $this->get($id, 'function_cache');
  108. if ($cached_object != NULL) {
  109. // cache hit: return cached output and result
  110. $output = $cached_object[0];
  111. $result = $cached_object[1];
  112. } else {
  113. // cache miss: call function, store output and result in cache
  114. ob_start();
  115. $target = array_shift($arguments);
  116. // classname::staticMethod
  117. if (strstr($target, '::')) {
  118. list($class, $method) = explode('::', $target);
  119. $result = call_user_func_array(array($class, $method), $arguments);
  120. }
  121. // object->method
  122. elseif (strstr($target, '->')) {
  123. list($object, $method) = explode('->', $target);
  124. global $$object;
  125. $result = call_user_func_array(array($$object, $method), $arguments);
  126. }
  127. // function
  128. else {
  129. $result = call_user_func_array($target, $arguments);
  130. }
  131. $output = ob_get_contents();
  132. ob_end_clean();
  133. $this->save($id, array($output, $result), $this->expires, 'function_cache');
  134. }
  135. echo $output;
  136. return $result;
  137. }
  138. }
  139. ?>