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