forked from Tinywan/tinywan-typephp-webman
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
76 lines (66 loc) · 2.68 KB
/
Copy pathmain.php
File metadata and controls
76 lines (66 loc) · 2.68 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
<?php
use support\App;
use Workerman\Worker;
function main(): void
{
if (!defined('BASE_PATH')) {
define('BASE_PATH', getcwd());
}
ini_set('display_errors', 'on');
error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
// 0. 初始化 Workerman 协议与协程组件
if (class_exists(\Workerman\Protocols\Http\Session::class)) {
\Workerman\Protocols\Http\Session::init();
}
if (class_exists(\Workerman\Protocols\Http\Session\FileSessionHandler::class)) {
\Workerman\Protocols\Http\Session\FileSessionHandler::init();
}
if (class_exists(\Workerman\Coroutine::class)) {
\Workerman\Coroutine::init();
}
if (class_exists(\Workerman\Coroutine\Fiber::class)) {
\Workerman\Coroutine\Fiber::init();
}
if (class_exists(\Workerman\Coroutine\Context::class)) {
\Workerman\Coroutine\Context::initDriver();
}
if (class_exists(\Workerman\Coroutine\Context\Fiber::class)) {
\Workerman\Coroutine\Context\Fiber::initContext();
}
// 1. 加载配置(排除 route 延迟加载,加载 container)
\support\App::loadAllConfig(['route']);
$errorReporting = config('app.error_reporting');
if (isset($errorReporting)) {
error_reporting($errorReporting);
}
// 2. 确保 runtime 目录
$runtimeLogsPath = runtime_path() . DIRECTORY_SEPARATOR . 'logs';
if (!is_dir($runtimeLogsPath)) {
@mkdir($runtimeLogsPath, 0777, true);
}
$runtimeViewsPath = runtime_path() . DIRECTORY_SEPARATOR . 'views';
if (!is_dir($runtimeViewsPath)) {
@mkdir($runtimeViewsPath, 0777, true);
}
// 3. 配置 Workerman 服务属性
$serverConfig = config('server', []);
\Workerman\Worker::$pidFile = $serverConfig['pid_file'] ?? (runtime_path() . '/webman.pid');
\Workerman\Worker::$statusFile = $serverConfig['status_file'] ?? (runtime_path() . '/webman.status');
\Workerman\Worker::$stdoutFile = $serverConfig['stdout_file'] ?? (runtime_path() . '/logs/stdout.log');
\Workerman\Worker::$logFile = $serverConfig['log_file'] ?? (runtime_path() . '/logs/workerman.log');
// 4. 加载中间件与路由表
\Webman\Middleware::load(config('middleware', []));
\Webman\Middleware::load(['__static__' => config('static.middleware', [])]);
\Webman\Route::load([config_path()]);
// 5. 启动 Webman HTTP 进程
$process = config('process', []);
if (isset($process['webman'])) {
worker_start('webman', $process['webman']);
} else {
foreach ($process as $processName => $procConfig) {
worker_start($processName, $procConfig);
}
}
// 5. 进入事件循环监听
\Workerman\Worker::runAll();
}