Source for file 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.1 2005/07/22 01:57:13 eddieajau Exp $
  15. @author Sebastian BERGMANN <sb@sebastian-bergmann.de>
  16. @author Fabien MARTY <fab@php.net>
  17. */
  18.  
  19. /** ensure this file is being included by a parent file */
  20. defined'_VALID_MOS' or die'Direct Access to this location is not allowed.' );
  21.  
  22. require_once$mosConfig_absolute_path '/includes/Cache/Lite.php' );
  23.  
  24. {
  25.  
  26.     // --- Private properties ---
  27.     
  28.     /**
  29.     * Default cache group for function caching
  30.     *
  31.     * @var string $_defaultGroup 
  32.     */
  33.     var $_defaultGroup = 'Cache_Lite_Function';
  34.     
  35.     // --- Public methods ----
  36.     
  37.     /**
  38.     * Constructor
  39.     *
  40.     * $options is an assoc. To have a look at availables options,
  41.     * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
  42.     *
  43.     * Comparing to Cache_Lite constructor, there is another option :
  44.     * $options = array(
  45.     *     (...) see Cache_Lite constructor
  46.     *     'defaultGroup' => default cache group for function caching (string)
  47.     * );
  48.     *
  49.     * @param array $options options
  50.     * @access public
  51.     */
  52.     function Cache_Lite_Function($options array(NULL))
  53.     {
  54.         if (isset($options['defaultGroup'])) {
  55.             $this->_defaultGroup = $options['defaultGroup'];
  56.         }
  57.         $this->Cache_Lite($options);
  58.     }
  59.     
  60.     /**
  61.     * Calls a cacheable function or method (or not if there is already a cache for it)
  62.     *
  63.     * Arguments of this method are read with func_get_args. So it doesn't appear
  64.     * in the function definition. Synopsis :
  65.     * call('functionName', $arg1, $arg2, ...)
  66.     * (arg1, arg2... are arguments of 'functionName')
  67.     *
  68.     * @return mixed result of the function/method
  69.     * @access public
  70.     */
  71.     function call()
  72.     {
  73.         $arguments func_get_args();
  74.         $id serialize($arguments)// Generate a cache id
  75.         if (!$this->_fileNameProtection{
  76.             $id md5($id);
  77.             // if fileNameProtection is set to false, then the id has to be hashed
  78.             // because it's a very bad file name in most cases
  79.         }
  80.         $data $this->get($id$this->_defaultGroup);
  81.         if ($data !== false{
  82.             $array unserialize($data);
  83.             $output $array['output'];
  84.             $result $array['result'];
  85.         else {
  86.             ob_start();
  87.             ob_implicit_flush(false);
  88.             $target array_shift($arguments);
  89.             if (strstr($target'::')) // classname::staticMethod
  90.                 list($class$methodexplode('::'$target);
  91.                 $result call_user_func_array(array($class$method)$arguments);
  92.             else if (strstr($target'->')) // object->method
  93.                 // use a stupid name ($objet_123456789 because) of problems when the object
  94.                 // name is the same as this var name
  95.                 list($object_123456789$methodexplode('->'$target);
  96.                 global $$object_123456789;
  97.                 $result call_user_func_array(array($$object_123456789$method)$arguments);
  98.             else // function
  99.                 $result call_user_func_array($target$arguments);
  100.             }
  101.             $output ob_get_contents();
  102.             ob_end_clean();
  103.             $array['output'$output;
  104.             $array['result'$result;
  105.             $this->save(serialize($array)$id$this->_defaultGroup);
  106.         }
  107.         echo($output);
  108.         return $result;
  109.     }
  110.     
  111. }
  112.  
  113. ?>

Documentation generated on Mon, 05 May 2008 16:19:47 +0400 by phpDocumentor 1.4.0