PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/eternaware/joomus
PHP | 205 lines | 112 code | 26 blank | 67 comment | 22 complexity | 4131177c1f5e1173797e2028650c874e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Cache
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla! Cache callback type object
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Cache
  15. * @since 11.1
  16. */
  17. class JCacheControllerCallback extends JCacheController
  18. {
  19. /**
  20. * Executes a cacheable callback if not found in cache else returns cached output and result
  21. *
  22. * Since arguments to this function are read with func_get_args you can pass any number of
  23. * 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 see <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. *
  33. * @since 11.1
  34. */
  35. public function call()
  36. {
  37. // Get callback and arguments
  38. $args = func_get_args();
  39. $callback = array_shift($args);
  40. return $this->get($callback, $args);
  41. }
  42. /**
  43. * Executes a cacheable callback if not found in cache else returns cached output and result
  44. *
  45. * @param mixed $callback Callback or string shorthand for a callback
  46. * @param array $args Callback arguments
  47. * @param string $id Cache id
  48. * @param boolean $wrkarounds True to use wrkarounds
  49. * @param array $woptions Workaround options
  50. *
  51. * @return mixed Result of the callback
  52. *
  53. * @since 11.1
  54. */
  55. public function get($callback, $args = array(), $id = false, $wrkarounds = false, $woptions = array())
  56. {
  57. // Normalize callback
  58. if (is_array($callback))
  59. {
  60. // We have a standard php callback array -- do nothing
  61. }
  62. elseif (strstr($callback, '::'))
  63. {
  64. // This is shorthand for a static method callback classname::methodname
  65. list ($class, $method) = explode('::', $callback);
  66. $callback = array(trim($class), trim($method));
  67. }
  68. elseif (strstr($callback, '->'))
  69. {
  70. /*
  71. * This is a really not so smart way of doing this... we provide this for backward compatability but this
  72. * WILL! disappear in a future version. If you are using this syntax change your code to use the standard
  73. * PHP callback array syntax: <http://php.net/callback>
  74. *
  75. * We have to use some silly global notation to pull it off and this is very unreliable
  76. */
  77. list ($object_123456789, $method) = explode('->', $callback);
  78. global $$object_123456789;
  79. $callback = array($$object_123456789, $method);
  80. }
  81. else
  82. {
  83. // We have just a standard function -- do nothing
  84. }
  85. if (!$id)
  86. {
  87. // Generate an ID
  88. $id = $this->_makeId($callback, $args);
  89. }
  90. $data = $this->cache->get($id);
  91. $locktest = new stdClass;
  92. $locktest->locked = null;
  93. $locktest->locklooped = null;
  94. if ($data === false)
  95. {
  96. $locktest = $this->cache->lock($id);
  97. if ($locktest->locked == true && $locktest->locklooped == true)
  98. {
  99. $data = $this->cache->get($id);
  100. }
  101. }
  102. $coptions = array();
  103. if ($data !== false)
  104. {
  105. $cached = unserialize(trim($data));
  106. $coptions['mergehead'] = isset($woptions['mergehead']) ? $woptions['mergehead'] : 0;
  107. $output = ($wrkarounds == false) ? $cached['output'] : JCache::getWorkarounds($cached['output'], $coptions);
  108. $result = $cached['result'];
  109. if ($locktest->locked == true)
  110. {
  111. $this->cache->unlock($id);
  112. }
  113. }
  114. else
  115. {
  116. if (!is_array($args))
  117. {
  118. $Args = !empty($args) ? array(&$args) : array();
  119. }
  120. else
  121. {
  122. $Args = &$args;
  123. }
  124. if ($locktest->locked == false)
  125. {
  126. $locktest = $this->cache->lock($id);
  127. }
  128. if (isset($woptions['modulemode']) && $woptions['modulemode'] == 1)
  129. {
  130. $document = JFactory::getDocument();
  131. $coptions['modulemode'] = 1;
  132. $coptions['headerbefore'] = $document->getHeadData();
  133. }
  134. else
  135. {
  136. $coptions['modulemode'] = 0;
  137. }
  138. ob_start();
  139. ob_implicit_flush(false);
  140. $result = call_user_func_array($callback, $Args);
  141. $output = ob_get_contents();
  142. ob_end_clean();
  143. $cached = array();
  144. $coptions['nopathway'] = isset($woptions['nopathway']) ? $woptions['nopathway'] : 1;
  145. $coptions['nohead'] = isset($woptions['nohead']) ? $woptions['nohead'] : 1;
  146. $coptions['nomodules'] = isset($woptions['nomodules']) ? $woptions['nomodules'] : 1;
  147. $cached['output'] = ($wrkarounds == false) ? $output : JCache::setWorkarounds($output, $coptions);
  148. $cached['result'] = $result;
  149. // Store the cache data
  150. $this->cache->store(serialize($cached), $id);
  151. if ($locktest->locked == true)
  152. {
  153. $this->cache->unlock($id);
  154. }
  155. }
  156. echo $output;
  157. return $result;
  158. }
  159. /**
  160. * Generate a callback cache id
  161. *
  162. * @param callback $callback Callback to cache
  163. * @param array $args Arguments to the callback method to cache
  164. *
  165. * @return string MD5 Hash : function cache id
  166. *
  167. * @since 11.1
  168. */
  169. protected function _makeId($callback, $args)
  170. {
  171. if (is_array($callback) && is_object($callback[0]))
  172. {
  173. $vars = get_object_vars($callback[0]);
  174. $vars[] = strtolower(get_class($callback[0]));
  175. $callback[0] = $vars;
  176. }
  177. return md5(serialize(array($callback, $args)));
  178. }
  179. }