-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathLoader.php
More file actions
87 lines (80 loc) · 2.96 KB
/
Copy pathLoader.php
File metadata and controls
87 lines (80 loc) · 2.96 KB
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
78
79
80
81
82
83
84
85
86
87
<?php
class Loader
{
/* 路径映射 */
public static $vendorMap;
/**
* [autoload 自动加载器]
* ------------------------------------------------------------------------------
* @Autor by.fan
* ------------------------------------------------------------------------------
* @DareTime 2017-12-06
* ------------------------------------------------------------------------------
* @param [type] $class [description]
* @return [type] [description]
*/
public static function autoload($class)
{
self::getmap();
$file = self::findFile($class);
if (file_exists($file)) {
self::includeFile($file);
}
}
/**
* [getmap 加载映射]
* ------------------------------------------------------------------------------
* @Autor by.fan
* ------------------------------------------------------------------------------
* @DareTime 2017-12-06
* ------------------------------------------------------------------------------
* @return [type] [description]
*/
private static function getmap()
{
self::$vendorMap = array(
'app' => __DIR__.DIRECTORY_SEPARATOR.'../app',
'core' => __DIR__.DIRECTORY_SEPARATOR.'../core',
);
}
/**
* [findFile 解析文件路径]
* ------------------------------------------------------------------------------
* @Autor by.fan
* ------------------------------------------------------------------------------
* @DareTime 2017-12-06
* ------------------------------------------------------------------------------
* @param [type] $class [description]
* @return [type] [description]
*/
private static function findFile($class)
{
$vendor = substr($class, 0, strpos($class, '\\')); // 顶级命名空间
$vendorDir = self::$vendorMap[$vendor]; // 文件基目录
$filePath = substr($class, strlen($vendor)) . '.php'; // 文件相对路径
return strtr($vendorDir . $filePath, '\\', DIRECTORY_SEPARATOR); // 文件标准路径
}
/**
* [includeFile 引入文件]
* ------------------------------------------------------------------------------
* @Autor by.fan
* ------------------------------------------------------------------------------
* @DareTime 2017-12-06
* ------------------------------------------------------------------------------
* @param [type] $file [description]
* @return [type] [description]
*/
private static function includeFile($file)
{
try {
if (!file_exists($file)) {
throw new Exception('controller ' . $controller . ' is not exists!');
return;
}
include($file);
} catch (Exception $e) {
echo $e; //展示错误结果
return;
}
}
}