PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/Cache/Lite/Function.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 118 lines | 54 code | 11 blank | 53 comment | 9 complexity | 4ab02230e9a6192d15dadb4561ae01b0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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 47 2005-09-15 02:55:27Z rhuk $
  14. * @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
  15. * @author Fabien MARTY <fab@php.net>
  16. */
  17. // no direct access
  18. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  19. require_once( $mosConfig_absolute_path . '/includes/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. // --- Public methods ----
  30. /**
  31. * Constructor
  32. *
  33. * $options is an assoc. To have a look at availables options,
  34. * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
  35. *
  36. * Comparing to Cache_Lite constructor, there is another option :
  37. * $options = array(
  38. * (...) see Cache_Lite constructor
  39. * 'defaultGroup' => default cache group for function caching (string)
  40. * );
  41. *
  42. * @param array $options options
  43. * @access public
  44. */
  45. function Cache_Lite_Function($options = array(NULL))
  46. {
  47. if (isset($options['defaultGroup'])) {
  48. $this->_defaultGroup = $options['defaultGroup'];
  49. }
  50. $this->Cache_Lite($options);
  51. }
  52. /**
  53. * Calls a cacheable function or method (or not if there is already a cache for it)
  54. *
  55. * Arguments of this method are read with func_get_args. So it doesn't appear
  56. * in the function definition. Synopsis :
  57. * call('functionName', $arg1, $arg2, ...)
  58. * (arg1, arg2... are arguments of 'functionName')
  59. *
  60. * @return mixed result of the function/method
  61. * @access public
  62. */
  63. function call()
  64. {
  65. $arguments = func_get_args();
  66. $numargs = func_num_args();
  67. for($i=1; $i < $numargs; $i++){
  68. $arguments[$i] = &$arguments[$i];
  69. }
  70. $id = serialize($arguments); // Generate a cache id
  71. if (!$this->_fileNameProtection) {
  72. $id = md5($id);
  73. // if fileNameProtection is set to false, then the id has to be hashed
  74. // because it's a very bad file name in most cases
  75. }
  76. $data = $this->get($id, $this->_defaultGroup);
  77. if ($data !== false) {
  78. $array = unserialize($data);
  79. $output = $array['output'];
  80. $result = $array['result'];
  81. } else {
  82. ob_start();
  83. ob_implicit_flush(false);
  84. $target = array_shift($arguments);
  85. if (strstr($target, '::')) { // classname::staticMethod
  86. list($class, $method) = explode('::', $target);
  87. $result = call_user_func_array(array($class, $method), $arguments);
  88. } else if (strstr($target, '->')) { // object->method
  89. // use a stupid name ($objet_123456789 because) of problems when the object
  90. // name is the same as this var name
  91. list($object_123456789, $method) = explode('->', $target);
  92. global $$object_123456789;
  93. $result = call_user_func_array(array($$object_123456789, $method), $arguments);
  94. } else { // function
  95. $result = call_user_func_array($target, $arguments);
  96. }
  97. $output = ob_get_contents();
  98. ob_end_clean();
  99. $array['output'] = $output;
  100. $array['result'] = $result;
  101. $this->save(serialize($array), $id, $this->_defaultGroup);
  102. }
  103. echo($output);
  104. return $result;
  105. }
  106. }
  107. ?>