merge framework and kernel

This commit is contained in:
sunxyw
2023-03-07 23:07:52 +08:00
parent 8f43012a5c
commit 772288b517
19 changed files with 153 additions and 176 deletions

View File

@@ -20,6 +20,7 @@ use OneBot\Driver\Swoole\SwooleDriver;
use OneBot\Driver\Workerman\Worker;
use OneBot\Driver\Workerman\WorkermanDriver;
use OneBot\Util\Singleton;
use ZM\Bootstrap\Bootstrapper;
use ZM\Command\Server\ServerStartCommand;
use ZM\Container\ContainerBindingListener;
use ZM\Event\Listener\HttpEventListener;
@@ -38,7 +39,7 @@ use ZM\Utils\EasterEgg;
* 框架入口类
* @since 3.0
*/
class Framework
class Framework implements HasRuntimeInfo
{
use Singleton;
@@ -67,31 +68,40 @@ class Framework
Bootstrap\SetInternalTimezone::class, // 设置时区
];
protected string $environment = 'development';
protected bool $debug_mode = false;
protected string $log_level = 'info';
protected string $config_dir = SOURCE_ROOT_DIR . '/config';
/**
* 框架初始化文件
*
* @param array<string, null|bool|string> $argv 传入的参数(见 ServerStartCommand
* @throws \Exception
*/
public function __construct(array $argv = [])
public function __construct()
{
// 单例化整个Framework类
if (self::$instance !== null) {
throw new SingletonViolationException(self::class);
}
self::$instance = $this;
// 初始化必需的args参数如果没有传入的话使用默认值
$this->argv = empty($argv) ? ServerStartCommand::exportOptionArray() : $argv;
}
/**
* 初始化框架
*
* @param array<string, null|bool|string> $argv 传入的参数(见 ServerStartCommand
*
* @throws \Exception
*/
public function init(): Framework
public function init(array $argv = []): Framework
{
// TODO: discard argv
// 初始化必需的args参数如果没有传入的话使用默认值
$this->argv = empty($argv) ? ServerStartCommand::exportOptionArray() : $argv;
// 初始化 @OnSetup 事件
$this->initSetupAnnotations();
@@ -250,6 +260,73 @@ class Framework
}
}
public function bootstrap(): void
{
foreach ($this->bootstrappers as $bootstrapper) {
/* @var Bootstrapper $bootstrapper */
(new $bootstrapper())->bootstrap($this);
}
}
public function environment(...$environments): string|bool
{
if (empty($environments)) {
return $this->environment;
}
return in_array($this->environment, $environments, true);
}
public function setEnvironment(string $environment): void
{
$this->environment = $environment;
}
public function isDebugMode(): bool
{
return $this->debug_mode;
}
public function setDebugMode(bool $debug_mode): void
{
$this->debug_mode = $debug_mode;
}
public function getLogLevel(): string
{
return $this->isDebugMode() ? 'debug' : $this->log_level;
}
public function setLogLevel(string $log_level): void
{
$this->log_level = $log_level;
}
public function getConfigDir(): string
{
return $this->config_dir;
}
public function setConfigDir(string $config_dir): void
{
$this->config_dir = $config_dir;
}
public function runningInInteractiveTerminal(): bool
{
if (function_exists('posix_isatty')) {
return posix_isatty(STDIN) && posix_isatty(STDOUT);
}
// fallback to stream_isatty() if posix_isatty() is not available (e.g. on Windows)
return function_exists('stream_isatty') && stream_isatty(STDIN) && stream_isatty(STDOUT);
}
public function runningUnitTests(): bool
{
return defined('PHPUNIT_RUNNING') && constant('PHPUNIT_RUNNING');
}
/**
* 打印属性表格
*/
@@ -259,7 +336,7 @@ class Framework
// 打印工作目录
$properties['working_dir'] = WORKING_DIR;
// 打印环境信息
$properties['environment'] = Kernel::getInstance()->environment();
$properties['environment'] = $this->environment();
// 打印驱动
$properties['driver'] = config('global.driver');
// 打印logger显示等级