Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

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