-
Notifications
You must be signed in to change notification settings - Fork 77
/
autoloader.php
61 lines (57 loc) · 2.19 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
<?php
/**
+----------------------------------------------------------------------
| swoolefy framework bases on swoole extension development, we can use it easily!
+----------------------------------------------------------------------
| Licensed ( https://opensource.org/licenses/MIT )
+----------------------------------------------------------------------
| Author: bingcool <[email protected] || [email protected]>
+----------------------------------------------------------------------
*/
/*************自定义命名空间加载类**************/
class autoloader {
/**
* $directory 当前的目录
* @var [type]
*/
private static $baseDirectory = __DIR__;
/**
* $prefix 自定义的根命名空间
* @var array
*/
private static $root_namespace = ['App','Service','protocol'];
/**
* @param string $className
* @return boolean
*/
public static function autoload($className) {
foreach(self::$root_namespace as $k=>$namespace) {
// 判断如果以\命名空间访问的格式符合
if (0 === strpos($className, $namespace)) {
//分隔出$this->prefixLength个字符串以后的字符返回,再以\为分隔符分隔
$parts = explode('\\', $className);
// 组合新的路径
$filepath = self::$baseDirectory.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts).'.php';
if (is_file($filepath)) {
require_once $filepath;
}
// 匹配到符合的,结束循环
break;
}
}
}
/**
* 注册自动加载
*/
public static function register($prepend=false) {
if(!function_exists('__autoload')) {
spl_autoload_register(array('autoloader', 'autoload'), true, $prepend);
}else {
trigger_error('spl_autoload_register() which will bypass your __autoload() and may break your autoloading', E_USER_WARNING);
}
}
}
// include文件时,即完成自动加载的注册
autoloader::register();
// include 加载框架整体定义常量
include_once SCORE_DIR_ROOT.'/score/MPHP.php';