2022-12-17 20:38:00 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of webman.
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* For full copyright and license information, please see the MIT-LICENSE.txt
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @author walkor<walkor@workerman.net>
|
|
|
|
* @copyright walkor<walkor@workerman.net>
|
|
|
|
* @link http://www.workerman.net/
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Dotenv\Dotenv;
|
|
|
|
use support\Log;
|
|
|
|
use Webman\Bootstrap;
|
|
|
|
use Webman\Config;
|
|
|
|
use Webman\Middleware;
|
2024-01-31 22:15:08 +08:00
|
|
|
use Webman\Route;
|
2022-12-17 20:38:00 +08:00
|
|
|
use Webman\Util;
|
|
|
|
|
|
|
|
$worker = $worker ?? null;
|
|
|
|
|
|
|
|
set_error_handler(function ($level, $message, $file = '', $line = 0) {
|
|
|
|
if (error_reporting() & $level) {
|
|
|
|
throw new ErrorException($message, 0, $level, $file, $line);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($worker) {
|
2024-01-31 22:15:08 +08:00
|
|
|
register_shutdown_function(function ($startTime) {
|
|
|
|
if (time() - $startTime <= 0.1) {
|
2022-12-17 20:38:00 +08:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}, time());
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
if (class_exists('Dotenv\Dotenv') && file_exists(base_path(false) . '/.env')) {
|
|
|
|
if (method_exists('Dotenv\Dotenv', 'createUnsafeMutable')) {
|
|
|
|
Dotenv::createUnsafeMutable(base_path(false))->load();
|
2022-12-17 20:38:00 +08:00
|
|
|
} else {
|
2024-01-31 22:15:08 +08:00
|
|
|
Dotenv::createMutable(base_path(false))->load();
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Config::clear();
|
|
|
|
support\App::loadAllConfig(['route']);
|
|
|
|
if ($timezone = config('app.default_timezone')) {
|
|
|
|
date_default_timezone_set($timezone);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (config('autoload.files', []) as $file) {
|
|
|
|
include_once $file;
|
|
|
|
}
|
|
|
|
foreach (config('plugin', []) as $firm => $projects) {
|
|
|
|
foreach ($projects as $name => $project) {
|
|
|
|
if (!is_array($project)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($project['autoload']['files'] ?? [] as $file) {
|
|
|
|
include_once $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($projects['autoload']['files'] ?? [] as $file) {
|
|
|
|
include_once $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
Middleware::load(config('middleware', []));
|
2022-12-17 20:38:00 +08:00
|
|
|
foreach (config('plugin', []) as $firm => $projects) {
|
|
|
|
foreach ($projects as $name => $project) {
|
|
|
|
if (!is_array($project) || $name === 'static') {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
Middleware::load($project['middleware'] ?? []);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
Middleware::load($projects['middleware'] ?? [], $firm);
|
2024-01-31 22:15:08 +08:00
|
|
|
if ($staticMiddlewares = config("plugin.$firm.static.middleware")) {
|
|
|
|
Middleware::load(['__static__' => $staticMiddlewares], $firm);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
Middleware::load(['__static__' => config('static.middleware', [])]);
|
2022-12-17 20:38:00 +08:00
|
|
|
|
2024-01-31 22:15:08 +08:00
|
|
|
foreach (config('bootstrap', []) as $className) {
|
|
|
|
if (!class_exists($className)) {
|
|
|
|
$log = "Warning: Class $className setting in config/bootstrap.php not found\r\n";
|
2022-12-17 20:38:00 +08:00
|
|
|
echo $log;
|
|
|
|
Log::error($log);
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
/** @var Bootstrap $className */
|
|
|
|
$className::start($worker);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (config('plugin', []) as $firm => $projects) {
|
|
|
|
foreach ($projects as $name => $project) {
|
|
|
|
if (!is_array($project)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
foreach ($project['bootstrap'] ?? [] as $className) {
|
|
|
|
if (!class_exists($className)) {
|
|
|
|
$log = "Warning: Class $className setting in config/plugin/$firm/$name/bootstrap.php not found\r\n";
|
2022-12-17 20:38:00 +08:00
|
|
|
echo $log;
|
|
|
|
Log::error($log);
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
/** @var Bootstrap $className */
|
|
|
|
$className::start($worker);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
foreach ($projects['bootstrap'] ?? [] as $className) {
|
|
|
|
/** @var string $className */
|
|
|
|
if (!class_exists($className)) {
|
|
|
|
$log = "Warning: Class $className setting in plugin/$firm/config/bootstrap.php not found\r\n";
|
2022-12-17 20:38:00 +08:00
|
|
|
echo $log;
|
|
|
|
Log::error($log);
|
|
|
|
continue;
|
|
|
|
}
|
2024-01-31 22:15:08 +08:00
|
|
|
/** @var Bootstrap $className */
|
|
|
|
$className::start($worker);
|
2022-12-17 20:38:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$directory = base_path() . '/plugin';
|
|
|
|
$paths = [config_path()];
|
|
|
|
foreach (Util::scanDir($directory) as $path) {
|
|
|
|
if (is_dir($path = "$path/config")) {
|
|
|
|
$paths[] = $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Route::load($paths);
|
|
|
|
|