From d699a152d50746b8b86616c9c4397aeb1b5c3c41 Mon Sep 17 00:00:00 2001 From: jerry Date: Sat, 27 Mar 2021 16:30:15 +0800 Subject: [PATCH] update to 2.4.2 version (build 402) change WORKING_DIR constant change logic of savePersistence() add LightCache addPersistence() and removePersistence() method add `./zhamao` command --- docs/update/v2.md | 13 +++++++- mkdocs.yml | 1 + src/ZM/Command/CheckConfigCommand.php | 9 +++-- src/ZM/Command/InitCommand.php | 8 ++++- src/ZM/ConsoleApplication.php | 33 +++++++------------ src/ZM/Framework.php | 4 ++- src/ZM/Store/LightCache.php | 47 +++++++++++++++++++-------- src/ZM/Store/Lock/SpinLock.php | 1 + src/ZM/Utils/DataProvider.php | 5 +-- src/ZM/global_functions.php | 30 ++--------------- zhamao | 4 +++ 11 files changed, 81 insertions(+), 74 deletions(-) create mode 100755 zhamao diff --git a/docs/update/v2.md b/docs/update/v2.md index b0b5997e..068c6f74 100644 --- a/docs/update/v2.md +++ b/docs/update/v2.md @@ -1,5 +1,16 @@ # 更新日志(v2 版本) +# v2.4.2 (build 402) + +> 更新时间:202.3.27 + +- 更改:`WORKING_DIR` 常量的含义 +- 修复:未指定 `--remote-terminal` 参数时还依旧开启远程终端的 bug +- 删除:`phar_classloader()` 全局方法 +- 更改:持久化存储 LightCache 的逻辑,修复一个愚蠢的容易造成误用的方式 +- 新增:LightCache 方法 `addPersistence()` 和 `removePersistence()` +- 新增:框架启动短指令 `./zhamao` 或 `php zhamao` + ## v2.4.1 (build 401) > 更新时间:2021.3.25 @@ -29,7 +40,7 @@ - 新增:ZMUtil 工具杂项类 `getReloadableFiles()` 函数 - 新增:`vendor/bin/start systemd:generate` 生成 systemd 配置文件的功能 - 新增:`vendor/bin/start check:config` 检查配置文件更新的命令 -- 新增:`vendor/bin/init` 新增 `--force` 参数,覆盖现有文件重新生成 +- 新增:`vendor/bin/start init` 新增 `--force` 参数,覆盖现有文件重新生成 - 新增:MessageUtil 新增方法:`addShortCommand()`,用于快速添加静态文本问答回复的 以下是需要**手动更新**或**更换新写法**的部分: diff --git a/mkdocs.yml b/mkdocs.yml index 28aee039..22061180 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -109,4 +109,5 @@ nav: - 更新日志: - 更新日志(v2): update/v2.md - 更新日志(v1): update/v1.md + - 配置文件更新日志: update/config.md - 炸毛框架 v1: https://docs-v1.zhamao.xin/ diff --git a/src/ZM/Command/CheckConfigCommand.php b/src/ZM/Command/CheckConfigCommand.php index 1d12f0c1..a2a7fa5c 100644 --- a/src/ZM/Command/CheckConfigCommand.php +++ b/src/ZM/Command/CheckConfigCommand.php @@ -19,11 +19,11 @@ class CheckConfigCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { if (LOAD_MODE !== 1) { - $output->writeln("仅限在Composer依赖模式中使用此命令!"); + $output->writeln("仅限在Composer依赖模式中使用此命令!"); return Command::FAILURE; } $current_cfg = getcwd() . "/config/"; - $remote_cfg = include_once WORKING_DIR . "/config/global.php"; + $remote_cfg = include_once FRAMEWORK_ROOT_DIR . "/config/global.php"; if (file_exists($current_cfg . "global.php")) { $this->check($remote_cfg, "global.php", $output); } @@ -37,7 +37,7 @@ class CheckConfigCommand extends Command $this->check($remote_cfg, "global.production.php", $output); } if ($this->need_update === true) { - $output->writeln("有配置文件需要更新,详情见文档 https://framework.zhamao.xin/update/config.md"); + $output->writeln("有配置文件需要更新,详情见文档 `https://framework.zhamao.xin/update/config.md`"); } else { $output->writeln("配置文件暂无更新!"); } @@ -51,9 +51,8 @@ class CheckConfigCommand extends Command private function check($remote, $local, OutputInterface $out) { $local_file = include_once getcwd() . "/config/".$local; foreach($remote as $k => $v) { - $out->writeln("正在检查".$k.""); if (!isset($local_file[$k])) { - $out->writeln("配置文件 ".$local . " 需要更新!(缺少 `$k` 字段配置)"); + $out->writeln("配置文件 ".$local . " 需要更新!(当前配置文件缺少 `$k` 字段配置)"); $this->need_update = true; } } diff --git a/src/ZM/Command/InitCommand.php b/src/ZM/Command/InitCommand.php index 8597f80f..ccebea0b 100644 --- a/src/ZM/Command/InitCommand.php +++ b/src/ZM/Command/InitCommand.php @@ -37,7 +37,7 @@ class InitCommand extends Command protected function execute(InputInterface $input, OutputInterface $output): int { if (LOAD_MODE === 1) { // 从composer依赖而来的项目模式,最基本的需要初始化的模式 $output->writeln("Initializing files"); - $base_path = LOAD_MODE_COMPOSER_PATH; + $base_path = WORKING_DIR; $args = $input->getOption("force"); foreach ($this->extract_files as $file) { if (!file_exists($base_path . $file) || $args) { @@ -50,6 +50,12 @@ class InitCommand extends Command echo "Skipping " . $file . " , file exists." . PHP_EOL; } } + echo "Copying ./zhamao\n"; + file_put_contents( + $base_path."/zhamao", + "#!/usr/bin/env php\ninitEnv()->run();" + ); + chmod($base_path."/zhamao", 0755); $autoload = [ "psr-4" => [ "Module\\" => "src/Module", diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 7f59168c..9ad65294 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -16,12 +16,11 @@ use Symfony\Component\Console\Application; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use ZM\Command\SystemdCommand; -use ZM\Utils\DataProvider; class ConsoleApplication extends Application { - const VERSION_ID = 401; - const VERSION = "2.4.1"; + const VERSION_ID = 402; + const VERSION = "2.4.2"; public function __construct(string $name = 'UNKNOWN') { define("ZM_VERSION_ID", self::VERSION_ID); @@ -29,35 +28,24 @@ class ConsoleApplication extends Application parent::__construct($name, ZM_VERSION); } - public function initEnv(): ConsoleApplication { + public function initEnv($with_default_cmd = ""): ConsoleApplication { $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__ . "/../../")); - if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) { - /** @noinspection PhpIncludeInspection */ - require_once DataProvider::getWorkingDir() . "/vendor/autoload.php"; - } + define("WORKING_DIR", getcwd()); + define("LOAD_MODE", is_dir(WORKING_DIR . "/src/ZM") ? 0 : 1); + define("FRAMEWORK_ROOT_DIR", realpath(__DIR__ . "/../../")); if (LOAD_MODE == 0) { - $composer = json_decode(file_get_contents(DataProvider::getWorkingDir() . "/composer.json"), true); + $composer = json_decode(file_get_contents(WORKING_DIR . "/composer.json"), true); if (!isset($composer["autoload"]["psr-4"]["Module\\"])) { echo "框架源码模式需要在autoload文件中添加Module目录为自动加载,是否添加?[Y/n] "; $r = strtolower(trim(fgets(STDIN))); if ($r === "" || $r === "y") { $composer["autoload"]["psr-4"]["Module\\"] = "src/Module"; $composer["autoload"]["psr-4"]["Custom\\"] = "src/Custom"; - $r = file_put_contents(DataProvider::getWorkingDir() . "/composer.json", json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); + $r = file_put_contents(WORKING_DIR . "/composer.json", json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); if ($r !== false) { echo "成功添加!请运行 composer dump-autoload !\n"; - exit(1); + exit(0); } else { echo "添加失败!请按任意键继续!"; fgets(STDIN); @@ -81,6 +69,9 @@ class ConsoleApplication extends Application if (LOAD_MODE === 1) { $this->add(new CheckConfigCommand()); } + if (!empty($with_default_cmd)) { + $this->setDefaultCommand($with_default_cmd); + } /* $command_register = ZMConfig::get("global", "command_register_class") ?? []; foreach ($command_register as $v) { diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index 218e52ca..faf59cfa 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -404,7 +404,9 @@ class Framework } break; case 'remote-terminal': - $add_port = true; + if ($y) { + $add_port = true; + } break; case 'show-php-ver': default: diff --git a/src/ZM/Store/LightCache.php b/src/ZM/Store/LightCache.php index b88ca3dd..0d7e57a0 100644 --- a/src/ZM/Store/LightCache.php +++ b/src/ZM/Store/LightCache.php @@ -15,8 +15,6 @@ class LightCache { /** @var Table|null */ private static $kv_table = null; - /** @var Table|null */ - private static $kv_lock = null; private static $config = []; @@ -35,14 +33,13 @@ class LightCache self::$kv_table->column("expire", Table::TYPE_INT); self::$kv_table->column("data_type", Table::TYPE_STRING, 8); $result = self::$kv_table->create(); - self::$kv_lock = new Table($config["size"], $config["hash_conflict_proportion"]); - $result = $result && self::$kv_lock->create(); + // 加载内容 if ($result === true && isset($config["persistence_path"])) { if (file_exists($config["persistence_path"])) { $r = json_decode(file_get_contents($config["persistence_path"]), true); if ($r === null) $r = []; foreach ($r as $k => $v) { - $write = self::set($k, $v, -2); + $write = self::set($k, $v); Console::verbose("Writing LightCache: " . $k); if ($write === false) { self::$last_error = '可能是由于 Hash 冲突过多导致动态空间无法分配内存'; @@ -180,6 +177,30 @@ class LightCache return $r; } + public static function addPersistence($key) { + if (file_exists(self::$config["persistence_path"])) { + $r = json_decode(file_get_contents(self::$config["persistence_path"]), true); + if ($r === null) $r = []; + if (!isset($r[$key])) $r[$key] = null; + file_put_contents(self::$config["persistence_path"], json_encode($r, 64 | 128 | 256)); + return true; + } else { + return false; + } + } + + public static function removePersistence($key) { + if (file_exists(self::$config["persistence_path"])) { + $r = json_decode(file_get_contents(self::$config["persistence_path"]), true); + if ($r === null) $r = []; + if (isset($r[$key])) unset($r[$key]); + file_put_contents(self::$config["persistence_path"], json_encode($r, 64 | 128 | 256)); + return true; + } else { + return false; + } + } + /** * 这个只能在唯一一个工作进程中执行 * @throws Exception @@ -189,17 +210,15 @@ class LightCache $dispatcher->dispatchEvents(); if (self::$kv_table === null) return; - $r = []; - foreach (self::$kv_table as $k => $v) { - if ($v["expire"] === -2) { + + if (!empty(self::$config["persistence_path"])) { + $r = json_decode(file_get_contents(self::$config["persistence_path"]), true); + if ($r === null) $r = []; + foreach ($r as $k => $v) { Console::verbose("Saving " . $k); - $r[$k] = self::parseGet($v); + $r[$k] = self::get($k); } - } - if (self::$config["persistence_path"] == "") return; - if (file_exists(self::$config["persistence_path"])) { - $r = file_put_contents(self::$config["persistence_path"], json_encode($r, 128 | 256)); - if ($r === false) Console::error("Not saved, please check your \"persistence_path\"!"); + file_put_contents(self::$config["persistence_path"], json_encode($r, 64 | 128 | 256)); } Console::verbose("Saved."); } diff --git a/src/ZM/Store/Lock/SpinLock.php b/src/ZM/Store/Lock/SpinLock.php index 55e632e7..4f302823 100644 --- a/src/ZM/Store/Lock/SpinLock.php +++ b/src/ZM/Store/Lock/SpinLock.php @@ -7,6 +7,7 @@ namespace ZM\Store\Lock; use Swoole\Coroutine; use Swoole\Coroutine\System; use Swoole\Table; +use ZM\Console\Console; class SpinLock { diff --git a/src/ZM/Utils/DataProvider.php b/src/ZM/Utils/DataProvider.php index 23513dc9..df320dd4 100644 --- a/src/ZM/Utils/DataProvider.php +++ b/src/ZM/Utils/DataProvider.php @@ -16,10 +16,7 @@ class DataProvider } public static function getWorkingDir() { - if (LOAD_MODE == 0) return WORKING_DIR; - elseif (LOAD_MODE == 1) return LOAD_MODE_COMPOSER_PATH; - elseif (LOAD_MODE == 2) return realpath('.'); - return null; + return WORKING_DIR; } public static function getFrameworkLink() { diff --git a/src/ZM/global_functions.php b/src/ZM/global_functions.php index 903c6f5c..83662aa9 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -1,4 +1,4 @@ -initEnv("server")->run();