home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Function.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  3.6 KB  |  112 lines

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