From 4ff75cf19978792208c15d22fba651a40b983535 Mon Sep 17 00:00:00 2001 From: jerry Date: Tue, 2 Mar 2021 21:24:31 +0800 Subject: [PATCH] update to 2.2.8 version update motd message --- bin/start | 12 +-- composer.json | 2 +- config/motd.txt | 10 +- docs/FAQ.md | 2 + docs/update/v2.md | 7 +- src/ZM/Command/RunServerCommand.php | 1 - src/ZM/ConsoleApplication.php | 18 ++-- src/ZM/Framework.php | 156 +++++++++++++++++++++++----- src/ZM/global_defines.php | 2 +- 9 files changed, 156 insertions(+), 54 deletions(-) diff --git a/bin/start b/bin/start index ebb92c7f..f52dd8b5 100755 --- a/bin/start +++ b/bin/start @@ -1,14 +1,6 @@ #!/usr/bin/env php -initEnv()->run(); diff --git a/composer.json b/composer.json index b4f00878..695f98e8 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "High performance QQ robot and web server development framework", "minimum-stability": "stable", "license": "Apache-2.0", - "version": "2.2.7", + "version": "2.2.8", "extra": { "exclude_annotate": [ "src/ZM" diff --git a/config/motd.txt b/config/motd.txt index 45145957..d06ef323 100644 --- a/config/motd.txt +++ b/config/motd.txt @@ -1,6 +1,6 @@ - ______ -|__ / |__ __ _ _ __ ___ __ _ ___ - / /| '_ \ / _` | '_ ` _ \ / _` |/ _ \ - / /_| | | | (_| | | | | | | (_| | (_) | -/____|_| |_|\__,_|_| |_| |_|\__,_|\___/ + ______ + |__ / |__ __ _ _ __ ___ __ _ ___ + / /| '_ \ / _` | '_ ` _ \ / _` |/ _ \ + / /_| | | | (_| | | | | | | (_| | (_) | + /____|_| |_|\__,_|_| |_| |_|\__,_|\___/ diff --git a/docs/FAQ.md b/docs/FAQ.md index 4514b4c1..c974f108 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -1 +1,3 @@ # FAQ + +这里会写一些常见的疑难解答。 \ No newline at end of file diff --git a/docs/update/v2.md b/docs/update/v2.md index 61f51871..71917b8c 100644 --- a/docs/update/v2.md +++ b/docs/update/v2.md @@ -1,5 +1,11 @@ # 更新日志(v2 版本) +## v2.2.8 + +> 更新时间:2021.3.2 + +- 更新:MOTD 显示的方式,更加直观和炫酷 + ## v2.2.7 > 更新时间:2021.2.27 @@ -7,7 +13,6 @@ - 修复:2.2.6 版本下 `reply()` 方法在群里调用会 at 成员的 bug - 修复:空 `access_token` 的情况下会无法连入的 bug - 修复:使用 Closure 闭包函数自行编写逻辑的判断返回 false 无法阻断连接的 bug -- ## v2.2.6 diff --git a/src/ZM/Command/RunServerCommand.php b/src/ZM/Command/RunServerCommand.php index 926c96a6..e4b6812f 100644 --- a/src/ZM/Command/RunServerCommand.php +++ b/src/ZM/Command/RunServerCommand.php @@ -39,7 +39,6 @@ class RunServerCommand extends Command return Command::FAILURE; } } - if (LOAD_MODE == 0) echo "* This is repository mode.\n"; (new Framework($input->getOptions()))->start(); return Command::SUCCESS; } diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index ccf746fe..fa843223 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -26,23 +26,21 @@ class ConsoleApplication extends Application public function initEnv() { $this->selfCheck(); + if (!is_dir(__DIR__ . '/../../vendor')) { + define("LOAD_MODE", 1); // composer项目模式 + define("LOAD_MODE_COMPOSER_PATH", getcwd()); + } else { + define("LOAD_MODE", 0); // 源码模式 + } + //if (LOAD_MODE === 0) $this->add(new BuildCommand()); //只有在git源码模式才能使用打包指令 if (LOAD_MODE === 0) define("WORKING_DIR", getcwd()); elseif (LOAD_MODE == 1) define("WORKING_DIR", realpath(__DIR__ . "/../../")); - elseif (LOAD_MODE == 2) echo "Phar mode: " . WORKING_DIR . PHP_EOL; if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) { /** @noinspection PhpIncludeInspection */ require_once DataProvider::getWorkingDir() . "/vendor/autoload.php"; } - if (LOAD_MODE == 2) { - // Phar 模式,2.0 不提供哦 - //require_once FRAMEWORK_DIR . "/vendor/autoload.php"; - spl_autoload_register('phar_classloader'); - } elseif (LOAD_MODE == 0) { - /** @noinspection PhpIncludeInspection - * @noinspection RedundantSuppression - */ - require_once WORKING_DIR . "/vendor/autoload.php"; + if (LOAD_MODE == 0) { $composer = json_decode(file_get_contents(DataProvider::getWorkingDir() . "/composer.json"), true); if (!isset($composer["autoload"]["psr-4"]["Module\\"])) { echo "框架源码模式需要在autoload文件中添加Module目录为自动加载,是否添加?[Y/n] "; diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index b3f846f0..a589b35c 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -9,6 +9,7 @@ use Exception; use ZM\Annotation\Swoole\OnSetup; use ZM\Config\ZMConfig; use ZM\ConnectionManager\ManagerGM; +use ZM\Console\TermColor; use ZM\Event\ServerEventHandler; use ZM\Store\LightCache; use ZM\Store\LightCacheInside; @@ -87,35 +88,37 @@ class Framework $this->parseCliArgs(self::$argv); - $out = [ - "host" => ZMConfig::get("global", "host"), - "port" => ZMConfig::get("global", "port"), - "log_level" => Console::getLevel(), - "version" => ZM_VERSION, - "config" => $args["env"] === null ? 'global.php' : $args["env"] - ]; - if (APP_VERSION !== "unknown") $out["app_version"] = APP_VERSION; - if (isset(ZMConfig::get("global", "swoole")["task_worker_num"])) { - $out["task_worker_num"] = ZMConfig::get("global", "swoole")["task_worker_num"]; - } - if (($num = ZMConfig::get("global", "swoole")["worker_num"] ?? swoole_cpu_num()) != 1) { - $out["worker_num"] = $num; - } - $out["working_dir"] = DataProvider::getWorkingDir(); - Console::printProps($out, $tty_width); self::$server->set($this->server_set); - if (file_exists(DataProvider::getWorkingDir() . "/config/motd.txt")) { - $motd = file_get_contents(DataProvider::getWorkingDir() . "/config/motd.txt"); - } else { - $motd = file_get_contents(__DIR__ . "/../../config/motd.txt"); + + // 打印初始信息 + $out["listen"] = ZMConfig::get("global", "host") . ":" . ZMConfig::get("global", "port"); + if (!isset(ZMConfig::get("global", "swoole")["worker_num"])) $out["worker"] = swoole_cpu_num() . " (auto)"; + else $out["worker"] = ZMConfig::get("global", "swoole")["worker_num"]; + $out["env"] = $args["env"] === null ? "default" : $args["env"]; + $out["log_level"] = Console::getLevel(); + $out["version"] = ZM_VERSION; + if (APP_VERSION !== "unknown") $out["app_version"] = APP_VERSION; + if (isset(ZMConfig::get("global", "swoole")["task_worker_num"])) { + $out["task_worker"] = ZMConfig::get("global", "swoole")["task_worker_num"]; } - $motd = explode("\n", $motd); - foreach ($motd as $k => $v) { - $motd[$k] = substr($v, 0, $tty_width); + if (ZMConfig::get("global", "sql_config")["sql_host"] !== "") { + $conf = ZMConfig::get("global", "sql_config"); + $out["mysql_pool"] = $conf["database"] . "@" . $conf["sql_host"] . ":" . $conf["sql_port"]; } - $motd = implode("\n", $motd); - echo $motd; + if (ZMConfig::get("global", "redis_config")["host"] !== "") { + $conf = ZMConfig::get("global", "redis_config"); + $out["redis_pool"] = $conf["host"] . ":" . $conf["port"]; + } + if (ZMConfig::get("global", "static_file_server")["status"] !== false) { + $out["static_file_server"] = "enabled"; + } + + $out["working_dir"] = DataProvider::getWorkingDir(); + self::printProps($out, $tty_width, $args["log-theme"] === null); + + self::printMotd($tty_width); + global $asd; $asd = get_included_files(); // 注册 Swoole Server 的事件 @@ -171,6 +174,20 @@ class Framework } } + private static function printMotd($tty_width) { + if (file_exists(DataProvider::getWorkingDir() . "/config/motd.txt")) { + $motd = file_get_contents(DataProvider::getWorkingDir() . "/config/motd.txt"); + } else { + $motd = file_get_contents(__DIR__ . "/../../config/motd.txt"); + } + $motd = explode("\n", $motd); + foreach ($motd as $k => $v) { + $motd[$k] = substr($v, 0, $tty_width); + } + $motd = implode("\n", $motd); + echo $motd; + } + public function start() { self::$server->start(); } @@ -280,6 +297,95 @@ class Framework if ($coroutine_mode) Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); } + private static function writeNoDouble($k, $v, &$line_data, &$line_width, &$current_line, $colorful, $max_border) { + $tmp_line = $k . ": " . $v; + //Console::info("写入[".$tmp_line."]"); + if (strlen($tmp_line) >= $line_width[$current_line]) { //输出的内容太多了,以至于一行都放不下一个,要折行 + $title_strlen = strlen($k . ": "); + $content_len = $line_width[$current_line] - $title_strlen; + + $line_data[$current_line] = " " . $k . ": "; + if ($colorful) $line_data[$current_line] .= TermColor::color8(32); + $line_data[$current_line] .= substr($v, 0, $content_len); + if ($colorful) $line_data[$current_line] .= TermColor::RESET; + $rest = substr($v, $content_len); + ++$current_line; // 带标题的第一行满了,折到第二行 + do { + if ($colorful) $line_data[$current_line] = TermColor::color8(32); + $line_data[$current_line] .= " " . substr($rest, 0, $max_border - 2); + if ($colorful) $line_data[$current_line] .= TermColor::RESET; + $rest = substr($rest, $max_border - 2); + ++$current_line; + } while ($rest > $max_border - 2); // 循环,直到放完 + } else { // 不需要折行 + //Console::info("不需要折行"); + $line_data[$current_line] = " " . $k . ": "; + if ($colorful) $line_data[$current_line] .= TermColor::color8(32); + $line_data[$current_line] .= $v; + if ($colorful) $line_data[$current_line] .= TermColor::RESET; + + if ($max_border >= 57) { + if (strlen($tmp_line) >= intval(($max_border - 2) / 2)) { // 不需要折行,直接输出一个转下一行 + //Console::info("不需要折行,直接输出一个转下一行"); + ++$current_line; + } else { // 输出很小,写到前面并分片 + //Console::info("输出很小,写到前面并分片"); + $space = intval($max_border / 2) - 2 - strlen($tmp_line); + $line_data[$current_line] .= str_pad("", $space, " "); + $line_data[$current_line] .= "| "; // 添加分片 + $line_width[$current_line] -= (strlen($tmp_line) + 3 + $space); + } + } else { + ++$current_line; + } + } + } + + public static function printProps($out, $tty_width, $colorful = true) { + $max_border = $tty_width < 65 ? $tty_width : 65; + if (LOAD_MODE == 0) echo Console::setColor("* Framework started with source mode.\n", $colorful ? "yellow" : ""); + echo str_pad("", $max_border, "=") . PHP_EOL; + + $current_line = 0; + $line_width = []; + $line_data = []; + foreach ($out as $k => $v) { + if (!isset($line_width[$current_line])) { + $line_width[$current_line] = $max_border - 2; + } + //Console::info("行宽[$current_line]:".$line_width[$current_line]); + if ($max_border >= 57) { // 很宽的时候,一行能放两个短行 + if ($line_width[$current_line] == ($max_border - 2)) { //空行 + self::writeNoDouble($k, $v, $line_data, $line_width, $current_line, $colorful, $max_border); + } else { // 不是空行,已经有东西了 + $tmp_line = $k . ": " . $v; + //Console::info("[$current_line]即将插入后面的东西[".$tmp_line."]"); + if (strlen($tmp_line) > $line_width[$current_line]) { // 地方不够,另起一行 + $line_data[$current_line] = str_replace("| ", "", $line_data[$current_line]); + ++$current_line; + $line_data[$current_line] = " " . $k . ": "; + if ($colorful) $line_data[$current_line] .= TermColor::color8(32); + $line_data[$current_line] .= $v; + if ($colorful) $line_data[$current_line] .= TermColor::RESET; + ++$current_line; + } else { // 地方够,直接写到后面并另起一行 + $line_data[$current_line] .= $k . ": "; + if ($colorful) $line_data[$current_line] .= TermColor::color8(32); + $line_data[$current_line] .= $v; + if ($colorful) $line_data[$current_line] .= TermColor::RESET; + ++$current_line; + } + } + } else { // 不够宽,直接写单行 + self::writeNoDouble($k, $v, $line_data, $line_width, $current_line, $colorful, $max_border); + } + } + foreach ($line_data as $v) { + echo $v . PHP_EOL; + } + echo str_pad("", $max_border, "=") . PHP_EOL; + } + public static function getTtyWidth() { return explode(" ", trim(exec("stty size")))[1]; } diff --git a/src/ZM/global_defines.php b/src/ZM/global_defines.php index ec803e2d..80274677 100644 --- a/src/ZM/global_defines.php +++ b/src/ZM/global_defines.php @@ -6,7 +6,7 @@ use ZM\Utils\DataProvider; define("ZM_START_TIME", microtime(true)); define("ZM_DATA", ZMConfig::get("global", "zm_data")); define("ZM_VERSION", json_decode(file_get_contents(__DIR__ . "/../../composer.json"), true)["version"] ?? "unknown"); -define("APP_VERSION", json_decode(file_get_contents(DataProvider::getWorkingDir() . "/composer.json"), true)["version"] ?? "unknown"); +define("APP_VERSION", LOAD_MODE == 1 ? (json_decode(file_get_contents(DataProvider::getWorkingDir() . "/composer.json"), true)["version"] ?? "unknown") : "unknown"); define("CRASH_DIR", ZMConfig::get("global", "crash_dir")); @mkdir(ZM_DATA); @mkdir(CRASH_DIR);