Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Cache_Lite

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 /pear/cache/lite/Function.php

Documentation is available at Function.php

  1. <?php
  2.  
  3. /**
  4. * This class extends Cache_Lite and can be used to cache the result and output of functions/methods
  5. *
  6. * This class is completly inspired from Sebastian Bergmann's
  7. * PEAR/Cache_Function class. This is only an adaptation to
  8. * Cache_Lite
  9. *
  10. * There are some examples in the 'docs/examples' file
  11. * Technical choices are described in the 'docs/technical' file
  12. *
  13. @package Cache_Lite
  14. @version $Id: Function.php,v 1.10 2006/02/04 18:36:36 fab Exp $
  15. @author Sebastian BERGMANN <[email protected]>
  16. @author Fabien MARTY <[email protected]>
  17. */
  18.  
  19. // Check to ensure this file is within the rest of the framework
  20. defined('JPATH_BASE'or die();
  21.  
  22. //require_once('Cache/Lite.php');
  23. jimport('pear.cache.Lite');
  24.  
  25. {
  26.  
  27.     // --- Private properties ---
  28.  
  29.     
  30.     /**
  31.      * Default cache group for function caching
  32.      *
  33.      * @var string $_defaultGroup 
  34.      */
  35.     var $_defaultGroup = 'Cache_Lite_Function';
  36.  
  37.     /**
  38.      * Don't cache the method call when its output contains the string "NOCACHE"
  39.      *
  40.      * if set to true, the output of the method will never be displayed (because the output is used
  41.      * to control the cache)
  42.      *
  43.      * @var boolean $_dontCacheWhenTheOutputContainsNOCACHE 
  44.      */
  45.  
  46.     /**
  47.      * Don't cache the method call when its result is false
  48.      *
  49.      * @var boolean $_dontCacheWhenTheResultIsFalse 
  50.      */
  51.     var $_dontCacheWhenTheResultIsFalse = false;
  52.  
  53.     /**
  54.      * Don't cache the method call when its result is null
  55.      *
  56.      * @var boolean $_dontCacheWhenTheResultIsNull 
  57.      */
  58.     var $_dontCacheWhenTheResultIsNull = false;
  59.  
  60.     /**
  61.      * Debug the Cache_Lite_Function caching process
  62.      *
  63.      * @var boolean $_debugCacheLiteFunction 
  64.      */
  65.     var $_debugCacheLiteFunction = false;
  66.  
  67.     // --- Public methods ----
  68.  
  69.     
  70.     /**
  71.     * Constructor
  72.     *
  73.     * $options is an assoc. To have a look at availables options,
  74.     * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
  75.     *
  76.     * Comparing to Cache_Lite constructor, there is another option :
  77.     * $options = array(
  78.     *     (...) see Cache_Lite constructor
  79.     *     'debugCacheLiteFunction' => (bool) debug the caching process,
  80.     *     'defaultGroup' => default cache group for function caching (string),
  81.     *     'dontCacheWhenTheOutputContainsNOCACHE' => (bool) don't cache when the function output contains "NOCACHE",
  82.     *     'dontCacheWhenTheResultIsFalse' => (bool) don't cache when the function result is false,
  83.     *     'dontCacheWhenTheResultIsNull' => (bool don't cache when the function result is null
  84.     * );
  85.     *
  86.     * @param array $options options
  87.     * @access public
  88.     */
  89.     function Cache_Lite_Function($options array(NULL))
  90.     {
  91.         $availableOptions array('debugCacheLiteFunction''defaultGroup''dontCacheWhenTheOutputContainsNOCACHE''dontCacheWhenTheResultIsFalse''dontCacheWhenTheResultIsNull');
  92.         while (list($name$valueeach($options)) {
  93.             if (in_array($name$availableOptions)) {
  94.                 $property '_'.$name;
  95.                 $this->$property $value;
  96.             }
  97.         }
  98.         reset($options);
  99.         $this->Cache_Lite($options);
  100.     }
  101.  
  102.     /**
  103.     * Calls a cacheable function or method (or not if there is already a cache for it)
  104.     *
  105.     * Arguments of this method are read with func_get_args. So it doesn't appear
  106.     * in the function definition. Synopsis :
  107.     * call('functionName', $arg1, $arg2, ...)
  108.     * (arg1, arg2... are arguments of 'functionName')
  109.     *
  110.     * @return mixed result of the function/method
  111.     * @access public
  112.     */
  113.     function call()
  114.     {
  115.         $arguments func_get_args();
  116.         $id $this->_makeId($arguments);
  117.         $data $this->get($id$this->_defaultGroup);
  118.         if ($data !== false{
  119.             if ($this->_debugCacheLiteFunction{
  120.                 echo "Cache hit !\n";
  121.             }
  122.             $array unserialize($data);
  123.             $output $array['output'];
  124.             $result $array['result'];
  125.         else {
  126.             if ($this->_debugCacheLiteFunction{
  127.                 echo "Cache missed !\n";
  128.             }
  129.             ob_start();
  130.             ob_implicit_flush(false);
  131.             $target array_shift($arguments);
  132.             if (is_array($target)) {
  133.                 // in this case, $target is for example array($obj, 'method')
  134.                 $object $target[0];
  135.                 $method $target[1];
  136.                 $result call_user_func_array(array(&$object$method)$arguments);
  137.             else {
  138.                 if (strstr($target'::')) // classname::staticMethod
  139.                     list($class$methodexplode('::'$target);
  140.                     $result call_user_func_array(array($class$method)$arguments);
  141.                 else if (strstr($target'->')) // object->method
  142.                     // use a stupid name ($objet_123456789 because) of problems where the object
  143.                     // name is the same as this var name
  144.                     list($object_123456789$methodexplode('->'$target);
  145.                     global $$object_123456789;
  146.                     $result call_user_func_array(array($$object_123456789$method)$arguments);
  147.                 else // function
  148.                     $result call_user_func_array($target$arguments);
  149.                 }
  150.             }
  151.             $output ob_get_contents();
  152.             ob_end_clean();
  153.             if ($this->_dontCacheWhenTheResultIsFalse{
  154.                 if ((is_bool($result)) && (!($result))) {
  155.                     echo($output);
  156.                     return $result;
  157.                 }
  158.             }
  159.             if ($this->_dontCacheWhenTheResultIsNull{
  160.                 if (is_null($result)) {
  161.                     echo($output);
  162.                     return $result;
  163.                 }
  164.             }
  165.             if ($this->_dontCacheWhenTheOutputContainsNOCACHE{
  166.                 if (strpos($output'NOCACHE'> -1{
  167.                     return $result;
  168.                 }
  169.             }
  170.             $array['output'$output;
  171.             $array['result'$result;
  172.             $this->save(serialize($array)$id$this->_defaultGroup);
  173.         }
  174.         echo($output);
  175.         return $result;
  176.     }
  177.  
  178.     /**
  179.     * Drop a cache file
  180.     *
  181.     * Arguments of this method are read with func_get_args. So it doesn't appear
  182.     * in the function definition. Synopsis :
  183.     * remove('functionName', $arg1, $arg2, ...)
  184.     * (arg1, arg2... are arguments of 'functionName')
  185.     *
  186.     * @return boolean true if no problem
  187.     * @access public
  188.     */
  189.     function drop()
  190.     {
  191.         $id $this->_makeId(func_get_args());
  192.         $this->remove($id$this->_defaultGroup);
  193.     }
  194.  
  195.     /**
  196.     * Make an id for the cache
  197.     *
  198.     * @var array result of func_get_args for the call() or the remove() method
  199.     * @return string id
  200.     * @access private
  201.     */
  202.     function _makeId($arguments)
  203.     {
  204.         $id serialize($arguments)// Generate a cache id
  205.         if (!$this->_fileNameProtection{
  206.             $id md5($id);
  207.             // if fileNameProtection is set to false, then the id has to be hashed
  208.             // because it's a very bad file name in most cases
  209.         }
  210.         return $id;
  211.     }
  212.  
  213. }
  214.  
  215. ?>

Documentation generated on Mon, 05 Mar 2007 21:02:14 +0000 by phpDocumentor 1.3.1