-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoloader.php
77 lines (68 loc) · 2.03 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
<?php
namespace Cherry;
/**
* @brief PSR-0 compatible autoloader
*
*
*
* @code
* $al = new \Cherry\Autoloader('Cherry','system/cherry/','Cherry');
* $al->register();
* @endcode
*
* This loader supports both relative and absolute paths:
*
* - ./system/cherry/ is expected to live in the application root.
* - system/cherry/ points to same as above
* - /opt/cherryphp/system/cherry/ would indicate absolute path
*/
class Autoloader {
private
$paths = [];
/**
*
* @param string $namespace
* @param string $path
* @param string $root
*/
public function __construct($namespace, $path, $root=null) {
if (substr($path,-1) != _DS_) $path.= _DS_;
$this->paths[] = [
$namespace,
$path,
$root
];
}
public function addPaths(array $path, array $path2 = null) {
$args = func_get_args();
foreach ($args as $arg) {
if (is_string($arg)) {
$this->paths[] = $arg;
} elseif (count($arg) == 1) {
$this->paths[] = [ null, $arg[0], null];
} elseif (count($arg) == 2) {
$this->paths[] = [ $arg[0], $arg[1], null];
} elseif (count($arg) == 3) {
$this->paths[] = [ $arg[0], $arg[1], $arg[2]];
} else {
$argstr = join(',',(array)$arg);
user_error("Bad request to addPaths (in argument '{$argstr}'");
}
}
}
public function register($throw=false) {
spl_autoload_register([$this, '_autoload'],$throw);
}
public function unregister() {
spl_autoload_unregister([$this, '_autoload']);
}
public static function _autoload($class) {
// Replace underscores in Pear_Style_ClassNames if needed
if (strpos($class,_NS_)===false) {
$lpath = str_replace('_',_DS_,$class);
} else {
$lpath = str_replace('_',_NS_,$class);
}
$lpath = join(_DS_,[ APP_ROOT , 'vendor' , $lpath ]);
}
}