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

/libraries/joomla/cache/handler/callback.php

https://github.com/joebushi/joomla
PHP | 128 lines | 54 code | 11 blank | 63 comment | 7 complexity | 01ed5a84a67373a63f96b0e808e07180 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('JPATH_BASE') or die;
  11. /**
  12. * Joomla! Cache callback type object
  13. *
  14. * @package Joomla.Framework
  15. * @subpackage Cache
  16. * @since 1.5
  17. */
  18. class JCacheCallback extends JCache
  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. * @access public
  32. * @return mixed Result of the callback
  33. * @since 1.5
  34. */
  35. 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. * @access public
  46. * @param mixed Callback or string shorthand for a callback
  47. * @param array Callback arguments
  48. * @return mixed Result of the callback
  49. * @since 1.5
  50. */
  51. function get($callback, $args, $id=false)
  52. {
  53. // Normalize callback
  54. if (is_array($callback)) {
  55. // We have a standard php callback array -- do nothing
  56. } elseif (strstr($callback, '::')) {
  57. // This is shorthand for a static method callback classname::methodname
  58. list($class, $method) = explode('::', $callback);
  59. $callback = array(trim($class), trim($method));
  60. } elseif (strstr($callback, '->')) {
  61. /*
  62. * This is a really not so smart way of doing this... we provide this for backward compatability but this
  63. * WILL!!! disappear in a future version. If you are using this syntax change your code to use the standard
  64. * PHP callback array syntax: <http://php.net/callback>
  65. *
  66. * We have to use some silly global notation to pull it off and this is very unreliable
  67. */
  68. list($object_123456789, $method) = explode('->', $callback);
  69. global $$object_123456789;
  70. $callback = array($$object_123456789, $method);
  71. } else {
  72. // We have just a standard function -- do nothing
  73. }
  74. if (!$id) {
  75. // Generate an ID
  76. $id = $this->_makeId($callback, $args);
  77. }
  78. // Get the storage handler and get callback cache data by id and group
  79. $data = parent::get($id);
  80. if ($data !== false) {
  81. $cached = unserialize($data);
  82. $output = $cached['output'];
  83. $result = $cached['result'];
  84. } else {
  85. ob_start();
  86. ob_implicit_flush(false);
  87. $result = call_user_func_array($callback, $args);
  88. $output = ob_get_contents();
  89. ob_end_clean();
  90. $cached = array();
  91. $cached['output'] = $output;
  92. $cached['result'] = $result;
  93. // Store the cache data
  94. $this->store(serialize($cached), $id);
  95. }
  96. echo $output;
  97. return $result;
  98. }
  99. /**
  100. * Generate a callback cache id
  101. *
  102. * @access private
  103. * @param callback $callback Callback to cache
  104. * @param array $args Arguments to the callback method to cache
  105. * @return string MD5 Hash : function cache id
  106. * @since 1.5
  107. */
  108. function _makeId($callback, $args)
  109. {
  110. if (is_array($callback) && is_object($callback[0])) {
  111. $vars = get_object_vars($callback[0]);
  112. $vars[] = strtolower(get_class($callback[0]));
  113. $callback[0] = $vars;
  114. }
  115. return md5(serialize(array($callback, $args)));
  116. }
  117. }