[ Index ]
 

Code source de Joomla 1.0.13

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/includes/Cache/Lite/ -> Function.php (source)

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


Généré le : Wed Nov 21 14:43:32 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics