-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathYaf.php
207 lines (176 loc) · 5.19 KB
/
Yaf.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace
{
// PHP_RINIT_FUNCTION
use Yaf\Application;
/**
* @internal
* Class YAF_G_V
*/
class YAF_INI_V
{
public static $ini = [];
}
YAF_INI_V::$ini['yaf.library'] = '';
YAF_INI_V::$ini['yaf.action_prefer'] = '0';
YAF_INI_V::$ini['yaf.lowcase_path'] = '0';
YAF_INI_V::$ini['yaf.use_spl_autoload'] = '0';
YAF_INI_V::$ini['yaf.forward_limit'] = '5';
YAF_INI_V::$ini['yaf.name_suffix'] = '1';
YAF_INI_V::$ini['yaf.name_separator'] = '';
YAF_INI_V::$ini['yaf.st_compatible'] = '0';
YAF_INI_V::$ini['yaf.environ'] = 'product';
YAF_INI_V::$ini['yaf.use_namespace'] = '0';
/**
* @internal
* Class YAF_G_V
*/
class YAF_G_V
{
public static $globals = [];
}
// RINIT_FUNCTION
YAF_G_V::$globals['throw_exception'] = 1;
YAF_G_V::$globals['ext'] = 'php';
YAF_G_V::$globals['view_ext'] = 'phtml';
YAF_G_V::$globals['default_module'] = 'Index';
YAF_G_V::$globals['default_action'] = 'index';
YAF_G_V::$globals['default_controller'] = 'Index';
// ================================================== 内部方法 ==================================================
/**
* YAF内部全局(内部方法,外部不可调用)
*
* @param array $args
* @return null|*
*/
function YAF_G(...$args)
{
$name = $args[0] ?? null;
$value = $args[1] ?? null;
if (count($args) == 1) {
if (array_key_exists($name, YAF_INI_V::$ini)) {
return YAF_INI_V::$ini[$name];
}
return YAF_G_V::$globals[$name] ?? null;
}
if (array_key_exists($name, YAF_INI_V::$ini)) {
YAF_INI_V::$ini[$name] = $value;
} else {
YAF_G_V::$globals[$name] = $value;
}
}
/**
* YAF内部触发错误方法(内部方法,外部不可调用)
*
* @param int $type
* @param string[] $format
* @throws Exception
*/
function yaf_trigger_error(int $type, ...$format)
{
$message = call_user_func_array('sprintf', $format);
if (YAF_G('throw_exception')) {
$base_exception = \YAF\Exception::class;
if (($type & \Yaf\Exception\Internal\YAF_ERR_BASE) === \Yaf\Exception\Internal\YAF_ERR_BASE) {
/** @var \Exception $exception */
$base_exception = \Yaf\Exception\Internal\yaf_buildin_exceptions($type);
}
throw new $base_exception($message, $type);
} else {
$property = new \ReflectionProperty(Application::class, '_app');
$property->setAccessible(true);
/** @var Application $app */
$app = $property->getValue(null);
$errNoProperty = new ReflectionProperty($app, '_err_no');
$errNoProperty->setAccessible(true);
$errNoProperty->setValue($app, $type);
$errMsgProperty = new ReflectionProperty($app, '_err_msg');
$errMsgProperty->setAccessible(true);
$errMsgProperty->setValue($app, $message);
trigger_error($message);
}
}
}
/**
* 只适用于内部的常量(PHP常量)
*/
namespace INTERNAL\PHP
{
const DEFAULT_SLASH = DIRECTORY_SEPARATOR;
const DEFAULT_DIR_SEPARATOR = DIRECTORY_SEPARATOR;
const SUCCESS = 1;
const FAILURE = 0;
}
// MINIT_FUNCTION
namespace YAF
{
const ENVIRON = '';
const VERSION = '3.0.8-dev';
}
namespace YAF\ERR
{
const STARTUP_FAILED = 512;
const ROUTE_FAILED = 513;
const DISPATCH_FAILED = 514;
const CALL_FAILED = 519;
const AUTOLOAD_FAILED = 520;
const TYPE_ERROR = 521;
}
namespace YAF\ERR\NOTFOUND
{
const MODULE = 515;
const CONTROLLER = 516;
const ACTION = 517;
const VIEW = 518;
}
/**
* Yaf-PHP Internal Namespace
*/
namespace YP
{
/**
* @param $object
* @param string $method
* @param array $params
* @return mixed
* @throws \ReflectionException
*/
function internalCall($object, $method, ...$params)
{
$reflectionMethod = new \ReflectionMethod($object, $method);
$reflectionMethod->setAccessible(true);
return $reflectionMethod->invokeArgs($object, $params);
}
/**
* @param $object
* @param string $property
* @return mixed
* @throws \ReflectionException
*/
function internalPropertyGet($object, $property)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object);
}
/**
* 判断是否为绝对路径
*
* @param string $path
* @return bool
*/
function isAbsolutePath($path)
{
return is_string($path) && preg_match('/^\//', $path);
}
function getClassEntry($clazz)
{
$declaredClasses = get_declared_classes();
foreach ($declaredClasses as $class) {
if (!strnatcasecmp($class, $clazz)) {
return $class;
}
}
return null;
}
}