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
This commit is contained in:
jerry
2021-03-27 16:30:15 +08:00
parent beef44ea50
commit d699a152d5
11 changed files with 81 additions and 74 deletions

View File

@@ -1,5 +1,16 @@
# 更新日志v2 版本) # 更新日志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) ## v2.4.1 (build 401)
> 更新时间2021.3.25 > 更新时间2021.3.25
@@ -29,7 +40,7 @@
- 新增ZMUtil 工具杂项类 `getReloadableFiles()` 函数 - 新增ZMUtil 工具杂项类 `getReloadableFiles()` 函数
- 新增:`vendor/bin/start systemd:generate` 生成 systemd 配置文件的功能 - 新增:`vendor/bin/start systemd:generate` 生成 systemd 配置文件的功能
- 新增:`vendor/bin/start check:config` 检查配置文件更新的命令 - 新增:`vendor/bin/start check:config` 检查配置文件更新的命令
- 新增:`vendor/bin/init` 新增 `--force` 参数,覆盖现有文件重新生成 - 新增:`vendor/bin/start init` 新增 `--force` 参数,覆盖现有文件重新生成
- 新增MessageUtil 新增方法:`addShortCommand()`,用于快速添加静态文本问答回复的 - 新增MessageUtil 新增方法:`addShortCommand()`,用于快速添加静态文本问答回复的
以下是需要**手动更新**或**更换新写法**的部分: 以下是需要**手动更新**或**更换新写法**的部分:

View File

@@ -109,4 +109,5 @@ nav:
- 更新日志: - 更新日志:
- 更新日志v2: update/v2.md - 更新日志v2: update/v2.md
- 更新日志v1: update/v1.md - 更新日志v1: update/v1.md
- 配置文件更新日志: update/config.md
- <u>炸毛框架 v1</u>: https://docs-v1.zhamao.xin/ - <u>炸毛框架 v1</u>: https://docs-v1.zhamao.xin/

View File

@@ -19,11 +19,11 @@ class CheckConfigCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int { protected function execute(InputInterface $input, OutputInterface $output): int {
if (LOAD_MODE !== 1) { if (LOAD_MODE !== 1) {
$output->writeln("<error>仅限在Composer依赖模式中使用此命令"); $output->writeln("<error>仅限在Composer依赖模式中使用此命令</error>");
return Command::FAILURE; return Command::FAILURE;
} }
$current_cfg = getcwd() . "/config/"; $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")) { if (file_exists($current_cfg . "global.php")) {
$this->check($remote_cfg, "global.php", $output); $this->check($remote_cfg, "global.php", $output);
} }
@@ -37,7 +37,7 @@ class CheckConfigCommand extends Command
$this->check($remote_cfg, "global.production.php", $output); $this->check($remote_cfg, "global.production.php", $output);
} }
if ($this->need_update === true) { if ($this->need_update === true) {
$output->writeln("<comment>有配置文件需要更新,详情见文档 https://framework.zhamao.xin/update/config.md</comment>"); $output->writeln("<comment>有配置文件需要更新,详情见文档 `https://framework.zhamao.xin/update/config.md`</comment>");
} else { } else {
$output->writeln("<info>配置文件暂无更新!</info>"); $output->writeln("<info>配置文件暂无更新!</info>");
} }
@@ -51,9 +51,8 @@ class CheckConfigCommand extends Command
private function check($remote, $local, OutputInterface $out) { private function check($remote, $local, OutputInterface $out) {
$local_file = include_once getcwd() . "/config/".$local; $local_file = include_once getcwd() . "/config/".$local;
foreach($remote as $k => $v) { foreach($remote as $k => $v) {
$out->writeln("<comment>正在检查".$k."</comment>");
if (!isset($local_file[$k])) { if (!isset($local_file[$k])) {
$out->writeln("<error>配置文件 ".$local . " 需要更新!(缺少 `$k` 字段配置)</error>"); $out->writeln("<comment>配置文件 ".$local . " 需要更新!(当前配置文件缺少 `$k` 字段配置)</comment>");
$this->need_update = true; $this->need_update = true;
} }
} }

View File

@@ -37,7 +37,7 @@ class InitCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int { protected function execute(InputInterface $input, OutputInterface $output): int {
if (LOAD_MODE === 1) { // 从composer依赖而来的项目模式最基本的需要初始化的模式 if (LOAD_MODE === 1) { // 从composer依赖而来的项目模式最基本的需要初始化的模式
$output->writeln("<comment>Initializing files</comment>"); $output->writeln("<comment>Initializing files</comment>");
$base_path = LOAD_MODE_COMPOSER_PATH; $base_path = WORKING_DIR;
$args = $input->getOption("force"); $args = $input->getOption("force");
foreach ($this->extract_files as $file) { foreach ($this->extract_files as $file) {
if (!file_exists($base_path . $file) || $args) { if (!file_exists($base_path . $file) || $args) {
@@ -50,6 +50,12 @@ class InitCommand extends Command
echo "Skipping " . $file . " , file exists." . PHP_EOL; echo "Skipping " . $file . " , file exists." . PHP_EOL;
} }
} }
echo "Copying ./zhamao\n";
file_put_contents(
$base_path."/zhamao",
"#!/usr/bin/env php\n<?php require_once \"vendor/autoload.php\";(new ZM\ConsoleApplication(\"zhamao-framework\"))->initEnv()->run();"
);
chmod($base_path."/zhamao", 0755);
$autoload = [ $autoload = [
"psr-4" => [ "psr-4" => [
"Module\\" => "src/Module", "Module\\" => "src/Module",

View File

@@ -16,12 +16,11 @@ use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use ZM\Command\SystemdCommand; use ZM\Command\SystemdCommand;
use ZM\Utils\DataProvider;
class ConsoleApplication extends Application class ConsoleApplication extends Application
{ {
const VERSION_ID = 401; const VERSION_ID = 402;
const VERSION = "2.4.1"; const VERSION = "2.4.2";
public function __construct(string $name = 'UNKNOWN') { public function __construct(string $name = 'UNKNOWN') {
define("ZM_VERSION_ID", self::VERSION_ID); define("ZM_VERSION_ID", self::VERSION_ID);
@@ -29,35 +28,24 @@ class ConsoleApplication extends Application
parent::__construct($name, ZM_VERSION); parent::__construct($name, ZM_VERSION);
} }
public function initEnv(): ConsoleApplication { public function initEnv($with_default_cmd = ""): ConsoleApplication {
$this->selfCheck(); $this->selfCheck();
if (!is_dir(__DIR__ . '/../../vendor')) { define("WORKING_DIR", getcwd());
define("LOAD_MODE", 1); // composer项目模式 define("LOAD_MODE", is_dir(WORKING_DIR . "/src/ZM") ? 0 : 1);
define("LOAD_MODE_COMPOSER_PATH", getcwd()); define("FRAMEWORK_ROOT_DIR", realpath(__DIR__ . "/../../"));
} 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";
}
if (LOAD_MODE == 0) { 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\\"])) { if (!isset($composer["autoload"]["psr-4"]["Module\\"])) {
echo "框架源码模式需要在autoload文件中添加Module目录为自动加载是否添加[Y/n] "; echo "框架源码模式需要在autoload文件中添加Module目录为自动加载是否添加[Y/n] ";
$r = strtolower(trim(fgets(STDIN))); $r = strtolower(trim(fgets(STDIN)));
if ($r === "" || $r === "y") { if ($r === "" || $r === "y") {
$composer["autoload"]["psr-4"]["Module\\"] = "src/Module"; $composer["autoload"]["psr-4"]["Module\\"] = "src/Module";
$composer["autoload"]["psr-4"]["Custom\\"] = "src/Custom"; $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) { if ($r !== false) {
echo "成功添加!请运行 composer dump-autoload \n"; echo "成功添加!请运行 composer dump-autoload \n";
exit(1); exit(0);
} else { } else {
echo "添加失败!请按任意键继续!"; echo "添加失败!请按任意键继续!";
fgets(STDIN); fgets(STDIN);
@@ -81,6 +69,9 @@ class ConsoleApplication extends Application
if (LOAD_MODE === 1) { if (LOAD_MODE === 1) {
$this->add(new CheckConfigCommand()); $this->add(new CheckConfigCommand());
} }
if (!empty($with_default_cmd)) {
$this->setDefaultCommand($with_default_cmd);
}
/* /*
$command_register = ZMConfig::get("global", "command_register_class") ?? []; $command_register = ZMConfig::get("global", "command_register_class") ?? [];
foreach ($command_register as $v) { foreach ($command_register as $v) {

View File

@@ -404,7 +404,9 @@ class Framework
} }
break; break;
case 'remote-terminal': case 'remote-terminal':
$add_port = true; if ($y) {
$add_port = true;
}
break; break;
case 'show-php-ver': case 'show-php-ver':
default: default:

View File

@@ -15,8 +15,6 @@ class LightCache
{ {
/** @var Table|null */ /** @var Table|null */
private static $kv_table = null; private static $kv_table = null;
/** @var Table|null */
private static $kv_lock = null;
private static $config = []; private static $config = [];
@@ -35,14 +33,13 @@ class LightCache
self::$kv_table->column("expire", Table::TYPE_INT); self::$kv_table->column("expire", Table::TYPE_INT);
self::$kv_table->column("data_type", Table::TYPE_STRING, 8); self::$kv_table->column("data_type", Table::TYPE_STRING, 8);
$result = self::$kv_table->create(); $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 ($result === true && isset($config["persistence_path"])) {
if (file_exists($config["persistence_path"])) { if (file_exists($config["persistence_path"])) {
$r = json_decode(file_get_contents($config["persistence_path"]), true); $r = json_decode(file_get_contents($config["persistence_path"]), true);
if ($r === null) $r = []; if ($r === null) $r = [];
foreach ($r as $k => $v) { foreach ($r as $k => $v) {
$write = self::set($k, $v, -2); $write = self::set($k, $v);
Console::verbose("Writing LightCache: " . $k); Console::verbose("Writing LightCache: " . $k);
if ($write === false) { if ($write === false) {
self::$last_error = '可能是由于 Hash 冲突过多导致动态空间无法分配内存'; self::$last_error = '可能是由于 Hash 冲突过多导致动态空间无法分配内存';
@@ -180,6 +177,30 @@ class LightCache
return $r; 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 * @throws Exception
@@ -189,17 +210,15 @@ class LightCache
$dispatcher->dispatchEvents(); $dispatcher->dispatchEvents();
if (self::$kv_table === null) return; if (self::$kv_table === null) return;
$r = [];
foreach (self::$kv_table as $k => $v) { if (!empty(self::$config["persistence_path"])) {
if ($v["expire"] === -2) { $r = json_decode(file_get_contents(self::$config["persistence_path"]), true);
if ($r === null) $r = [];
foreach ($r as $k => $v) {
Console::verbose("Saving " . $k); Console::verbose("Saving " . $k);
$r[$k] = self::parseGet($v); $r[$k] = self::get($k);
} }
} file_put_contents(self::$config["persistence_path"], json_encode($r, 64 | 128 | 256));
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\"!");
} }
Console::verbose("Saved."); Console::verbose("Saved.");
} }

View File

@@ -7,6 +7,7 @@ namespace ZM\Store\Lock;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\System; use Swoole\Coroutine\System;
use Swoole\Table; use Swoole\Table;
use ZM\Console\Console;
class SpinLock class SpinLock
{ {

View File

@@ -16,10 +16,7 @@ class DataProvider
} }
public static function getWorkingDir() { public static function getWorkingDir() {
if (LOAD_MODE == 0) return WORKING_DIR; return WORKING_DIR;
elseif (LOAD_MODE == 1) return LOAD_MODE_COMPOSER_PATH;
elseif (LOAD_MODE == 2) return realpath('.');
return null;
} }
public static function getFrameworkLink() { public static function getFrameworkLink() {

View File

@@ -1,4 +1,4 @@
<?php /** @noinspection PhpUnused */ #plain <?php #plain
use Swoole\Atomic; use Swoole\Atomic;
use Swoole\Coroutine; use Swoole\Coroutine;
@@ -21,33 +21,13 @@ use Swoole\Coroutine\System;
use ZM\Context\ContextInterface; use ZM\Context\ContextInterface;
function phar_classloader($p) {
$filepath = getClassPath($p);
if ($filepath === null) {
Console::debug("F:Warning: get class path wrongs.$p");
return;
}
try {
/** @noinspection PhpIncludeInspection */
require_once $filepath;
} catch (Exception $e) {
echo "Error when finding class: " . $p . PHP_EOL;
die;
}
}
function getClassPath($class_name) { function getClassPath($class_name) {
$dir = str_replace("\\", "/", $class_name); $dir = str_replace("\\", "/", $class_name);
$dir2 = WORKING_DIR . "/src/" . $dir . ".php"; $dir2 = WORKING_DIR . "/src/" . $dir . ".php";
//echo "@@@".$dir2.PHP_EOL; //echo "@@@".$dir2.PHP_EOL;
$dir2 = str_replace("\\", "/", $dir2); $dir2 = str_replace("\\", "/", $dir2);
if (file_exists($dir2)) return $dir2; if (file_exists($dir2)) return $dir2;
else { else return null;
$dir = DataProvider::getWorkingDir() . "/src/" . $dir . ".php";
//echo "###".$dir.PHP_EOL;
if (file_exists($dir)) return $dir;
else return null;
}
} }
/** /**
@@ -360,13 +340,9 @@ function uuidgen($uppercase = false): string {
} }
function working_dir() { function working_dir() {
if (LOAD_MODE == 0) return WORKING_DIR; return WORKING_DIR;
elseif (LOAD_MODE == 1) return LOAD_MODE_COMPOSER_PATH;
elseif (LOAD_MODE == 2) return realpath('.');
return null;
} }
/** @noinspection PhpMissingReturnTypeInspection */
function zm_dump($var, ...$moreVars) { function zm_dump($var, ...$moreVars) {
VarDumper::dump($var); VarDumper::dump($var);

4
zhamao Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env php
<?php
require_once "vendor/autoload.php";
(new ZM\ConsoleApplication("zhamao-framework"))->initEnv("server")->run();