[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/cache/handler/ -> callback.php (source)

   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  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Joomla! Cache callback type object
  20   *
  21   * @package        Joomla.Framework
  22   * @subpackage    Cache
  23   * @since        1.5
  24   */
  25  class JCacheCallback extends JCache
  26  {
  27      /**
  28       * Executes a cacheable callback if not found in cache else returns cached output and result
  29       *
  30       * Since arguments to this function are read with func_get_args you can pass any number of arguments to this method
  31       * as long as the first argument passed is the callback definition.
  32       *
  33       * The callback definition can be in several forms:
  34       *     - Standard PHP Callback array <http://php.net/callback> [recommended]
  35       *     - Function name as a string eg. 'foo' for function foo()
  36       *     - Static method name as a string eg. 'MyClass::myMethod' for method myMethod() of class MyClass
  37       *
  38       * @access    public
  39       * @return    mixed    Result of the callback
  40       * @since    1.5
  41       */
  42  	function call()
  43      {
  44          // Get callback and arguments
  45          $args        = func_get_args();
  46          $callback    = array_shift($args);
  47  
  48          return $this->get( $callback, $args );
  49      }
  50  
  51      /**
  52       * Executes a cacheable callback if not found in cache else returns cached output and result
  53       *
  54       * @access    public
  55       * @param    mixed    Callback or string shorthand for a callback
  56       * @param    array    Callback arguments
  57       * @return    mixed    Result of the callback
  58       * @since    1.5
  59       */
  60  	function get( $callback, $args, $id=false )
  61      {
  62          // Normalize callback
  63          if (is_array( $callback )) {
  64              // We have a standard php callback array -- do nothing
  65          } elseif (strstr( $callback, '::' )) {
  66              // This is shorthand for a static method callback classname::methodname
  67              list( $class, $method ) = explode( '::', $callback );
  68              $callback = array( trim($class), trim($method) );
  69          } elseif (strstr( $callback, '->' )) {
  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          } else {
  81              // We have just a standard function -- do nothing
  82          }
  83  
  84          if (!$id) {
  85              // Generate an ID
  86              $id = $this->_makeId($callback, $args);
  87          }
  88  
  89          // Get the storage handler and get callback cache data by id and group
  90          $data = parent::get($id);
  91          if ($data !== false) {
  92              $cached = unserialize( $data );
  93              $output = $cached['output'];
  94              $result = $cached['result'];
  95          } else {
  96              ob_start();
  97              ob_implicit_flush( false );
  98  
  99              $result = call_user_func_array($callback, $args);
 100              $output = ob_get_contents();
 101  
 102              ob_end_clean();
 103  
 104              $cached = array();
 105              $cached['output'] = $output;
 106              $cached['result'] = $result;
 107              // Store the cache data
 108              $this->store(serialize($cached), $id);
 109          }
 110  
 111          echo $output;
 112          return $result;
 113      }
 114  
 115      /**
 116       * Generate a callback cache id
 117       *
 118       * @access    private
 119       * @param    callback    $callback    Callback to cache
 120       * @param    array        $args    Arguments to the callback method to cache
 121       * @return    string    MD5 Hash : function cache id
 122       * @since    1.5
 123       */
 124  	function _makeId($callback, $args)
 125      {
 126          if(is_array($callback) && is_object($callback[0])) {
 127              $vars = get_object_vars($callback[0]);
 128              $vars[] = strtolower(get_class($callback[0]));
 129              $callback[0] = $vars;
 130          }
 131          return md5(serialize(array($callback, $args)));
 132      }
 133  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1