-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.php
More file actions
executable file
·199 lines (172 loc) · 7.71 KB
/
App.php
File metadata and controls
executable file
·199 lines (172 loc) · 7.71 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
<?php
/*
* Copyright (c) 2023. Ankio. All Rights Reserved.
*/
/**
* Package: core
* Class App
* Created By ankio.
* Date : 2022/11/9
* Time : 12:40
* Description :
*/
namespace cleanphp;
use cleanphp\base\Config;
use cleanphp\base\Controller;
use cleanphp\base\Error;
use cleanphp\base\EventManager;
use cleanphp\base\Loader;
use cleanphp\base\MainApp;
use cleanphp\base\Response;
use cleanphp\base\Route;
use cleanphp\base\Variables;
use cleanphp\engine\CliEngine;
use cleanphp\engine\EngineManager;
use cleanphp\exception\ExitApp;
use cleanphp\file\Log;
use cleanphp\process\Async;
use Throwable;
class App
{
public static bool $debug = false;//是否调试模式
public static bool $cli = false;//是否命令行模式
public static bool $exit = false;//标记是否退出运行
public static float $db = 0;//db运行时间
public static float $route = 0;//路由时间
public static float $frame = 0;//框架时间
public static float $app_ = 0;//app运行时间
/**
* @var $app ?MainApp
*/
private static ?MainApp $app = null;
/**
* 启动
*/
static function run(): void
{
App::$debug = file_exists(APP_DIR.DIRECTORY_SEPARATOR."debug.lock");
if(App::$debug && function_exists("opcache_reset")){
opcache_reset();
}
error_reporting(E_ALL & ~(E_STRICT | E_NOTICE));
ini_set("display_errors", "Off");
define("DS", DIRECTORY_SEPARATOR);//定义斜杠符号
define("APP_CORE", APP_DIR . DS . 'cleanphp' . DS);//定义程序的核心目录
self::$cli = PHP_SAPI === 'cli';
if (self::$cli) {
$_SERVER["SERVER_NAME"] = "127.0.0.1";
$_SERVER["REQUEST_METHOD"] = "GET";
}//命令行重置
include APP_CORE . "helper.php";//载入内置助手函数
include APP_CORE . "base" . DS . "Variables.php";// 加载变量
include APP_CORE . "base" . DS . "Loader.php";// 加载自动加载器
Variables::init();//初始化变量
Loader::register();// 注册自动加载
Config::register();// 加载配置文件
try {
if (self::$cli) {
EngineManager::setDefaultEngine(new CliEngine());
}
if (self::$debug) {
if (self::$cli)
Log::record("Request", "命令行启动框架", Log::TYPE_WARNING);
else {
Log::record("Request", "收到请求:".$_SERVER["REQUEST_METHOD"] . " " . $_SERVER["REQUEST_URI"]);
}
}
Error::register();// 注册错误和异常处理机制
//Application实例化
$app = "\app\\Application"; //入口初始化
if (class_exists($app) && ($imp = class_implements($app)) && in_array(MainApp::class, $imp)) {
self::$app = new $app();
self::$app->onFrameworkStart();
}
EventManager::trigger("__frame_init__");//框架初始化
Async::register();//异步任务注册
$t = round(((microtime(true) - Variables::get("__frame_start__", 0)) * 1000),4);
self::$frame = $t;
App::$debug && Log::record("Frame", "框架基础环境加载完毕,耗时:".$t."ms", Log::TYPE_WARNING);
//路由
[$__module, $__controller, $__action] = Route::rewrite();
//模块检查
Variables::set("__request_module__", $__module);
Variables::set("__request_controller__", $__controller);
Variables::set("__request_action__", $__action);
//通过路由检测后才认为是请求到达
self::$app && self::$app->onRequestArrive();
EventManager::trigger("__application_init__");//框架初始化
if (!is_dir(Variables::getControllerPath($__module))) {
EngineManager::getEngine()->onNotFound("模块 '$__module' 不存在!");
}
// 控制器检查
if (strtolower($__controller) === 'basecontroller'){
Error::err("基类 'BaseController' 不允许被访问!", [], "Controller");
}
$controller_name = ucfirst($__controller);
$controller_class = 'app\\controller\\' . $__module . '\\' . $controller_name;
if (!class_exists($controller_class, true)) {
$data = [$__module, $__controller, $__action, $controller_class];
EventManager::trigger("__not_render__", $data);
EngineManager::getEngine()->onNotFound("模块 ( $__module ) => 控制器 ( $controller_name ) 不存在!");
}
$method = method_exists($controller_class, $__action);
if (!$method) {
$data = [$__module, $__controller, $__action, $controller_class];
EventManager::trigger("__not_render__", $data);
EngineManager::getEngine()->onNotFound("模块 ( $__module ) => 控制器 ( $controller_name ) 中的方法 ( $__action ) 不存在!");
}
if (!in_array_case($__action, get_class_methods($controller_class)) || $__action === '__init') {
Error::err("模块 ( $__module ) => 控制器 ( $controller_name ) 中的方法 ( $__action ) 为私有方法,禁止访问!", [], "Action");
}
$app_start = microtime(true);
/**
* @var $controller_obj Controller
*/
$controller_obj = new $controller_class();
$result = $controller_obj->$__action();
App::$app_ = round((microtime(true) - $app_start) * 1000,4);
$engine = EngineManager::getEngine();
if ($result !== null){
(new Response())
->render($result)
->setHeaders($engine->getHeaders())
->contentType($engine->getContentType())
->code($engine->getCode())
->send();
} else {
$data = [$__module, $__controller, $__action, $controller_class];
EventManager::trigger("__not_render__", $data);
$engine->onNotFound("No data.",$controller_obj);
}
} catch (ExitApp $exit_app) {//执行退出
App::$debug && Log::record("Frame", sprintf("框架执行退出: %s", $exit_app->getMessage()));
} catch (Throwable $exception) {
Error::err("Exception: ".get_class($exception)."\r\n\r\n".$exception->getMessage(), $exception->getTrace());
} finally {
self::$app && self::$app->onRequestEnd();
if (App::$debug) {
Log::record("Frame", "框架响应结束...");
$t = round((microtime(true) - Variables::get("__frame_start__", 0)) * 1000,4);
Log::record("Frame", sprintf("会话运行时间:%s ms,App运行时间:%s ms", $t,App::$app_), Log::TYPE_WARNING);
$memory = round((memory_get_usage() - $GLOBALS['__memory_start__'])/ 1024 / 1024,2);
Log::record("Frame", sprintf("会话运行占用内存:%s MB", $memory), Log::TYPE_WARNING);
if (App::$app_ > 50) {
Log::record("Frame", sprintf("优化提醒:您的当前应用处理用时(%s毫秒)超过 50 毫秒,建议对代码进行优化以获得更好的使用体验。", $t), Log::TYPE_WARNING);
}
}
}
}
/**
* 退出会话
* @param $msg
* @param bool $output 直接输出
* @return void
*/
static function exit($msg, bool $output = false)
{
if (self::$exit) return; //防止一个会话中重复抛出exit异常
self::$exit = true;
if ($output) echo $msg;
throw new ExitApp($msg);
}
}