PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 206 lines | 113 code | 26 blank | 67 comment | 20 complexity | b9515929b555d53ee22dc267f6a5d1d0 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, 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 = false;
  91. $data = $this->cache->get($id);
  92. $locktest = new stdClass;
  93. $locktest->locked = null;
  94. $locktest->locklooped = null;
  95. if ($data === false)
  96. {
  97. $locktest = $this->cache->lock($id);
  98. if ($locktest->locked == true && $locktest->locklooped == true)
  99. {
  100. $data = $this->cache->get($id);
  101. }
  102. }
  103. $coptions = array();
  104. if ($data !== false)
  105. {
  106. $cached = unserialize(trim($data));
  107. $coptions['mergehead'] = isset($woptions['mergehead']) ? $woptions['mergehead'] : 0;
  108. $output = ($wrkarounds == false) ? $cached['output'] : JCache::getWorkarounds($cached['output'], $coptions);
  109. $result = $cached['result'];
  110. if ($locktest->locked == true)
  111. {
  112. $this->cache->unlock($id);
  113. }
  114. }
  115. else
  116. {
  117. if (!is_array($args))
  118. {
  119. $Args = !empty($args) ? array(&$args) : array();
  120. }
  121. else
  122. {
  123. $Args = &$args;
  124. }
  125. if ($locktest->locked == false)
  126. {
  127. $locktest = $this->cache->lock($id);
  128. }
  129. if (isset($woptions['modulemode']))
  130. {
  131. $document = JFactory::getDocument();
  132. $coptions['modulemode'] = $woptions['modulemode'];
  133. $coptions['headerbefore'] = $document->getHeadData();
  134. }
  135. else
  136. {
  137. $coptions['modulemode'] = 0;
  138. }
  139. ob_start();
  140. ob_implicit_flush(false);
  141. $result = call_user_func_array($callback, $Args);
  142. $output = ob_get_contents();
  143. ob_end_clean();
  144. $cached = array();
  145. $coptions['nopathway'] = isset($woptions['nopathway']) ? $woptions['nopathway'] : 1;
  146. $coptions['nohead'] = isset($woptions['nohead']) ? $woptions['nohead'] : 1;
  147. $coptions['nomodules'] = isset($woptions['nomodules']) ? $woptions['nomodules'] : 1;
  148. $cached['output'] = ($wrkarounds == false) ? $output : JCache::setWorkarounds($output, $coptions);
  149. $cached['result'] = $result;
  150. // Store the cache data
  151. $this->cache->store(serialize($cached), $id);
  152. if ($locktest->locked == true)
  153. {
  154. $this->cache->unlock($id);
  155. }
  156. }
  157. echo $output;
  158. return $result;
  159. }
  160. /**
  161. * Generate a callback cache id
  162. *
  163. * @param callback $callback Callback to cache
  164. * @param array $args Arguments to the callback method to cache
  165. *
  166. * @return string MD5 Hash : function cache id
  167. *
  168. * @since 11.1
  169. */
  170. protected function _makeId($callback, $args)
  171. {
  172. if (is_array($callback) && is_object($callback[0]))
  173. {
  174. $vars = get_object_vars($callback[0]);
  175. $vars[] = strtolower(get_class($callback[0]));
  176. $callback[0] = $vars;
  177. }
  178. return md5(serialize(array($callback, $args)));
  179. }
  180. }