From d56f4b23432ff9680caa80d2a72e4e7afb119cab Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Wed, 29 Mar 2023 20:43:29 +0800 Subject: [PATCH] adjust git strategy --- src/ZM/Plugin/Strategy/GitStrategy.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ZM/Plugin/Strategy/GitStrategy.php b/src/ZM/Plugin/Strategy/GitStrategy.php index cca5d949..4a10852a 100644 --- a/src/ZM/Plugin/Strategy/GitStrategy.php +++ b/src/ZM/Plugin/Strategy/GitStrategy.php @@ -43,6 +43,7 @@ class GitStrategy extends PluginInstallStrategy $composer = ZMUtil::getComposerMetadata($this->root_composer_path); $origin_composer = $composer; $already_has_repo = false; + // 不破坏原有队列,加入 GitHub 的 repo if (!isset($composer['repositories'])) { $composer['repositories'] = []; @@ -56,6 +57,8 @@ class GitStrategy extends PluginInstallStrategy break; } } + + // 缓存一下对应的 repositories 属于的插件名称 if (!$already_has_repo) { $composer['repositories'][] = [ 'type' => $is_github ? 'github' : 'git', @@ -63,32 +66,42 @@ class GitStrategy extends PluginInstallStrategy '.belongs' => $plugin_name, ]; } + // 写入 composer.json if (ZMUtil::putComposerMetadata($this->root_composer_path, $composer) === false) { $this->error = '写入 composer.json 失败'; return false; } - $env = getenv('COMPOSER_EXECUTABLE'); - if ($env === false) { - $env = 'composer'; - } + + // 获取 Composer 命令行名称 + $env = ZMUtil::getComposerExecutable(); + + // 这里返回 null 表明没调用成功 GitHub API 拿 composer.json 的元信息 if ($plugin_name === null) { $this->error = '没有从 Git 获取到插件的元信息,目前无法从 GitHub 以外的 Git 仓库下载插件,后续会更新!'; ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer); return false; } + + // 这里为空字符串表明插件名称不对,获取了空的,说明元 composer.json 文件出错 if ($plugin_name === '') { $this->error = '获取插件名称失败!'; ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer); return false; } + + // Git 方式拉取插件,在 Linux 系统上监听一下 Ctrl+C,这样即使用户想 Ctrl+C 断掉,也可以方便地恢复原来的 composer.json 文件内容。 if (function_exists('pcntl_signal')) { pcntl_signal(SIGINT, function () use ($origin_composer) { echo "强行中断,恢复 Composer 中\n"; ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer); }); } + + // 引入 passthru("{$env} require {$plugin_name}", $code); + + // 恢复 SIGINT 信号 if (function_exists('pcntl_signal')) { pcntl_signal(SIGINT, SIG_IGN); }