[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Pear/PEAR/ -> Autoloader.php (source)

   1  <?php
   2  // /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP Version 5                                                        |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997-2004 The PHP Group                                |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 3.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available through the world-wide-web at the following url:           |
  11  // | http://www.php.net/license/3_0.txt.                                  |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Author: Stig Bakken <ssb@php.net>                                    |
  17  // |                                                                      |
  18  // +----------------------------------------------------------------------+
  19  //
  20  // $Id: Autoloader.php,v 1.11 2004/02/27 02:21:29 cellog Exp $
  21  
  22  if (!extension_loaded("overload")) {
  23      // die hard without ext/overload
  24      die("Rebuild PHP with the `overload' extension to use PEAR_Autoloader");
  25  }
  26  
  27  require_once "PEAR.php";
  28  
  29  /**
  30   * This class is for objects where you want to separate the code for
  31   * some methods into separate classes.  This is useful if you have a
  32   * class with not-frequently-used methods that contain lots of code
  33   * that you would like to avoid always parsing.
  34   *
  35   * The PEAR_Autoloader class provides autoloading and aggregation.
  36   * The autoloading lets you set up in which classes the separated
  37   * methods are found.  Aggregation is the technique used to import new
  38   * methods, an instance of each class providing separated methods is
  39   * stored and called every time the aggregated method is called.
  40   *
  41   * @author Stig Sæther Bakken <ssb@php.net>
  42   */
  43  class PEAR_Autoloader extends PEAR
  44  {
  45      // {{{ properties
  46  
  47      /**
  48       * Map of methods and classes where they are defined
  49       *
  50       * @var array
  51       *
  52       * @access private
  53       */
  54      var $_autoload_map = array();
  55  
  56      /**
  57       * Map of methods and aggregate objects
  58       *
  59       * @var array
  60       *
  61       * @access private
  62       */
  63      var $_method_map = array();
  64  
  65      // }}}
  66      // {{{ addAutoload()
  67  
  68      /**
  69       * Add one or more autoload entries.
  70       *
  71       * @param string $method     which method to autoload
  72       *
  73       * @param string $classname  (optional) which class to find the method in.
  74       *                           If the $method parameter is an array, this
  75       *                           parameter may be omitted (and will be ignored
  76       *                           if not), and the $method parameter will be
  77       *                           treated as an associative array with method
  78       *                           names as keys and class names as values.
  79       *
  80       * @return void
  81       *
  82       * @access public
  83       */
  84      function addAutoload($method, $classname = null)
  85      {
  86          if (is_array($method)) {
  87              array_walk($method, create_function('$a,&$b', '$b = strtolower($b);'));
  88              $this->_autoload_map = array_merge($this->_autoload_map, $method);
  89          } else {
  90              $this->_autoload_map[strtolower($method)] = $classname;
  91          }
  92      }
  93  
  94      // }}}
  95      // {{{ removeAutoload()
  96  
  97      /**
  98       * Remove an autoload entry.
  99       *
 100       * @param string $method  which method to remove the autoload entry for
 101       *
 102       * @return bool TRUE if an entry was removed, FALSE if not
 103       *
 104       * @access public
 105       */
 106      function removeAutoload($method)
 107      {
 108          $method = strtolower($method);
 109          $ok = isset($this->_autoload_map[$method]);
 110          unset($this->_autoload_map[$method]);
 111          return $ok;
 112      }
 113  
 114      // }}}
 115      // {{{ addAggregateObject()
 116  
 117      /**
 118       * Add an aggregate object to this object.  If the specified class
 119       * is not defined, loading it will be attempted following PEAR's
 120       * file naming scheme.  All the methods in the class will be
 121       * aggregated, except private ones (name starting with an
 122       * underscore) and constructors.
 123       *
 124       * @param string $classname  what class to instantiate for the object.
 125       *
 126       * @return void
 127       *
 128       * @access public
 129       */
 130      function addAggregateObject($classname)
 131      {
 132          $classname = strtolower($classname);
 133          if (!class_exists($classname)) {
 134              $include_file = preg_replace('/[^a-z0-9]/i', '_', $classname);
 135              include_once $include_file;
 136          }
 137          $obj =& new $classname;
 138          $methods = get_class_methods($classname);
 139          foreach ($methods as $method) {
 140              // don't import priviate methods and constructors
 141              if ($method{0} != '_' && $method != $classname) {
 142                  $this->_method_map[$method] = $obj;
 143              }
 144          }
 145      }
 146  
 147      // }}}
 148      // {{{ removeAggregateObject()
 149  
 150      /**
 151       * Remove an aggregate object.
 152       *
 153       * @param string $classname  the class of the object to remove
 154       *
 155       * @return bool  TRUE if an object was removed, FALSE if not
 156       *
 157       * @access public
 158       */
 159      function removeAggregateObject($classname)
 160      {
 161          $ok = false;
 162          $classname = strtolower($classname);
 163          reset($this->_method_map);
 164          while (list($method, $obj) = each($this->_method_map)) {
 165              if (is_a($obj, $classname)) {
 166                  unset($this->_method_map[$method]);
 167                  $ok = true;
 168              }
 169          }
 170          return $ok;
 171      }
 172  
 173      // }}}
 174      // {{{ __call()
 175  
 176      /**
 177       * Overloaded object call handler, called each time an
 178       * undefined/aggregated method is invoked.  This method repeats
 179       * the call in the right aggregate object and passes on the return
 180       * value.
 181       *
 182       * @param string $method  which method that was called
 183       *
 184       * @param string $args    An array of the parameters passed in the
 185       *                        original call
 186       *
 187       * @return mixed  The return value from the aggregated method, or a PEAR
 188       *                error if the called method was unknown.
 189       */
 190      function __call($method, $args, &$retval)
 191      {
 192          $method = strtolower($method);
 193          if (empty($this->_method_map[$method]) && isset($this->_autoload_map[$method])) {
 194              $this->addAggregateObject($this->_autoload_map[$method]);
 195          }
 196          if (isset($this->_method_map[$method])) {
 197              $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);
 198              return true;
 199          }
 200          return false;
 201      }
 202  
 203      // }}}
 204  }
 205  
 206  overload("PEAR_Autoloader");
 207  
 208  ?>


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7