1
|
|
|
<?php |
2
|
|
|
namespace nebula\loader; |
3
|
|
|
|
4
|
|
|
use nebula\loader\IncludeManager; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 类自动加载器 |
9
|
|
|
*/ |
10
|
|
|
class Loader extends IncludeManager |
11
|
|
|
{ |
12
|
|
|
protected static $loader; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 注册加载器 |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public static function register() |
20
|
|
|
{ |
21
|
|
|
// 注册加载器 |
22
|
|
|
spl_autoload_register(array(__CLASS__, 'classLoader')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 获取加载器 |
27
|
|
|
* |
28
|
|
|
* @return self |
29
|
|
|
*/ |
30
|
|
|
public static function getLoader() |
31
|
|
|
{ |
32
|
|
|
if (isset(static::$loader)) { |
33
|
|
|
return static::$loader; |
34
|
|
|
} |
35
|
|
|
return static::$loader = new static; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 自动类加载器 |
40
|
|
|
* |
41
|
|
|
* @param string $className |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public static function classLoader(string $className) |
45
|
|
|
{ |
46
|
|
|
if ($path = static::$loader->getClassPath($className)) { |
47
|
|
|
if (!class_exists($className, false)) { |
48
|
|
|
@require_once $path; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 获取类路径 |
55
|
|
|
* |
56
|
|
|
* @param string $className |
57
|
|
|
* @return string|null |
58
|
|
|
*/ |
59
|
|
|
public function getClassPath(string $className):?string |
60
|
|
|
{ |
61
|
|
|
// 搜索路径 |
62
|
|
|
foreach ($this->includePath as $includeNamespace => $includePaths) { |
63
|
|
|
foreach ($includePaths as $includePath) { |
64
|
|
|
if ($path = $this->getClassPathByName($includeNamespace, $includePath, $className)) { |
65
|
|
|
return $path; |
66
|
|
|
} elseif ($path = $this->getClassPathByAlias($includePath, $className)) { |
67
|
|
|
return $path; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* 根据类别名获取路径 |
76
|
|
|
* |
77
|
|
|
* @param string $includePath |
78
|
|
|
* @param string $className |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
|
|
protected function getClassPathByAlias(string $includePath, string $className):?string |
82
|
|
|
{ |
83
|
|
|
$namepath=static::formatSeparator($className); |
84
|
|
|
$className=static::realName($className); |
85
|
|
|
foreach ($this->namespace as $namespace) { |
86
|
|
|
$path = $includePath.DIRECTORY_SEPARATOR.$namespace.DIRECTORY_SEPARATOR.$namepath.'.php'; |
87
|
|
|
if ($path = static::realPath($path)) { |
88
|
|
|
// 精简类名 |
89
|
|
|
if (!class_exists($className, false)) { |
90
|
|
|
class_alias($namespace.'\\'.$className, $className); |
91
|
|
|
} |
92
|
|
|
return $path; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 根据类名获取路径 |
100
|
|
|
* |
101
|
|
|
* @param string $includeNamespace |
102
|
|
|
* @param string $includePath |
103
|
|
|
* @param string $className |
104
|
|
|
* @return string|null |
105
|
|
|
*/ |
106
|
|
|
protected function getClassPathByName(string $includeNamespace, string $includePath, string $className):?string |
107
|
|
|
{ |
108
|
|
|
if (is_numeric($includeNamespace)) { |
109
|
|
|
$path = $includePath.DIRECTORY_SEPARATOR.static::formatSeparator($className).'.php'; |
110
|
|
|
} else { |
111
|
|
|
$nl = strlen($includeNamespace); |
112
|
|
|
if (substr(static::realName($className), 0, $nl) === $includeNamespace) { |
113
|
|
|
$path=$includePath.DIRECTORY_SEPARATOR.static::formatSeparator(substr($className, $nl)).'.php'; |
114
|
|
|
} else { |
115
|
|
|
$path=$includePath.DIRECTORY_SEPARATOR.static::formatSeparator($className).'.php'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
if ($path = static::realPath($path)) { |
119
|
|
|
return $path; |
120
|
|
|
} |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|