7 * LICENSE: This source file is subject to version 3.0 of the PHP license
\r
8 * that is available through the world-wide-web at the following URI:
\r
9 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
\r
10 * the PHP License and are unable to obtain it through the web, please
\r
11 * send a note to license@php.net so we can mail you a copy immediately.
\r
15 * @author Stig Bakken <ssb@php.net>
\r
16 * @copyright 1997-2008 The PHP Group
\r
17 * @license http://www.php.net/license/3_0.txt PHP License 3.0
\r
18 * @version CVS: $Id$
\r
19 * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
\r
20 * @since File available since Release 0.1
\r
21 * @deprecated File deprecated in Release 1.4.0a1
\r
24 // /* vim: set expandtab tabstop=4 shiftwidth=4: */
\r
26 if (!extension_loaded("overload")) {
\r
27 // die hard without ext/overload
\r
28 die("Rebuild PHP with the `overload' extension to use PEAR_Autoloader");
\r
32 * Include for PEAR_Error and PEAR classes
\r
34 require_once "PEAR.php";
\r
37 * This class is for objects where you want to separate the code for
\r
38 * some methods into separate classes. This is useful if you have a
\r
39 * class with not-frequently-used methods that contain lots of code
\r
40 * that you would like to avoid always parsing.
\r
42 * The PEAR_Autoloader class provides autoloading and aggregation.
\r
43 * The autoloading lets you set up in which classes the separated
\r
44 * methods are found. Aggregation is the technique used to import new
\r
45 * methods, an instance of each class providing separated methods is
\r
46 * stored and called every time the aggregated method is called.
\r
50 * @author Stig Bakken <ssb@php.net>
\r
51 * @copyright 1997-2008 The PHP Group
\r
52 * @license http://www.php.net/license/3_0.txt PHP License 3.0
\r
53 * @version Release: 1.4.4
\r
54 * @link http://pear.php.net/manual/en/core.ppm.php#core.ppm.pear-autoloader
\r
55 * @since File available since Release 0.1
\r
56 * @deprecated File deprecated in Release 1.4.0a1
\r
58 class PEAR_Autoloader extends PEAR
\r
63 * Map of methods and classes where they are defined
\r
69 var $_autoload_map = array();
\r
72 * Map of methods and aggregate objects
\r
78 var $_method_map = array();
\r
81 // {{{ addAutoload()
\r
84 * Add one or more autoload entries.
\r
86 * @param string $method which method to autoload
\r
88 * @param string $classname (optional) which class to find the method in.
\r
89 * If the $method parameter is an array, this
\r
90 * parameter may be omitted (and will be ignored
\r
91 * if not), and the $method parameter will be
\r
92 * treated as an associative array with method
\r
93 * names as keys and class names as values.
\r
99 function addAutoload($method, $classname = null)
\r
101 if (is_array($method)) {
\r
102 array_walk($method, create_function('$a,&$b', '$b = strtolower($b);'));
\r
103 $this->_autoload_map = array_merge($this->_autoload_map, $method);
\r
105 $this->_autoload_map[strtolower($method)] = $classname;
\r
110 // {{{ removeAutoload()
\r
113 * Remove an autoload entry.
\r
115 * @param string $method which method to remove the autoload entry for
\r
117 * @return bool TRUE if an entry was removed, FALSE if not
\r
121 function removeAutoload($method)
\r
123 $method = strtolower($method);
\r
124 $ok = isset($this->_autoload_map[$method]);
\r
125 unset($this->_autoload_map[$method]);
\r
130 // {{{ addAggregateObject()
\r
133 * Add an aggregate object to this object. If the specified class
\r
134 * is not defined, loading it will be attempted following PEAR's
\r
135 * file naming scheme. All the methods in the class will be
\r
136 * aggregated, except private ones (name starting with an
\r
137 * underscore) and constructors.
\r
139 * @param string $classname what class to instantiate for the object.
\r
145 function addAggregateObject($classname)
\r
147 $classname = strtolower($classname);
\r
148 if (!class_exists($classname)) {
\r
149 $include_file = preg_replace('/[^a-z0-9]/i', '_', $classname);
\r
150 include_once $include_file;
\r
152 $obj = new $classname;
\r
153 $methods = get_class_methods($classname);
\r
154 foreach ($methods as $method) {
\r
155 // don't import priviate methods and constructors
\r
156 if ($method{0} != '_' && $method != $classname) {
\r
157 $this->_method_map[$method] = $obj;
\r
163 // {{{ removeAggregateObject()
\r
166 * Remove an aggregate object.
\r
168 * @param string $classname the class of the object to remove
\r
170 * @return bool TRUE if an object was removed, FALSE if not
\r
174 function removeAggregateObject($classname)
\r
177 $classname = strtolower($classname);
\r
178 reset($this->_method_map);
\r
179 while (list($method, $obj) = each($this->_method_map)) {
\r
180 if (is_a($obj, $classname)) {
\r
181 unset($this->_method_map[$method]);
\r
192 * Overloaded object call handler, called each time an
\r
193 * undefined/aggregated method is invoked. This method repeats
\r
194 * the call in the right aggregate object and passes on the return
\r
197 * @param string $method which method that was called
\r
199 * @param string $args An array of the parameters passed in the
\r
202 * @return mixed The return value from the aggregated method, or a PEAR
\r
203 * error if the called method was unknown.
\r
205 function __call($method, $args, &$retval)
\r
207 $method = strtolower($method);
\r
208 if (empty($this->_method_map[$method]) && isset($this->_autoload_map[$method])) {
\r
209 $this->addAggregateObject($this->_autoload_map[$method]);
\r
211 if (isset($this->_method_map[$method])) {
\r
212 $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);
\r
221 overload("PEAR_Autoloader");
\r