mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 15:45:36 +08:00
refactor framework kernel
This commit is contained in:
12
src/ZM/Bootstrap/Bootstrapper.php
Normal file
12
src/ZM/Bootstrap/Bootstrapper.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
use ZM\Kernel;
|
||||
|
||||
interface Bootstrapper
|
||||
{
|
||||
public function bootstrap(Kernel $kernel): void;
|
||||
}
|
||||
@@ -6,10 +6,11 @@ namespace ZM\Bootstrap;
|
||||
|
||||
use OneBot\Exception\ExceptionHandler;
|
||||
use ZM\Exception\Handler;
|
||||
use ZM\Kernel;
|
||||
|
||||
class HandleExceptions
|
||||
class HandleExceptions implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
// 注册全局错误处理器
|
||||
set_error_handler(function ($error_no, $error_msg, $error_file, $error_line) {
|
||||
|
||||
@@ -5,75 +5,24 @@ declare(strict_types=1);
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use OneBot\Driver\Workerman\Worker;
|
||||
use ZM\Config\Environment;
|
||||
use ZM\Config\EnvironmentInterface;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\Kernel;
|
||||
|
||||
class LoadConfiguration
|
||||
class LoadConfiguration implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
// TODO: 重新思考容器绑定的加载方式,从而在此处使用 interface
|
||||
$env = resolve(Environment::class);
|
||||
$this->loadEnvVariables($env);
|
||||
|
||||
$config_i = config();
|
||||
$config_i->addConfigPath($this->getConfigDir($config));
|
||||
$config_i->setEnvironment($this->getConfigEnvironment($config));
|
||||
$this->parseArgvToConfig($config, $config_i);
|
||||
}
|
||||
|
||||
private function getConfigDir(array $config): string
|
||||
{
|
||||
$config_dir = $config['config-dir'] ?? null;
|
||||
// 默认配置文件目录
|
||||
$find_dir = [
|
||||
WORKING_DIR . '/config',
|
||||
SOURCE_ROOT_DIR . '/config',
|
||||
];
|
||||
// 如果启动参数指定了配置文件目录,则优先使用
|
||||
if ($config_dir !== null) {
|
||||
array_unshift($find_dir, $config_dir);
|
||||
}
|
||||
|
||||
// 遍历目录,找到第一个存在的目录
|
||||
foreach ($find_dir as $dir) {
|
||||
if (is_dir($dir)) {
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到目录,则抛出异常
|
||||
throw new \RuntimeException('No config directory found');
|
||||
}
|
||||
|
||||
private function getConfigEnvironment(array $config): string
|
||||
{
|
||||
return $config['env'] ?? 'development';
|
||||
}
|
||||
|
||||
private function parseArgvToConfig(array $argv, ZMConfig $config): void
|
||||
{
|
||||
foreach ($argv as $x => $y) {
|
||||
// 当值为 true/false 时,表示该参数为可选参数。当值为 null 时,表示该参数必定会有一个值,如果是 null,说明没指定
|
||||
if ($y === false || is_null($y)) {
|
||||
continue;
|
||||
}
|
||||
switch ($x) {
|
||||
case 'driver': // 动态设置驱动类型
|
||||
$config->set('global.driver', $y);
|
||||
break;
|
||||
case 'worker-num': // 动态设置 Worker 数量
|
||||
$config->set('global.swoole_options.swoole_set.worker_num', (int) $y);
|
||||
$config->set('global.workerman_options.workerman_worker_num', (int) $y);
|
||||
break;
|
||||
case 'daemon': // 启动为守护进程
|
||||
$config->set('global.swoole_options.swoole_set.daemonize', 1);
|
||||
Worker::$daemonize = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
new ZMConfig([
|
||||
'source' => [
|
||||
'paths' => [$kernel->getConfigDir()],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function loadEnvVariables(EnvironmentInterface $env): void
|
||||
|
||||
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
class LoadGlobalDefines
|
||||
use ZM\Kernel;
|
||||
|
||||
class LoadGlobalDefines implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
require FRAMEWORK_ROOT_DIR . '/src/Globals/global_defines_framework.php';
|
||||
}
|
||||
|
||||
@@ -4,11 +4,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
use ZM\Kernel;
|
||||
use ZM\Plugin\PluginManager;
|
||||
|
||||
class LoadPlugins
|
||||
class LoadPlugins implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
// 先遍历下插件目录下是否有这个插件,没有这个插件则不能打包
|
||||
$plugin_dir = config('global.plugin.load_dir', SOURCE_ROOT_DIR . '/plugins');
|
||||
|
||||
@@ -5,10 +5,11 @@ declare(strict_types=1);
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
use ZM\Event\EventProvider;
|
||||
use ZM\Kernel;
|
||||
|
||||
class RegisterEventProvider
|
||||
class RegisterEventProvider implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
global $ob_event_provider;
|
||||
$ob_event_provider = EventProvider::getInstance();
|
||||
|
||||
@@ -4,18 +4,17 @@ declare(strict_types=1);
|
||||
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
use ZM\Kernel;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
|
||||
class RegisterLogger
|
||||
class RegisterLogger implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
// 初始化 Logger
|
||||
if (!ob_logger_registered()) {
|
||||
$debug = $config['verbose'] ?? false;
|
||||
$debug = $debug ? 'debug' : null;
|
||||
// 如果没有注册过 Logger,那么就初始化一个,在启动框架前注册的话,就不会初始化了,可替换为其他 Logger
|
||||
$logger = new ConsoleLogger($config['log-level'] ?? $debug ?? 'info');
|
||||
$logger = new ConsoleLogger($kernel->getLogLevel());
|
||||
ob_logger_register($logger);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
class SetInternalTimezone
|
||||
use ZM\Kernel;
|
||||
|
||||
class SetInternalTimezone implements Bootstrapper
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
public function bootstrap(Kernel $kernel): void
|
||||
{
|
||||
date_default_timezone_set(config('global.runtime.timezone', 'UTC'));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user