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