From b6d1f724e95c30d9d5aea67999c74f729e0ac99d Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Thu, 18 Mar 2021 16:36:28 +0800 Subject: [PATCH] update to build 389 add various global functions --- docs/update/v2.md | 16 ++++++++-- src/ZM/ConsoleApplication.php | 4 +-- src/ZM/Framework.php | 1 + src/ZM/global_functions.php | 56 ++++++++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/docs/update/v2.md b/docs/update/v2.md index 670c513b..c1f12e2f 100644 --- a/docs/update/v2.md +++ b/docs/update/v2.md @@ -1,6 +1,18 @@ # 更新日志(v2 版本) -## 2.3.0 +## v2.3.2 + +> 更新时间:2021.3.18 + +- 新增:全局方法(`zm_dump()`,`zm_error()`,`zm_warning()`,`zm_info()`,`zm_success()`,`zm_verbose()`,`zm_debug()`,`zm_config()`,) + +## v2.3.1 + +> 更新时间:2021.3.18 + +- 规范代码,修复一个小报错的 bug + +## v2.3.0 > 更新时间:2021.3.16 @@ -14,7 +26,7 @@ 注:本次升级建议升级后合并全局配置文件,有一些新加的内容。 -## 2.2.11 +## v2.2.11 > 更新时间:2021.3.13 diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index 760b0b96..c0cc583b 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -18,8 +18,8 @@ use ZM\Utils\DataProvider; class ConsoleApplication extends Application { - const VERSION_ID = 388; - const VERSION = "2.3.1"; + const VERSION_ID = 389; + const VERSION = "2.3.2"; public function __construct(string $name = 'UNKNOWN') { define("ZM_VERSION_ID", self::VERSION_ID); diff --git a/src/ZM/Framework.php b/src/ZM/Framework.php index c164aeb0..0e966a45 100644 --- a/src/ZM/Framework.php +++ b/src/ZM/Framework.php @@ -304,6 +304,7 @@ class Framework } } if ($coroutine_mode) Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL); + else Runtime::enableCoroutine(false, SWOOLE_HOOK_ALL); } private static function writeNoDouble($k, $v, &$line_data, &$line_width, &$current_line, $colorful, $max_border) { diff --git a/src/ZM/global_functions.php b/src/ZM/global_functions.php index 14e790dd..9b38f3cf 100644 --- a/src/ZM/global_functions.php +++ b/src/ZM/global_functions.php @@ -3,6 +3,7 @@ use Swoole\Atomic; use Swoole\Coroutine; use Swoole\WebSocket\Server; +use Symfony\Component\VarDumper\VarDumper; use ZM\API\ZMRobot; use ZM\Config\ZMConfig; use ZM\ConnectionManager\ManagerGM; @@ -243,8 +244,6 @@ function ctx(): ?ContextInterface { } } -function zm_debug($msg) { Console::debug($msg); } - function onebot_target_id_name($message_type): string { return ($message_type == "group" ? "group_id" : "user_id"); } @@ -255,13 +254,21 @@ function zm_sleep($s = 1): bool { return true; } -function zm_exec($cmd): array { return System::exec($cmd); } +function zm_exec($cmd): array { + return System::exec($cmd); +} -function zm_cid() { return Co::getCid(); } +function zm_cid() { + return Co::getCid(); +} -function zm_yield() { Co::yield(); } +function zm_yield() { + Co::yield(); +} -function zm_resume(int $cid) { Co::resume($cid); } +function zm_resume(int $cid) { + Co::resume($cid); +} function zm_timer_after($ms, callable $callable) { Swoole\Timer::after($ms, function () use ($callable) { @@ -287,10 +294,14 @@ function zm_timer_tick($ms, callable $callable) { if (zm_cid() === -1) { return go(function () use ($ms, $callable) { Console::debug("Adding extra timer tick of " . $ms . " ms"); - Swoole\Timer::tick($ms, function () use ($callable) {call_with_catch($callable);}); + Swoole\Timer::tick($ms, function () use ($callable) { + call_with_catch($callable); + }); }); } else { - return Swoole\Timer::tick($ms, function () use ($callable) {call_with_catch($callable);}); + return Swoole\Timer::tick($ms, function () use ($callable) { + call_with_catch($callable); + }); } } @@ -354,3 +365,32 @@ function working_dir() { elseif (LOAD_MODE == 2) return realpath('.'); return null; } + +/** @noinspection PhpMissingReturnTypeInspection */ +function zm_dump($var, ...$moreVars) { + VarDumper::dump($var); + + foreach ($moreVars as $v) { + VarDumper::dump($v); + } + + if (1 < func_num_args()) { + return func_get_args(); + } + + return $var; +} + +function zm_info($obj) { Console::info($obj); } + +function zm_warning($obj) { Console::warning($obj); } + +function zm_success($obj) { Console::success($obj); } + +function zm_debug($obj) { Console::debug($obj); } + +function zm_verbose($obj) { Console::verbose($obj); } + +function zm_error($obj) { Console::error($obj); } + +function zm_config($name, $key = null) { return ZMConfig::get($name, $key); }