moved code up one level to eliminate the docs subdirectory
[acontent.git] / include / classes / XML / XML_HTMLSax / PEAR / Autoloader.php
1 <?php\r
2 /**\r
3  * Class auto-loader\r
4  *\r
5  * PHP versions 4\r
6  *\r
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
12  *\r
13  * @category   pear\r
14  * @package    PEAR\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: Autoloader.php 8901 2009-11-11 19:10:19Z cindy $\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
22  */\r
23 \r
24 // /* vim: set expandtab tabstop=4 shiftwidth=4: */\r
25 \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
29 }\r
30 \r
31 /**\r
32  * Include for PEAR_Error and PEAR classes\r
33  */\r
34 require_once "PEAR.php";\r
35 \r
36 /**\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
41  *\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
47  *\r
48  * @category   pear\r
49  * @package    PEAR\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
57  */\r
58 class PEAR_Autoloader extends PEAR\r
59 {\r
60     // {{{ properties\r
61 \r
62     /**\r
63      * Map of methods and classes where they are defined\r
64      *\r
65      * @var array\r
66      *\r
67      * @access private\r
68      */\r
69     var $_autoload_map = array();\r
70 \r
71     /**\r
72      * Map of methods and aggregate objects\r
73      *\r
74      * @var array\r
75      *\r
76      * @access private\r
77      */\r
78     var $_method_map = array();\r
79 \r
80     // }}}\r
81     // {{{ addAutoload()\r
82 \r
83     /**\r
84      * Add one or more autoload entries.\r
85      *\r
86      * @param string $method     which method to autoload\r
87      *\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
94      *\r
95      * @return void\r
96      *\r
97      * @access public\r
98      */\r
99     function addAutoload($method, $classname = null)\r
100     {\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
104         } else {\r
105             $this->_autoload_map[strtolower($method)] = $classname;\r
106         }\r
107     }\r
108 \r
109     // }}}\r
110     // {{{ removeAutoload()\r
111 \r
112     /**\r
113      * Remove an autoload entry.\r
114      *\r
115      * @param string $method  which method to remove the autoload entry for\r
116      *\r
117      * @return bool TRUE if an entry was removed, FALSE if not\r
118      *\r
119      * @access public\r
120      */\r
121     function removeAutoload($method)\r
122     {\r
123         $method = strtolower($method);\r
124         $ok = isset($this->_autoload_map[$method]);\r
125         unset($this->_autoload_map[$method]);\r
126         return $ok;\r
127     }\r
128 \r
129     // }}}\r
130     // {{{ addAggregateObject()\r
131 \r
132     /**\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
138      *\r
139      * @param string $classname  what class to instantiate for the object.\r
140      *\r
141      * @return void\r
142      *\r
143      * @access public\r
144      */\r
145     function addAggregateObject($classname)\r
146     {\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
151         }\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
158             }\r
159         }\r
160     }\r
161 \r
162     // }}}\r
163     // {{{ removeAggregateObject()\r
164 \r
165     /**\r
166      * Remove an aggregate object.\r
167      *\r
168      * @param string $classname  the class of the object to remove\r
169      *\r
170      * @return bool  TRUE if an object was removed, FALSE if not\r
171      *\r
172      * @access public\r
173      */\r
174     function removeAggregateObject($classname)\r
175     {\r
176         $ok = false;\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
182                 $ok = true;\r
183             }\r
184         }\r
185         return $ok;\r
186     }\r
187 \r
188     // }}}\r
189     // {{{ __call()\r
190 \r
191     /**\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
195      * value.\r
196      *\r
197      * @param string $method  which method that was called\r
198      *\r
199      * @param string $args    An array of the parameters passed in the\r
200      *                        original call\r
201      *\r
202      * @return mixed  The return value from the aggregated method, or a PEAR\r
203      *                error if the called method was unknown.\r
204      */\r
205     function __call($method, $args, &$retval)\r
206     {\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
210         }\r
211         if (isset($this->_method_map[$method])) {\r
212             $retval = call_user_func_array(array($this->_method_map[$method], $method), $args);\r
213             return true;\r
214         }\r
215         return false;\r
216     }\r
217 \r
218     // }}}\r
219 }\r
220 \r
221 overload("PEAR_Autoloader");\r
222 \r
223 ?>\r