split runtime info into runtime preferences

This commit is contained in:
sunxyw 2023-03-09 22:36:20 +08:00
parent 772288b517
commit 510bb8dc30
No known key found for this signature in database
GPG Key ID: F391C42B19AFFC98
17 changed files with 126 additions and 134 deletions

View File

@ -58,9 +58,10 @@ $options['worker-num'] = 1;
$options['private-mode'] = true;
try {
$framework = (new Framework($options));
$framework->setEnvironment('development');
$framework->setConfigDir(dirname(__DIR__) . '/config');
$framework = new Framework();
$framework->runtime_preferences = $framework->runtime_preferences
->withConfigDir(dirname(__DIR__) . '/config')
->withEnvironment('development');
$framework->bootstrap();
$framework->init()->start();
exit($_swoole_atomic->get());

View File

@ -39,7 +39,7 @@ return [
// 详细介绍请参阅https://php-di.org/doc/performances.html#caching
'cache' => [
// 是否启用缓存,支持 bool、callable
'enable' => fn () => Framework::getInstance()->environment('production'),
'enable' => fn () => Framework::getInstance()->runtime_preferences->environment('production'),
'namespace' => 'zm',
],
];

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use ZM\HasRuntimeInfo;
use ZM\Config\RuntimePreferences;
interface Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void;
public function bootstrap(RuntimePreferences $preferences): void;
}

View File

@ -5,12 +5,12 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use OneBot\Exception\ExceptionHandler;
use ZM\Config\RuntimePreferences;
use ZM\Exception\Handler;
use ZM\HasRuntimeInfo;
class HandleExceptions implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
// 注册全局错误处理器
set_error_handler(function ($error_no, $error_msg, $error_file, $error_line) {

View File

@ -7,12 +7,12 @@ namespace ZM\Bootstrap;
use Dotenv\Dotenv;
use ZM\Config\Environment;
use ZM\Config\EnvironmentInterface;
use ZM\Config\RuntimePreferences;
use ZM\Config\ZMConfig;
use ZM\HasRuntimeInfo;
class LoadConfiguration implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
// TODO: 重新思考容器绑定的加载方式,从而在此处使用 interface
$env = resolve(Environment::class);
@ -20,7 +20,7 @@ class LoadConfiguration implements Bootstrapper
new ZMConfig([
'source' => [
'paths' => [$runtime_info->getConfigDir()],
'paths' => [$preferences->getConfigDir()],
],
]);
}

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use ZM\HasRuntimeInfo;
use ZM\Config\RuntimePreferences;
class LoadGlobalDefines implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
require FRAMEWORK_ROOT_DIR . '/src/Globals/global_defines_framework.php';
}

View File

@ -4,12 +4,12 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use ZM\HasRuntimeInfo;
use ZM\Config\RuntimePreferences;
use ZM\Plugin\PluginManager;
class LoadPlugins implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
// 先遍历下插件目录下是否有这个插件,没有这个插件则不能打包
$plugin_dir = config('global.plugin.load_dir', SOURCE_ROOT_DIR . '/plugins');

View File

@ -4,12 +4,12 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use ZM\Config\RuntimePreferences;
use ZM\Event\EventProvider;
use ZM\HasRuntimeInfo;
class RegisterEventProvider implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
global $ob_event_provider;
$ob_event_provider = EventProvider::getInstance();

View File

@ -4,17 +4,17 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use ZM\HasRuntimeInfo;
use ZM\Config\RuntimePreferences;
use ZM\Logger\ConsoleLogger;
class RegisterLogger implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
// 初始化 Logger
if (!ob_logger_registered()) {
// 如果没有注册过 Logger那么就初始化一个在启动框架前注册的话就不会初始化了可替换为其他 Logger
$logger = new ConsoleLogger($runtime_info->getLogLevel());
$logger = new ConsoleLogger($preferences->getLogLevel());
ob_logger_register($logger);
}
}

View File

@ -4,11 +4,11 @@ declare(strict_types=1);
namespace ZM\Bootstrap;
use ZM\HasRuntimeInfo;
use ZM\Config\RuntimePreferences;
class SetInternalTimezone implements Bootstrapper
{
public function bootstrap(HasRuntimeInfo $runtime_info): void
public function bootstrap(RuntimePreferences $preferences): void
{
date_default_timezone_set(config('global.runtime.timezone', 'UTC'));
}

View File

@ -33,11 +33,6 @@ abstract class Command extends \Symfony\Component\Console\Command\Command implem
$this->input = $input;
$this->output = $output;
if ($this->shouldExecute()) {
if (property_exists($this, 'bootstrappers')) {
foreach ($this->bootstrappers as $bootstrapper) {
(new $bootstrapper())->bootstrap($this->input->getOptions());
}
}
try {
return $this->handle();
} catch (\Throwable $e) {

View File

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace ZM\Config;
class RuntimePreferences
{
protected string $environment = 'development';
protected bool $debug_mode = false;
protected string $log_level = 'info';
protected string $config_dir = SOURCE_ROOT_DIR . '/config';
public function environment(...$environments): string|bool
{
if (empty($environments)) {
return $this->environment;
}
return in_array($this->environment, $environments, true);
}
public function withEnvironment(string $environment): self
{
$copy = clone $this;
$copy->environment = $environment;
return $copy;
}
public function isDebugMode(): bool
{
return $this->debug_mode;
}
public function enableDebugMode(bool $debug_mode): self
{
$copy = clone $this;
$copy->debug_mode = $debug_mode;
return $copy;
}
public function getLogLevel(): string
{
return $this->isDebugMode() ? 'debug' : $this->log_level;
}
public function withLogLevel(string $log_level): self
{
$copy = clone $this;
$copy->log_level = $log_level;
return $copy;
}
public function getConfigDir(): string
{
return $this->config_dir;
}
public function withConfigDir(string $config_dir): self
{
$copy = clone $this;
$copy->config_dir = $config_dir;
return $copy;
}
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');
}
}

View File

@ -302,7 +302,7 @@ class ZMConfig
}
if ($type === 'environment') {
$name_and_env = explode('.', $name);
if (Framework::getInstance()->environment($name_and_env[1])) {
if (Framework::getInstance()->runtime_preferences->environment($name_and_env[1])) {
return true;
}
}

View File

@ -54,10 +54,11 @@ final class ConsoleApplication extends Application
// 初始化内核
/** @var Framework $kernel */
$kernel = Framework::getInstance();
$kernel->setConfigDir($input->getOption('config-dir'));
$kernel->setEnvironment($input->getOption('env'));
$kernel->setDebugMode($input->getOption('debug'));
$kernel->setLogLevel($input->getOption('log-level'));
$kernel->runtime_preferences = $kernel->runtime_preferences
->withConfigDir($input->getOption('config-dir'))
->withEnvironment($input->getOption('env'))
->enableDebugMode($input->getOption('debug'))
->withLogLevel($input->getOption('log-level'));
$kernel->bootstrap();
});

View File

@ -66,6 +66,6 @@ class ContainerHolder
if (self::$config) {
return;
}
self::$config = require Framework::getInstance()->getConfigDir() . '/container.php';
self::$config = require Framework::getInstance()->runtime_preferences->getConfigDir() . '/container.php';
}
}

View File

@ -22,6 +22,7 @@ use OneBot\Driver\Workerman\WorkermanDriver;
use OneBot\Util\Singleton;
use ZM\Bootstrap\Bootstrapper;
use ZM\Command\Server\ServerStartCommand;
use ZM\Config\RuntimePreferences;
use ZM\Container\ContainerBindingListener;
use ZM\Event\Listener\HttpEventListener;
use ZM\Event\Listener\ManagerEventListener;
@ -38,8 +39,10 @@ use ZM\Utils\EasterEgg;
/**
* 框架入口类
* @since 3.0
*
* @method static Framework getInstance()
*/
class Framework implements HasRuntimeInfo
class Framework
{
use Singleton;
@ -49,6 +52,11 @@ class Framework implements HasRuntimeInfo
/** @var string 版本名称 */
public const VERSION = '3.1.1';
/**
* @var RuntimePreferences 运行时偏好(环境信息&参数)
*/
public RuntimePreferences $runtime_preferences;
/** @var array 传入的参数 */
protected array $argv;
@ -68,14 +76,6 @@ class Framework implements HasRuntimeInfo
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';
/**
* 框架初始化文件
* @throws \Exception
@ -87,6 +87,8 @@ class Framework implements HasRuntimeInfo
throw new SingletonViolationException(self::class);
}
self::$instance = $this;
$this->runtime_preferences = new RuntimePreferences();
}
/**
@ -264,69 +266,10 @@ class Framework implements HasRuntimeInfo
{
foreach ($this->bootstrappers as $bootstrapper) {
/* @var Bootstrapper $bootstrapper */
(new $bootstrapper())->bootstrap($this);
(new $bootstrapper())->bootstrap($this->runtime_preferences);
}
}
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');
}
/**
* 打印属性表格
*/
@ -336,7 +279,7 @@ class Framework implements HasRuntimeInfo
// 打印工作目录
$properties['working_dir'] = WORKING_DIR;
// 打印环境信息
$properties['environment'] = $this->environment();
$properties['environment'] = $this->runtime_preferences->environment();
// 打印驱动
$properties['driver'] = config('global.driver');
// 打印logger显示等级

View File

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace ZM;
interface HasRuntimeInfo
{
/**
* 是否正在可交互終端中运行
*/
public function runningInInteractiveTerminal(): bool;
/**
* 是否正执行单元测试
*/
public function runningUnitTests(): bool;
/**
* 获取或检查运行环境
*
* @param array|string ...$environments
*/
public function environment(...$environments): string|bool;
public function isDebugMode(): bool;
public function getLogLevel(): string;
public function getConfigDir(): string;
}