mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 00:25:35 +08:00
update plugin install and load strategy
This commit is contained in:
15
src/ZM/Plugin/Strategy/ComposerStrategy.php
Normal file
15
src/ZM/Plugin/Strategy/ComposerStrategy.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Plugin\Strategy;
|
||||
|
||||
class ComposerStrategy extends PluginInstallStrategy
|
||||
{
|
||||
public function install(array $option = []): bool
|
||||
{
|
||||
// TODO: Composer 类型的插件还没有实现怎么安装,但很简单。这次 Commit 我偏要鸽!
|
||||
$this->error = 'Not implemented';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
149
src/ZM/Plugin/Strategy/GitStrategy.php
Normal file
149
src/ZM/Plugin/Strategy/GitStrategy.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Plugin\Strategy;
|
||||
|
||||
use ZM\Utils\ZMRequest;
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
class GitStrategy extends PluginInstallStrategy
|
||||
{
|
||||
private string $git_api_link = 'https://api.github.com/repos/{owner}/{repo}/contents/composer.json';
|
||||
|
||||
private string $token = '';
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function install(array $option = []): bool
|
||||
{
|
||||
// 应用 git_api_link
|
||||
if (isset($option['git-api-link'])) {
|
||||
$this->git_api_link = $option['git-api-link'];
|
||||
}
|
||||
if (isset($option['github-token'])) {
|
||||
$this->token = $option['github-token'];
|
||||
}
|
||||
|
||||
$git_url = parse_url($this->input);
|
||||
|
||||
// GitHub 做特殊处理,直接调用 API 检查
|
||||
$is_github = false;
|
||||
$plugin_name = null;
|
||||
if ($git_url['host'] === 'github.com' && ($option['github-skip-check'] ?? false) !== true) {
|
||||
if (!$this->checkGitAPI($git_url, $plugin_name)) {
|
||||
return false;
|
||||
}
|
||||
$is_github = true;
|
||||
}
|
||||
|
||||
// 使用 Composer 管理插件,将仓库链接绑定到 composer.json
|
||||
$this->logger->info('正在使用 Composer 下载并安装插件');
|
||||
$composer = ZMUtil::getComposerMetadata($this->root_composer_path);
|
||||
$origin_composer = $composer;
|
||||
$already_has_repo = false;
|
||||
// 不破坏原有队列,加入 GitHub 的 repo
|
||||
if (!isset($composer['repositories'])) {
|
||||
$composer['repositories'] = [];
|
||||
}
|
||||
if (is_assoc_array($composer['repositories'])) {
|
||||
$composer['repositories'] = [$composer['repositories']];
|
||||
}
|
||||
foreach ($composer['repositories'] as $v) {
|
||||
if (($v['url'] ?? '') === $this->input) {
|
||||
$already_has_repo = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$already_has_repo) {
|
||||
$composer['repositories'][] = [
|
||||
'type' => $is_github ? 'github' : 'git',
|
||||
'url' => $this->input,
|
||||
'.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';
|
||||
}
|
||||
if ($plugin_name === null) {
|
||||
$this->error = '没有从 Git 获取到插件的元信息,目前无法从 GitHub 以外的 Git 仓库下载插件,后续会更新!';
|
||||
ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer);
|
||||
return false;
|
||||
}
|
||||
if ($plugin_name === '') {
|
||||
$this->error = '获取插件名称失败!';
|
||||
ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer);
|
||||
return false;
|
||||
}
|
||||
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);
|
||||
if (function_exists('pcntl_signal')) {
|
||||
pcntl_signal(SIGINT, SIG_IGN);
|
||||
}
|
||||
if ($code !== 0) {
|
||||
$this->error = '使用 composer 引入 Git 插件出现了一些错误,请看上方错误';
|
||||
ZMUtil::putComposerMetadata($this->root_composer_path, $origin_composer);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于调用 GitHub 的 API 用作不下载就检查插件是否合规
|
||||
*
|
||||
* @param array $git_url 解析后的链接
|
||||
*/
|
||||
private function checkGitAPI(array $git_url, ?string &$plugin_name = null): bool
|
||||
{
|
||||
$this->logger->info('正在检查 GitHub 插件是否为框架插件');
|
||||
[, $owner, $repo] = explode('/', $git_url['path']);
|
||||
if (str_ends_with($repo, '.git')) {
|
||||
$repo = substr($repo, 0, -4);
|
||||
}
|
||||
|
||||
// 调用 HTTP 客户端获取 API 信息
|
||||
$header = ['User-Agent' => 'zhamao-framework'];
|
||||
if ($this->token !== '') {
|
||||
$header['Authorization'] = 'token ' . $this->token;
|
||||
}
|
||||
$api = ZMRequest::get(
|
||||
str_replace(['{owner}', '{repo}'], [$owner, $repo], $this->git_api_link),
|
||||
$header,
|
||||
only_body: false
|
||||
);
|
||||
if ($api->getStatusCode() !== 200) {
|
||||
$this->error = "GitHub API 请求失败[{$api->getStatusCode()}]";
|
||||
if ($api->getStatusCode() === 403) {
|
||||
$this->error .= '可能是 API 滥用导致的,建议生成一个 GitHub Token。';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检查插件的 composer.json 是否合规
|
||||
$content = json_decode($api->getBody()->getContents(), true);
|
||||
if (isset($content['message'])) {
|
||||
$this->error = '该 GitHub 仓库中不存在 composer.json 文件!';
|
||||
return false;
|
||||
}
|
||||
$contents = implode('', array_map(fn ($x) => base64_decode($x), explode("\n", $content['content'])));
|
||||
$json = json_decode($contents, true);
|
||||
if (!$this->checkComposerIntegrity($json)) {
|
||||
return false;
|
||||
}
|
||||
$plugin_name = $json['name'];
|
||||
$this->installed_name = $plugin_name;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
67
src/ZM/Plugin/Strategy/PluginInstallStrategy.php
Normal file
67
src/ZM/Plugin/Strategy/PluginInstallStrategy.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Plugin\Strategy;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ZM\Plugin\PluginManager;
|
||||
|
||||
abstract class PluginInstallStrategy
|
||||
{
|
||||
protected string $error = '';
|
||||
|
||||
protected string $installed_name = '';
|
||||
|
||||
public function __construct(
|
||||
protected string $input,
|
||||
protected string $plugin_dir,
|
||||
protected string $root_composer_path = '',
|
||||
protected ?LoggerInterface $logger = null,
|
||||
) {
|
||||
if ($this->root_composer_path === '') {
|
||||
$this->root_composer_path = zm_dir(WORKING_DIR);
|
||||
}
|
||||
if ($this->logger === null) {
|
||||
$this->logger = ob_logger();
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function install(array $option = []): bool;
|
||||
|
||||
public function getError(): string
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
public function getInstalledName(): string
|
||||
{
|
||||
return $this->installed_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于检查 Composer 文件的信息是否完整
|
||||
*/
|
||||
protected function checkComposerIntegrity(mixed $composer): bool
|
||||
{
|
||||
// 必须是 array
|
||||
if (!is_array($composer)) {
|
||||
$this->error = 'composer.json 元信息获取出错';
|
||||
return false;
|
||||
}
|
||||
if (!isset($composer['extra']['zm-plugin-version'])) {
|
||||
$this->error = 'composer.json 内没有标明该炸毛插件的版本,或该仓库不是炸毛插件';
|
||||
return false;
|
||||
}
|
||||
if (!isset($composer['name'])) {
|
||||
$this->error = 'composer.json 插件元信息内没有名字!';
|
||||
return false;
|
||||
}
|
||||
$plugin_name = $composer['name'];
|
||||
if (PluginManager::isPluginExists($plugin_name)) {
|
||||
$this->error = "插件 {$plugin_name} 已存在,无法再次安装";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user