/libraries/joomla/cache/controller/callback.php

https://github.com/pacoqueen/callao_chico · PHP · 165 lines · 79 code · 25 blank · 61 comment · 22 complexity · dd33dd44eb8918a5ec9c8162ed3d5293 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id: callback.php 19705 2010-11-30 18:29:50Z chdemko $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. // No direct access
  9. defined('JPATH_BASE') or die;
  10. jimport('joomla.cache.controller');
  11. /**
  12. * Joomla! Cache callback type object
  13. *
  14. * @package Joomla.Framework
  15. * @subpackage Cache
  16. * @since 1.6
  17. */
  18. class JCacheControllerCallback extends JCacheController
  19. {
  20. /**
  21. * Executes a cacheable callback if not found in cache else returns cached output and result
  22. *
  23. * Since arguments to this function are read with func_get_args you can pass any number of arguments to this method
  24. * as long as the first argument passed is the callback definition.
  25. *
  26. * The callback definition can be in several forms:
  27. * - Standard PHP Callback array <http://php.net/callback> [recommended]
  28. * - Function name as a string eg. 'foo' for function foo()
  29. * - Static method name as a string eg. 'MyClass::myMethod' for method myMethod() of class MyClass
  30. *
  31. * @return mixed Result of the callback
  32. * @since 1.6
  33. */
  34. public function call()
  35. {
  36. // Get callback and arguments
  37. $args = func_get_args();
  38. $callback = array_shift($args);
  39. return $this->get($callback, $args);
  40. }
  41. /**
  42. * Executes a cacheable callback if not found in cache else returns cached output and result
  43. *
  44. * @param mixed Callback or string shorthand for a callback
  45. * @param array Callback arguments
  46. * @param string Cache id
  47. * @param boolean Perform workarounds on data?
  48. * @param array Workaround options
  49. * @return mixed Result of the callback
  50. * @since 1.6
  51. */
  52. public function get($callback, $args=array(), $id=false, $wrkarounds=false, $woptions=array())
  53. {
  54. // Normalize callback
  55. if (is_array($callback)) {
  56. // We have a standard php callback array -- do nothing
  57. } elseif (strstr($callback, '::')) {
  58. // This is shorthand for a static method callback classname::methodname
  59. list($class, $method) = explode('::', $callback);
  60. $callback = array(trim($class), trim($method));
  61. } elseif (strstr($callback, '->')) {
  62. /*
  63. * This is a really not so smart way of doing this... we provide this for backward compatability but this
  64. * WILL! disappear in a future version. If you are using this syntax change your code to use the standard
  65. * PHP callback array syntax: <http://php.net/callback>
  66. *
  67. * We have to use some silly global notation to pull it off and this is very unreliable
  68. */
  69. list($object_123456789, $method) = explode('->', $callback);
  70. global $$object_123456789;
  71. $callback = array($$object_123456789, $method);
  72. } else {
  73. // We have just a standard function -- do nothing
  74. }
  75. if (!$id) {
  76. // Generate an ID
  77. $id = $this->_makeId($callback, $args);
  78. }
  79. $data = false;
  80. $data = $this->cache->get($id);
  81. $locktest = new stdClass;
  82. $locktest->locked = null;
  83. $locktest->locklooped = null;
  84. if ($data === false) {
  85. $locktest = $this->cache->lock($id);
  86. if ($locktest->locked == true && $locktest->locklooped == true) {
  87. $data = $this->cache->get($id);
  88. }
  89. }
  90. $coptions= array();
  91. if ($data !== false) {
  92. $cached = unserialize(trim($data));
  93. $coptions['mergehead'] = isset($woptions['mergehead']) ? $woptions['mergehead'] : 0;
  94. $output = ($wrkarounds == false) ? $cached['output'] : JCache::getWorkarounds($cached['output'], $coptions);
  95. $result = $cached['result'];
  96. if ($locktest->locked == true) $this->cache->unlock($id);
  97. } else {
  98. if (!is_array($args)) {
  99. $Args = !empty($args) ? array( &$args) : array();
  100. } else {
  101. $Args = &$args;
  102. }
  103. if ($locktest->locked == false) $locktest = $this->cache->lock($id);
  104. ob_start();
  105. ob_implicit_flush(false);
  106. $result = call_user_func_array($callback, $Args);
  107. $output = ob_get_contents();
  108. ob_end_clean();
  109. $cached = array();
  110. $coptions['nopathway'] = isset($woptions['nopathway']) ? $woptions['nopathway'] : 1;
  111. $coptions['nohead'] = isset($woptions['nohead']) ? $woptions['nohead'] : 1;
  112. $coptions['nomodules'] = isset($woptions['nomodules']) ? $woptions['nomodules'] : 1;
  113. $coptions['modulemode'] = isset($woptions['modulemode']) ? $woptions['modulemode'] : 0;
  114. $cached['output'] = ($wrkarounds == false) ? $output : JCache::setWorkarounds($output, $coptions);
  115. $cached['result'] = $result;
  116. // Store the cache data
  117. $this->cache->store(serialize($cached), $id);
  118. if ($locktest->locked == true) $this->cache->unlock($id);
  119. }
  120. echo $output;
  121. return $result;
  122. }
  123. /**
  124. * Generate a callback cache id
  125. *
  126. * @param callback $callback Callback to cache
  127. * @param array $args Arguments to the callback method to cache
  128. * @return string MD5 Hash : function cache id
  129. * @since 1.6
  130. */
  131. private function _makeId($callback, $args)
  132. {
  133. if (is_array($callback) && is_object($callback[0])) {
  134. $vars = get_object_vars($callback[0]);
  135. $vars[] = strtolower(get_class($callback[0]));
  136. $callback[0] = $vars;
  137. }
  138. return md5(serialize(array($callback, $args)));
  139. }
  140. }