-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoloader.php
66 lines (61 loc) · 2.54 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
<?php
/**
* Description of autoload
*
* @author Administrator
*/
class Autoloader {
public static $classMap = array();
public static $aliases = array(
'@app' => __DIR__ . '/src/app',
'@core' => __DIR__ . '/src/core',
);
public static function getAlias($alias, $throwException = true) {
if (strncmp($alias, '@', 1)) {
// not an alias
return $alias;
}
$pos = strpos($alias, '/');
$root = $pos === false ? $alias : substr($alias, 0, $pos);
if (isset(static::$aliases[$root])) {
if (is_string(static::$aliases[$root])) {
return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
}
foreach (static::$aliases[$root] as $name => $path) {
if (strpos($alias . '/', $name . '/') === 0) {
return $path . substr($alias, strlen($name));
}
}
}
if ($throwException) {
throw new \InvalidArgumentException("Invalid path alias: $alias");
}
return false;
}
public static function autoload($className) {
// 自动加载类
if (isset(static::$classMap[$className])) {
// 如果 $classMap 中存在该类,就直接使用
$classFile = static::$classMap[$className];
// 如果第一个字符串为'@',就意味着对应的文件地址是别名,就将它转化成真实的文件地址
if ($classFile[0] === '@') {
$classFile = static::getAlias($classFile);
}
} elseif (strpos($className, '\\') !== false) {
// 如果存在'\\',就意味着含有 namespace,可以拼成别名,再根据别名获取真实的文件地址
$classFile = static::getAlias('@' . str_replace('\\', '/', $className) . '.php', false);
// 没取到真是文件地址或者获取的地址不是一个文件,就返回空
if ($classFile === false || !is_file($classFile)) {
return;
}
} else {
return;
}
// 引入该类的文件
include($classFile);
// 如果是调试模式,而且 $className 即不是类,不是接口,也不是 trait,就抛出异常
if (!class_exists($className, false) && !interface_exists($className, false) && !trait_exists($className, false)) {
throw new \Exception("Unable to find '$className' in file: $classFile. Namespace missing?");
}
}
}