Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/cache/handlers/callback.php

Documentation is available at callback.php

  1. <?php
  2. /**
  3. @version        $Id: callback.php 6673 2007-02-19 05:12:48Z louis $
  4. @package        Joomla.Framework
  5. @subpackage    Cache
  6. @copyright    Copyright (C) 2005 - 2007 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.  * @author        Louis Landry <[email protected]>
  22.  * @package        Joomla.Framework
  23.  * @subpackage    Cache
  24.  * @since        1.5
  25.  */
  26. class JCacheCallback extends JCache
  27. {
  28.     /**
  29.      * Executes a cacheable callback if not found in cache else returns cached output and result
  30.      *
  31.      * Since arguments to this function are read with func_get_args you can pass any number of arguments to this method
  32.      * as long as the first argument passed is the callback definition.
  33.      *
  34.      * The callback definition can be in several forms:
  35.      *     - Standard PHP Callback array <http://php.net/callback> [recommended]
  36.      *     - Function name as a string eg. 'foo' for function foo()
  37.      *     - Static method name as a string eg. 'MyClass::myMethod' for method myMethod() of class MyClass
  38.      *
  39.      * @access    public
  40.      * @return    mixed    Result of the callback
  41.      * @since    1.5
  42.      */
  43.     function call()
  44.     {
  45.         // Get callback and arguments
  46.         $args        func_get_args();
  47.         $callback    array_shift($args);
  48.  
  49.         return $this->get$callback$args );
  50.     }
  51.  
  52.     /**
  53.      * Executes a cacheable callback if not found in cache else returns cached output and result
  54.      *
  55.      * @access    public
  56.      * @param    mixed    Callback or string shorthand for a callback
  57.      * @param    array    Callback arguments
  58.      * @return    mixed    Result of the callback
  59.      * @since    1.5
  60.      */
  61.     function get$callback$args$id=false )
  62.     {
  63.         // Normalize callback
  64.         if (is_array$callback )) {
  65.             // We have a standard php callback array -- do nothing
  66.         elseif (strstr$callback'::' )) {
  67.             // This is shorthand for a static method callback classname::methodname
  68.             list$class$method explode'::'$callback );
  69.             $callback arraytrim($class)trim($method) );
  70.         elseif (strstr$callback'->' )) {
  71.             /*
  72.              * This is a really not so smart way of doing this... we provide this for backward compatability but this
  73.              * WILL!!! disappear in a future version.  If you are using this syntax change your code to use the standard
  74.              * PHP callback array syntax: <http://php.net/callback>
  75.              *
  76.              * We have to use some silly global notation to pull it off and this is very unreliable
  77.              */
  78.             list$object_123456789$method explode('->'$callback);
  79.             global $$object_123456789;
  80.             $callback array$$object_123456789$method );
  81.         else {
  82.             // We have just a standard function -- do nothing
  83.         }
  84.  
  85.         if (!$id{
  86.             // Generate an ID
  87.             $id $this->_makeId($callback$args);
  88.         }
  89.  
  90.         // Get the storage handler and get callback cache data by id and group
  91.         $data parent::get($id);
  92.         if ($data !== false{
  93.             $cached unserialize$data );
  94.             $output $cached['output'];
  95.             $result $cached['result'];
  96.         else {
  97.             ob_start();
  98.             ob_implicit_flushfalse );
  99.  
  100.             $result call_user_func_array($callback$args);
  101.             $output ob_get_contents();
  102.  
  103.             ob_end_clean();
  104.  
  105.             $cached array();
  106.             $cached['output'$output;
  107.             $cached['result'$result;
  108.             // Store the cache data
  109.             $this->store(serialize($cached)$id);
  110.         }
  111.  
  112.         echo $output;
  113.         return $result;
  114.     }
  115.  
  116.     /**
  117.      * Generate a callback cache id
  118.      *
  119.      * @access    private
  120.      * @param    callback    $callback    Callback to cache
  121.      * @param    array        $args    Arguments to the callback method to cache
  122.      * @return    string    MD5 Hash : function cache id
  123.      * @since    1.5
  124.      */
  125.     function _makeId($callback$args)
  126.     {
  127.         return md5(serialize(array($callback$args)));
  128.     }
  129. }
  130. ?>

Documentation generated on Mon, 05 Mar 2007 20:53:46 +0000 by phpDocumentor 1.3.1