/
autoloader.php
executable file
·356 lines (323 loc) · 8.04 KB
/
autoloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* Part of the Fuel framework.
*
* @package Fuel
* @version 1.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2012 Fuel Development Team
* @link http://fuelphp.com
*/
namespace Fuel\Core;
/**
* The Autloader is responsible for all class loading. It allows you to define
* different load paths based on namespaces. It also lets you set explicit paths
* for classes to be loaded from.
*
* @package Fuel
* @subpackage Core
*/
class Autoloader
{
/**
* @var array $classes holds all the classes and paths
*/
protected static $classes = array();
/**
* @var array holds all the namespace paths
*/
protected static $namespaces = array();
/**
* Holds all the PSR-0 compliant namespaces. These namespaces should
* be loaded according to the PSR-0 standard.
*
* @var array
*/
protected static $psr_namespaces = array();
/**
* @var array list off namespaces of which classes will be aliased to global namespace
*/
protected static $core_namespaces = array(
'Fuel\\Core'
);
/**
* @var array the default path to look in if the class is not in a package
*/
protected static $default_path = null;
/**
* @var bool whether to initialize a loaded class
*/
protected static $auto_initialize = null;
/**
* Adds a namespace search path. Any class in the given namespace will be
* looked for in the given path.
*
* @param string the namespace
* @param string the path
* @return void
*/
public static function add_namespace($namespace, $path, $psr = false)
{
static::$namespaces[$namespace] = $path;
if ($psr)
{
static::$psr_namespaces[$namespace] = $path;
}
}
/**
* Adds an array of namespace paths. See {add_namespace}.
*
* @param array the namespaces
* @param bool whether to prepend the namespace to the search path
* @return void
*/
public static function add_namespaces(array $namespaces, $prepend = false)
{
if ( ! $prepend)
{
static::$namespaces = array_merge(static::$namespaces, $namespaces);
}
else
{
static::$namespaces = $namespaces + static::$namespaces;
}
}
/**
* Returns the namespace's path or false when it doesn't exist.
*
* @param string the namespace to get the path for
* @return array|bool the namespace path or false
*/
public static function namespace_path($namespace)
{
if ( ! array_key_exists($namespace, static::$namespaces))
{
return false;
}
return static::$namespaces[$namespace];
}
/**
* Adds a classes load path. Any class added here will not be searched for
* but explicitly loaded from the path.
*
* @param string the class name
* @param string the path to the class file
* @return void
*/
public static function add_class($class, $path)
{
static::$classes[$class] = $path;
}
/**
* Adds multiple class paths to the load path. See {@see Autoloader::add_class}.
*
* @param array the class names and paths
* @return void
*/
public static function add_classes($classes)
{
foreach ($classes as $class => $path)
{
static::$classes[$class] = $path;
}
}
/**
* Aliases the given class into the given Namespace. By default it will
* add it to the global namespace.
*
* <code>
* Autoloader::alias_to_namespace('Foo\\Bar');
* Autoloader::alias_to_namespace('Foo\\Bar', '\\Baz');
* </code>
*
* @param string $class the class name
* @param string $namespace the namespace to alias to
*/
public static function alias_to_namespace($class, $namespace = '')
{
empty($namespace) or $namespace = rtrim($namespace, '\\').'\\';
$parts = explode('\\', $class);
$root_class = $namespace.array_pop($parts);
class_alias($class, $root_class);
}
/**
* Register's the autoloader to the SPL autoload stack.
*
* @return void
*/
public static function register()
{
spl_autoload_register('Autoloader::load', true, true);
}
/**
* Returns the class with namespace prefix when available
*
* @param string
* @return bool|string
*/
protected static function find_core_class($class)
{
foreach (static::$core_namespaces as $ns)
{
if (array_key_exists($ns_class = $ns.'\\'.$class, static::$classes))
{
return $ns_class;
}
}
return false;
}
/**
* Add a namespace for which classes may be used without the namespace prefix and
* will be auto-aliased to the global namespace.
* Prefixing the classes will overwrite core classes and previously added namespaces.
*
* @param string
* @param bool
* @return void
*/
public static function add_core_namespace($namespace, $prefix = true)
{
if ($prefix)
{
array_unshift(static::$core_namespaces, $namespace);
}
else
{
array_push(static::$core_namespaces, $namespace);
}
}
/**
* Loads a class.
*
* @param string $class Class to load
* @return bool If it loaded the class
*/
public static function load($class)
{
// deal with funny is_callable('static::classname') side-effect
if (strpos($class, 'static::') === 0)
{
// is called from within the class, so it's already loaded
return true;
}
$loaded = false;
$class = ltrim($class, '\\');
$namespaced = ($pos = strripos($class, '\\')) !== false;
if (empty(static::$auto_initialize))
{
static::$auto_initialize = $class;
}
if (array_key_exists($class, static::$classes))
{
include str_replace('/', DS, static::$classes[$class]);
static::init_class($class);
$loaded = true;
}
elseif ($full_class = static::find_core_class($class))
{
if ( ! class_exists($full_class, false) and ! interface_exists($full_class, false))
{
include static::prep_path(static::$classes[$full_class]);
}
class_alias($full_class, $class);
static::init_class($class);
$loaded = true;
}
else
{
$full_ns = substr($class, 0, $pos);
if ($full_ns)
{
foreach (static::$namespaces as $ns => $path)
{
$ns = ltrim($ns, '\\');
if (stripos($full_ns, $ns) === 0)
{
$path .= static::class_to_path(
substr($class, strlen($ns) + 1),
array_key_exists($ns, static::$psr_namespaces)
);
if (is_file($path))
{
require $path;
static::init_class($class);
$loaded = true;
break;
}
}
}
}
if ( ! $loaded)
{
$path = APPPATH.'classes/'.static::class_to_path($class);
if (file_exists($path))
{
include $path;
static::init_class($class);
$loaded = true;
}
}
}
// Prevent failed load from keeping other classes from initializing
if (static::$auto_initialize == $class)
{
static::$auto_initialize = null;
}
return $loaded;
}
/**
* Takes a class name and turns it into a path. It follows the PSR-0
* standard, except for makes the entire path lower case, unless you
* tell it otherwise.
*
* Note: This does not check if the file exists...just gets the path
*
* @param string $class Class name
* @param bool $psr Whether this is a PSR-0 compliant class
* @return string Path for the class
*/
protected static function class_to_path($class, $psr = false)
{
$file = '';
if ($last_ns_pos = strripos($class, '\\'))
{
$namespace = substr($class, 0, $last_ns_pos);
$class = substr($class, $last_ns_pos + 1);
$file = str_replace('\\', DS, $namespace).DS;
}
$file .= str_replace('_', DS, $class).'.php';
if ( ! $psr)
{
$file = strtolower($file);
}
return $file;
}
/**
* Prepares a given path by making sure the directory separators are correct.
*
* @param string $path Path to prepare
* @return string Prepped path
*/
protected static function prep_path($path)
{
return str_replace(array('/', '\\'), DS, $path);
}
/**
* Checks to see if the given class has a static _init() method. If so then
* it calls it.
*
* @param string the class name
*/
protected static function init_class($class)
{
if (static::$auto_initialize === $class)
{
static::$auto_initialize = null;
if (method_exists($class, '_init') and is_callable($class.'::_init'))
{
call_user_func($class.'::_init');
}
}
}
}