-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboot.php
157 lines (117 loc) · 4.01 KB
/
boot.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
<?php
// 调试开关,正式部署时应该设置为false
defined('DEBUG') or define('DEBUG', true);
// 是否网站模式
defined('SITE_MODE') or define('SITE_MODE', false);
define('ROOT_DIR', __DIR__);
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/src/functions.php';
\Owl\Application::registerNamespace('\\', __DIR__.'/src');
set_error_handler(function ($errno, $error, $file = null, $line = null) {
if (error_reporting() & $errno) {
throw new \ErrorException($error, $errno, $errno, $file, $line);
}
return true;
});
if (!SITE_MODE) {
__bootstrap();
}
function __bootstrap()
{
static $boot = false;
if ($boot) {
return true;
}
$boot = true;
// 加载配置文件
\Owl\Config::merge(require ROOT_DIR.'/config/main.php');
// 初始化外部服务容器
\Owl\Service\Container::getInstance()->setServices(\Owl\Config::get('services'));
}
function __ini_app(\Owl\Application $app)
{
$app->middleware(function ($request, $response) {
$start = microtime(true);
yield;
$use_time = (microtime(true) - $start) * 1000;
$response->withHeader('x-run-time', (int) $use_time);
});
$router = new \Owl\Mvc\Router([
'namespace' => '\Controller',
]);
$app->middleware(function ($request, $response) use ($router) {
$router->execute($request, $response);
});
$app->setExceptionHandler(function ($exception, $request, $response) {
if ($exception instanceof \Owl\Http\Exception) {
$status = $exception->getCode();
} else {
$status = 500;
log_exception(get_logger('default'), $exception);
}
$response->withStatus($status);
if (DEBUG) {
foreach (__exception_headers($exception, 8) as $key => $value) {
$response->withHeader($key, $value);
}
}
if (!$request->isAjax()) {
$view = new \Owl\Mvc\View(ROOT_DIR.'/src/View');
$response->write($view->render('_error', ['exception' => $exception]));
}
});
return $app;
}
function __get_fpm_app()
{
static $app;
if (!$app) {
__bootstrap();
$app = new \Owl\Application();
$app = __ini_app($app);
}
return $app;
}
function __get_swoole_app(array $config)
{
$app = new \Owl\Swoole\Application($config['server']['ip'], $config['server']['port']);
if (isset($config['swoole_setting']) && $config['swoole_setting']) {
$app->getSwooleServer()->set($config['swoole_setting']);
}
$server = $app->getSwooleServer();
$server->on('start', function () use ($config) {
$pid = posix_getpid();
if (isset($config['server']['pid_file'])) {
file_put_contents($config['server']['pid_file'], $pid);
}
echo sprintf("Server PID: %d\n", $pid);
echo sprintf("Listening http://%s:%d/ ...\n", $config['server']['ip'], $config['server']['port']);
});
$server->on('shutdown', function () use ($config) {
if (isset($config['server']['pid_file']) && file_exists($config['server']['pid_file'])) {
unlink($config['server']['pid_file']);
}
});
// 在workstart之后再bootstrap,就可以通过server reload重置应用配置
$server->on('workerstart', function () {
__bootstrap();
});
return __ini_app($app);
}
function __exception_headers($exception, $max_line)
{
if ($previous = $exception->getPrevious()) {
return __exception_headers($previous, $max_line);
}
$headers = [];
$message = $exception->getMessage();
if ($pos = strpos($message, "\n")) {
$message = substr($message, 0, $pos);
}
$headers['X-Exception'] = sprintf('%s(%d) %s', get_class($exception), $exception->getCode(), $message);
foreach (explode("\n", $exception->getTraceAsString()) as $index => $line) {
$key = sprintf('X-Exception-Trace-%02d', $index);
$headers[$key] = $line;
}
return array_splice($headers, 0, $max_line);
}