App.php
5.5 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Lib;
/**
* @author:dc
* @time 2023/2/13 15:07
* Class App
* @package Lib
*/
class App {
/**
* @var App
*/
public static App $instance;
/**
* 当前日期
* @var string
*/
private string $date = '';
/**
* 当前时间
* @var string
*/
private string $dateTime = '';
/**
* @var Route
*/
private Route $route;
/**
* 请求的参数
* @var array
*/
private array $request = [];
/**
* TODO:: 如果debug打开,错误时会返回错误消息到前端
* @var bool
*/
public bool $debug = false;
/**
* 输出到前端的数据
* @var array
*/
private array $data = [];
public function __construct()
{
$this->date = date('Y-m-d');
$this->dateTime = date('Y-m-d H:i:s');
// 路由
$this->route = new Route();
// 请求参数 TODO::不允许其他类型的请求参数
$this->request = my_filter($_POST,['trim']);
// 调试
if(defined('APP_DEBUG')){
$this->debug = boolval(APP_DEBUG);
}
}
/**
* @return App
* @author:dc
* @time 2023/2/13 10:37
*/
public static function instance(){
if(empty(static::$instance)){
static::$instance = new App();
}
return static::$instance;
}
/**
* 开始运行
* @author:dc
* @time 2023/2/13 11:15
*/
public static function run() {
$app = self::instance();
try {
// if ($_SERVER['REQUEST_METHOD'] != 'POST'){
// $app->e('need_post_request');
// }
// 取到路由 控制器
$route = $app->route->get(explode('?',$_SERVER['REQUEST_URI'])[0]);
if(!$route){
$app->e('route_not_found');
}
list($class,$action) = $route;
$controller = new $class();
$data = $controller->{$action}();
if($data){
$app->data = $data;
}
// end 控制器
}catch (\Throwable $exception){
if(!($exception instanceof Err)){
// 记录日志
$filename = LOG_PATH.'/'.$app->nowDate().'.log';
logs(
$exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
$filename
);
// 非 Err 错误类型
$app->data = [
'error_message' => $app->debug ? $exception->getMessage().PHP_EOL.$exception->getTraceAsString() : __('server_error'),
'status' => $exception->getCode() ? $exception->getCode() : 500,
];
}else{
$app->data = [
'error_message' => $exception->getMessage(),
'status' => $exception->getCode() ? $exception->getCode() : 500,
];
}
}
}
/**
* 请求的参数
* @param string $name
* @param null $default
* @param array|string|null $filter
* @return array|false|float|int|mixed|null
* @author:dc
* @time 2023/2/17 9:37
*/
public function request($name='*',$default=null,$filter = null){
$data = [];
if($name === '*'){
$data = $this->request;
} else if (is_string($name)){
$data = $this->request[$name]??$default;
}else if(is_array($name)){
foreach ($this->request as $key=>$value){
if(in_array($key,$name)){
$data[$key] = $value;
}
}
}
if($filter){
$data = my_filter($data,$filter);
}
if($data !== []){
return $data;
}
return null;
}
/**
* @return string
* @author:dc
* @time 2023/2/13 11:17
*/
public function nowDate():string {
return $this->date;
}
/**
* @return string
* @author:dc
* @time 2023/2/13 11:18
*/
public function nowDateTime():string{
return $this->dateTime;
}
/**
* 错误
* @param string $message
* @param int $status
* @throws Err
* @author:dc
* @time 2023/2/13 10:57
*/
public function e($message,$status=400){
if(is_array($message)){
$this->data['error_message'] = __($message[0]);
$this->data['error_message'] = sprintf($this->data['error_message'],...$message[1]);
}else{
$this->data['error_message'] = __($message);
}
$this->data['status'] = $status;
throw new Err($this->data['error_message'],$status);
}
/**
* 成功消息
* @param $data
* @param string $message
* @throws Err
* @author:dc
* @time 2023/2/13 11:03
*/
public function _json($data){
$this->data = $data;
throw new Err('',200);
}
/**
* 结束
* @author:dc
* @time 2023/2/13 10:54
*/
public static function end()
{
// 这里可以做其他事
/**
* 这里写
*/
// end code
header("Content-Type:application/json;Charset=UTF-8;");
// header("Content-Type:text/html;Charset=UTF-8;");
// header("Content-Type:text/event-stream;Charset=UTF-8;");
if(self::instance()->data){
echo json_encode(self::instance()->data,JSON_UNESCAPED_UNICODE);
}
// ob_flush();
// ob_clean();
}
}