-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathautoloader.php
75 lines (69 loc) · 2.2 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
<?php
/**
* +----------------------------------------------------------------------
* | swoolefy framework bases on swoole extension development, we can use it easily!
* +----------------------------------------------------------------------
* | Licensed ( https://opensource.org/licenses/MIT )
* +----------------------------------------------------------------------
* | @see https://github.com/bingcool/swoolefy
* +----------------------------------------------------------------------
*/
class autoloader
{
/**
* $directory
* @var string
*/
private static $baseDirectory = START_DIR_ROOT;
/**
* Root Dir Map Namespace
* @var array
*/
private static $rootNamespace = ["<{APP_NAME}>"];
/**
* Class Map Namespace
* @var array
*/
private static $classMapNamespace = [];
/**
* @param string $className
* @return void
*/
public static function autoload($className)
{
if (isset(self::$classMapNamespace[$className])) {
return;
}
foreach (self::$rootNamespace as $appDir => $namespace) {
if (0 === strpos($className, $namespace)) {
$parts = explode('\\', $className);
if (!is_numeric($appDir)) {
$parts[0] = $appDir;
}
$filepath = self::$baseDirectory . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $parts) . '.php';
if (is_file($filepath)) {
$res = require_once $filepath;
if ($res) {
self::$classMapNamespace[$className] = true;
}
}
break;
}
}
}
/**
* register autoload
*/
public static function register(bool $prepend = false)
{
if (!function_exists('__autoload')) {
spl_autoload_register(['autoloader', 'autoload'], true, $prepend);
} else {
trigger_error('spl_autoload_register() which will bypass your __autoload() and may break your autoloading', E_USER_WARNING);
}
}
}
// include file
autoloader::register();
// include constants
include APP_PATH.'/Config/constants.php';