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

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

https://github.com/bhar1red/anahita
PHP | 133 lines | 54 code | 11 blank | 68 comment | 8 complexity | 45359725097ed24e8e60906a7d1dade8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: callback.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Cache
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. /**
  17. * Joomla! Cache callback type object
  18. *
  19. * @package Joomla.Framework
  20. * @subpackage Cache
  21. * @since 1.5
  22. */
  23. class JCacheCallback extends JCache
  24. {
  25. /**
  26. * Executes a cacheable callback if not found in cache else returns cached output and result
  27. *
  28. * Since arguments to this function are read with func_get_args you can pass any number of arguments to this method
  29. * as long as the first argument passed is the callback definition.
  30. *
  31. * The callback definition can be in several forms:
  32. * - Standard PHP Callback array <http://php.net/callback> [recommended]
  33. * - Function name as a string eg. 'foo' for function foo()
  34. * - Static method name as a string eg. 'MyClass::myMethod' for method myMethod() of class MyClass
  35. *
  36. * @access public
  37. * @return mixed Result of the callback
  38. * @since 1.5
  39. */
  40. function call()
  41. {
  42. // Get callback and arguments
  43. $args = func_get_args();
  44. $callback = array_shift($args);
  45. return $this->get( $callback, $args );
  46. }
  47. /**
  48. * Executes a cacheable callback if not found in cache else returns cached output and result
  49. *
  50. * @access public
  51. * @param mixed Callback or string shorthand for a callback
  52. * @param array Callback arguments
  53. * @return mixed Result of the callback
  54. * @since 1.5
  55. */
  56. function get( $callback, $args, $id=false )
  57. {
  58. // Normalize callback
  59. if (is_array( $callback ) || $callback instanceof Closure) {
  60. // We have a standard php callback array -- do nothing
  61. } elseif (strstr( $callback, '::' )) {
  62. // This is shorthand for a static method callback classname::methodname
  63. list( $class, $method ) = explode( '::', $callback );
  64. $callback = array( trim($class), trim($method) );
  65. } elseif (strstr( $callback, '->' )) {
  66. /*
  67. * This is a really not so smart way of doing this... we provide this for backward compatability but this
  68. * WILL!!! disappear in a future version. If you are using this syntax change your code to use the standard
  69. * PHP callback array syntax: <http://php.net/callback>
  70. *
  71. * We have to use some silly global notation to pull it off and this is very unreliable
  72. */
  73. list( $object_123456789, $method ) = explode('->', $callback);
  74. global $$object_123456789;
  75. $callback = array( $$object_123456789, $method );
  76. } else {
  77. // We have just a standard function -- do nothing
  78. }
  79. if (!$id) {
  80. // Generate an ID
  81. $id = $this->_makeId($callback, $args);
  82. }
  83. // Get the storage handler and get callback cache data by id and group
  84. $data = parent::get($id);
  85. if ($data !== false) {
  86. $cached = unserialize( $data );
  87. $output = $cached['output'];
  88. $result = $cached['result'];
  89. } else {
  90. ob_start();
  91. ob_implicit_flush( false );
  92. $result = call_user_func_array($callback, $args);
  93. $output = ob_get_contents();
  94. ob_end_clean();
  95. $cached = array();
  96. $cached['output'] = $output;
  97. $cached['result'] = $result;
  98. // Store the cache data
  99. $this->store(serialize($cached), $id);
  100. }
  101. echo $output;
  102. return $result;
  103. }
  104. /**
  105. * Generate a callback cache id
  106. *
  107. * @access private
  108. * @param callback $callback Callback to cache
  109. * @param array $args Arguments to the callback method to cache
  110. * @return string MD5 Hash : function cache id
  111. * @since 1.5
  112. */
  113. function _makeId($callback, $args)
  114. {
  115. if(is_array($callback) && is_object($callback[0])) {
  116. $vars = get_object_vars($callback[0]);
  117. $vars[] = strtolower(get_class($callback[0]));
  118. $callback[0] = $vars;
  119. }
  120. return md5(serialize(array($callback, $args)));
  121. }
  122. }