From 510bb8dc30acbe6a0f4f57de7cc0532715000b13 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Thu, 9 Mar 2023 22:36:20 +0800 Subject: [PATCH] split runtime info into runtime preferences --- bin/phpunit-zm | 7 +- config/container.php | 2 +- src/ZM/Bootstrap/Bootstrapper.php | 4 +- src/ZM/Bootstrap/HandleExceptions.php | 4 +- src/ZM/Bootstrap/LoadConfiguration.php | 6 +- src/ZM/Bootstrap/LoadGlobalDefines.php | 4 +- src/ZM/Bootstrap/LoadPlugins.php | 4 +- src/ZM/Bootstrap/RegisterEventProvider.php | 4 +- src/ZM/Bootstrap/RegisterLogger.php | 6 +- src/ZM/Bootstrap/SetInternalTimezone.php | 4 +- src/ZM/Command/Command.php | 5 -- src/ZM/Config/RuntimePreferences.php | 83 ++++++++++++++++++++++ src/ZM/Config/ZMConfig.php | 2 +- src/ZM/ConsoleApplication.php | 9 +-- src/ZM/Container/ContainerHolder.php | 2 +- src/ZM/Framework.php | 83 ++++------------------ src/ZM/HasRuntimeInfo.php | 31 -------- 17 files changed, 126 insertions(+), 134 deletions(-) create mode 100644 src/ZM/Config/RuntimePreferences.php delete mode 100644 src/ZM/HasRuntimeInfo.php diff --git a/bin/phpunit-zm b/bin/phpunit-zm index 6346a1b4..0b07a84a 100755 --- a/bin/phpunit-zm +++ b/bin/phpunit-zm @@ -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()); diff --git a/config/container.php b/config/container.php index c858cb47..684f31b0 100644 --- a/config/container.php +++ b/config/container.php @@ -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', ], ]; diff --git a/src/ZM/Bootstrap/Bootstrapper.php b/src/ZM/Bootstrap/Bootstrapper.php index e5c32876..05f2290d 100644 --- a/src/ZM/Bootstrap/Bootstrapper.php +++ b/src/ZM/Bootstrap/Bootstrapper.php @@ -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; } diff --git a/src/ZM/Bootstrap/HandleExceptions.php b/src/ZM/Bootstrap/HandleExceptions.php index f873a7ca..2e4fcec1 100644 --- a/src/ZM/Bootstrap/HandleExceptions.php +++ b/src/ZM/Bootstrap/HandleExceptions.php @@ -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) { diff --git a/src/ZM/Bootstrap/LoadConfiguration.php b/src/ZM/Bootstrap/LoadConfiguration.php index 98983505..c5c3346e 100644 --- a/src/ZM/Bootstrap/LoadConfiguration.php +++ b/src/ZM/Bootstrap/LoadConfiguration.php @@ -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()], ], ]); } diff --git a/src/ZM/Bootstrap/LoadGlobalDefines.php b/src/ZM/Bootstrap/LoadGlobalDefines.php index 4152f43b..e9cf67c5 100644 --- a/src/ZM/Bootstrap/LoadGlobalDefines.php +++ b/src/ZM/Bootstrap/LoadGlobalDefines.php @@ -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'; } diff --git a/src/ZM/Bootstrap/LoadPlugins.php b/src/ZM/Bootstrap/LoadPlugins.php index d469e867..79501be4 100644 --- a/src/ZM/Bootstrap/LoadPlugins.php +++ b/src/ZM/Bootstrap/LoadPlugins.php @@ -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'); diff --git a/src/ZM/Bootstrap/RegisterEventProvider.php b/src/ZM/Bootstrap/RegisterEventProvider.php index 1b50bd7c..8bf53c71 100644 --- a/src/ZM/Bootstrap/RegisterEventProvider.php +++ b/src/ZM/Bootstrap/RegisterEventProvider.php @@ -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(); diff --git a/src/ZM/Bootstrap/RegisterLogger.php b/src/ZM/Bootstrap/RegisterLogger.php index 3451e31c..2bbf799a 100644 --- a/src/ZM/Bootstrap/RegisterLogger.php +++ b/src/ZM/Bootstrap/RegisterLogger.php @@ -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); } } diff --git a/src/ZM/Bootstrap/SetInternalTimezone.php b/src/ZM/Bootstrap/SetInternalTimezone.php index f9bea253..aa3a7f96 100644 --- a/src/ZM/Bootstrap/SetInternalTimezone.php +++ b/src/ZM/Bootstrap/SetInternalTimezone.php @@ -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')); } diff --git a/src/ZM/Command/Command.php b/src/ZM/Command/Command.php index 77f77191..066f2333 100644 --- a/src/ZM/Command/Command.php +++ b/src/ZM/Command/Command.php @@ -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) { diff --git a/src/ZM/Config/RuntimePreferences.php b/src/ZM/Config/RuntimePreferences.php new file mode 100644 index 00000000..458f2377 --- /dev/null +++ b/src/ZM/Config/RuntimePreferences.php @@ -0,0 +1,83 @@ +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'); + } +} diff --git a/src/ZM/Config/ZMConfig.php b/src/ZM/Config/ZMConfig.php index 319e389b..38e44cc5 100644 --- a/src/ZM/Config/ZMConfig.php +++ b/src/ZM/Config/ZMConfig.php @@ -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; } } diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 1d1d804e..f595b541 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -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(); }); diff --git a/src/ZM/Container/ContainerHolder.php b/src/ZM/Container/ContainerHolder.php index 41022b6a..ba4f7527 100644 --- a/src/ZM/Container/ContainerHolder.php +++ b/src/ZM/Container/ContainerHolder.php @@ -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'; } } diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index 7758c31c..8df6bf46 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -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显示等级 diff --git a/src/ZM/HasRuntimeInfo.php b/src/ZM/HasRuntimeInfo.php deleted file mode 100644 index 9d415ba0..00000000 --- a/src/ZM/HasRuntimeInfo.php +++ /dev/null @@ -1,31 +0,0 @@ -