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