-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFateBase.php
More file actions
163 lines (138 loc) · 3.92 KB
/
Copy pathFateBase.php
File metadata and controls
163 lines (138 loc) · 3.92 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* @author
* @license MIT
*/
namespace fate;
class FateBase {
/**
* @var object 当前应用
*/
public static $app = null;
/**
* @var array 路径别名
*/
public static $pathAliases = ['@fate' => __DIR__];
/**
* 别名路径转换真实路径
*
* @param string $alias 路径别名
* @return string 路径
*/
public static function getPathAlias($alias) {
if('@' !== $alias[0]) {
return $alias;
}
// 截取开头作为别名
$pos = strpos($alias, '/');
$root = false === $pos ? $alias : substr($alias, 0, $pos);
if(isset(static::$pathAliases[$root])) {
return false === $pos ?
static::$pathAliases[$root] :
static::$pathAliases[$root] . substr($alias, $pos);
}
return '';
}
/**
* 设置路径别名
*
* @param string $alias 路径别名
* @param string $path 路径
*/
public static function setPathAlias($alias, $path) {
if('@' !== $alias[0]) {
$alias = '@' . $alias;
}
if(null === $path) {
unset(static::$pathAliases[$alias]);
} else {
static::$pathAliases[$alias] = rtrim($path, '/');
}
}
/**
* 对象配置
*
* @param Object $object 需要配置的对象
* @param array $properties 配置项
* @return Object 源对象
*/
public static function configure($object, $properties) {
foreach($properties as $key => $val) {
$object->$key = $val;
}
return $object;
}
/**
* 创建对象
*
* @param string | array $clazz 类全名 或 类配置
*
* 'some\namespace\Class'
* or
* [
* 'classPath' => 'some\namespace\Class'
* '...' => ...
* ]
*
* @param array $parameters 参数
* @return Object 类实例
* @throws \ReflectionException
*/
public static function createObject($clazz, array $parameters = []) {
if(is_string($clazz)) {
return static::createObjectAsString($clazz, $parameters);
}
return static::createObjectAsDefinition($clazz, $parameters);
}
/**
* 字符串方式创建对象
*
* @param string classPath
* @throws \ReflectionException
*/
public static function createObjectAsString($classPath, $parameters) {
$reflection = new \ReflectionClass($classPath);
return $reflection->newInstanceArgs($parameters);
}
/**
* 配置方式创建对象
*
* @param array definition
* @throws \ReflectionException
*/
public static function createObjectAsDefinition($definition, $parameters) {
$classPath = $definition['classPath'];
$properties = $definition;
unset($properties['classPath']);
$reflection = new \ReflectionClass($classPath);
$instance = $reflection->newInstanceArgs($parameters);
static::configure($instance, $properties);
return $instance;
}
/**
* namespace 路径转换真实路径
*
* @param string $namespace 命名空间
* @param string $extension 扩展
* @return string 文件路径
*/
public static function namespaceToNormal($namespace, $extension = '.php') {
$path = static::getPathAlias('@' . str_replace('\\', '/', $namespace));
return $path . $extension;
}
/**
* 类自动加载器
*
* @param string $className 要载入的类全名 eg. fate\web\Application
*/
public static function autoload($className) {
// 导入有命名空间的类
if(false !== strpos($className, '\\')) {
$classFile = static::namespaceToNormal($className);
if('' === $classFile || !is_file($classFile)) {
return;
}
include($classFile);
}
}
}