From 65c609754a666f22f49573da5eb16648d2da99be Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 18 Feb 2023 14:38:48 +0800 Subject: [PATCH] update class-alias update command --- .../Command/Generate/TextGenerateCommand.php | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/ZM/Command/Generate/TextGenerateCommand.php b/src/ZM/Command/Generate/TextGenerateCommand.php index b47b975f..45e934dc 100644 --- a/src/ZM/Command/Generate/TextGenerateCommand.php +++ b/src/ZM/Command/Generate/TextGenerateCommand.php @@ -45,11 +45,29 @@ class TextGenerateCommand extends Command $full_maxlen = max($full_maxlen, strlen('`' . $full_class . '`')); $short_maxlen = max($short_maxlen, strlen('`' . $short_class . '`')); } - $this->write('| ' . str_pad('全类名', $full_maxlen) . ' | ' . str_pad('别名', $short_maxlen) . ' |'); - $this->write('| ' . str_pad('', $full_maxlen, '-') . ' | ' . str_pad('', $short_maxlen, '-') . ' |'); - foreach ($line as $v) { - $this->write('| ' . str_pad('`' . $v[0] . '`', $full_maxlen) . ' | ' . str_pad('`' . $v[1] . '`', $short_maxlen) . ' |'); - } + $obj = <<<'LINE' +# 类全局别名 + +在框架 1.x 和 2.x 老版本中,我们发现许多开发者在使用框架时,往往不会使用 PhpStorm 这类大型 IDE,而即使使用 VSCode 这类编辑器的时候也不一定会安装补全插件, +这样在编写机器人模块或插件时会因寻找每个对象的完整命名空间而烦恼。 + +在 3.0 版本起,框架对常用的注解事件和对象均使用了类别名功能,方便非 IDE 开发者编写插件。 + +## 别名使用 + +框架对别名的定义比较简单,由于内部暂时没有不同命名空间下重复类名的情况,所以我们目前只对需要别名类名的命名空间移除,例如: + +`\ZM\Annotation\OneBot\BotCommand` 注解事件类,在经过全局别名后,你也可以使用 `\BotCommand` 作为注解事件,效果相同。 + +## 别名列表 +LINE; + file_put_contents( + SOURCE_ROOT_DIR . '/docs/components/common/class-alias.md', + $obj . "\n" . '| ' . str_pad('全类名', $full_maxlen) . ' | ' . str_pad('别名', $short_maxlen) . ' |' . + "\n" . '| ' . str_pad('', $full_maxlen, '-') . ' | ' . str_pad('', $short_maxlen, '-') . ' |' . + "\n" . implode("\n", array_map(fn ($v) => '| ' . str_pad('`' . $v[0] . '`', $full_maxlen) . ' | ' . str_pad('`' . $v[1] . '`', $short_maxlen) . ' |', $line)) . "\n" + ); + $this->write('成功'); return static::SUCCESS; }