|
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|
|
namespace SixShop\System;
|
|
|
|
|
|
use Composer\InstalledVersions;
|
|
|
+use Composer\Json\JsonFile;
|
|
|
use RuntimeException;
|
|
|
use SixShop\Core\Contracts\ExtensionInterface;
|
|
|
use SixShop\Core\Service\CoreService;
|
|
|
@@ -310,17 +311,82 @@ class ExtensionManager extends Service
|
|
|
|
|
|
public function refresh(string $id): void
|
|
|
{
|
|
|
- // 获取当前版本更新版本信息
|
|
|
- $currentVersion = '0.0.0';
|
|
|
- if (isset(CoreService::$extensionComposerMap[$id])) {
|
|
|
- $packageName = CoreService::$extensionComposerMap[$id]['name'];
|
|
|
- $currentVersion = InstalledVersions::getPrettyVersion($packageName);
|
|
|
+ // 刷新单个扩展最新信息(含版本)
|
|
|
+ $packageName = CoreService::getExtensionComposerMap()[$id]['name'] ?? null;
|
|
|
+ if (!is_string($packageName) || $packageName === '') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $this->upsertExtensionInfo($id, $packageName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步所有已安装扩展的最新信息(名称、描述、版本、作者等)到扩展表
|
|
|
+ *
|
|
|
+ * @return int 同步的扩展数量
|
|
|
+ */
|
|
|
+ public function syncInfo(): int
|
|
|
+ {
|
|
|
+ $updated = 0;
|
|
|
+ foreach (CoreService::getExtensionComposerMap() as $extensionID => $composer) {
|
|
|
+ $packageName = $composer['name'] ?? null;
|
|
|
+ if (!is_string($packageName) || $packageName === '') {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ($this->upsertExtensionInfo($extensionID, $packageName)) {
|
|
|
+ $updated++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $updated;
|
|
|
+ }
|
|
|
|
|
|
+ private function upsertExtensionInfo(string $extensionID, string $packageName): bool
|
|
|
+ {
|
|
|
+ $data = $this->buildSyncData($extensionID, $packageName);
|
|
|
+ $extension = ExtensionModel::where(['id' => $extensionID])->findOrEmpty();
|
|
|
+ if ($extension->isEmpty()) {
|
|
|
+ $data['status'] = !empty($data['is_core']) ? 3 : 1; // 核心扩展默认启用,其余默认未安装
|
|
|
+ $extension->save($data);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ unset($data['id']);
|
|
|
+ $extension->save($data);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从包目录组装扩展最新信息,版本取 InstalledVersions::getPrettyVersion
|
|
|
+ */
|
|
|
+ private function buildSyncData(string $extensionID, string $packageName): array
|
|
|
+ {
|
|
|
+ $info = [];
|
|
|
+ $installPath = InstalledVersions::getInstallPath($packageName);
|
|
|
+ if ($installPath !== null && is_file($installPath . '/info.php')) {
|
|
|
+ $info = include $installPath . '/info.php';
|
|
|
+ if (!is_array($info)) {
|
|
|
+ $info = [];
|
|
|
+ }
|
|
|
+ } elseif ($installPath !== null && is_file($installPath . '/composer.json')) {
|
|
|
+ $composer = (new JsonFile($installPath . '/composer.json'))->read();
|
|
|
+ $info = [
|
|
|
+ 'id' => $extensionID,
|
|
|
+ 'name' => $composer['name'] ?? $extensionID,
|
|
|
+ 'description' => $composer['description'] ?? '',
|
|
|
+ ];
|
|
|
}
|
|
|
- ExtensionModel::where([
|
|
|
- ['id', '=', $id],
|
|
|
- ['version', '<>', $currentVersion],
|
|
|
- ])->update(['version' => $currentVersion]);
|
|
|
+ return [
|
|
|
+ 'id' => $extensionID,
|
|
|
+ 'name' => $info['name'] ?? $extensionID,
|
|
|
+ 'is_core' => (int)($info['is_core'] ?? 0),
|
|
|
+ 'category' => $info['category'] ?? '',
|
|
|
+ 'description' => $info['description'] ?? '',
|
|
|
+ 'version' => InstalledVersions::getPrettyVersion($packageName) ?? ($info['version'] ?? '1.0.0'),
|
|
|
+ 'core_version' => $info['core_version'] ?? '^1.0',
|
|
|
+ 'author' => $info['author'] ?? '未知',
|
|
|
+ 'email' => $info['email'] ?? '',
|
|
|
+ 'website' => $info['website'] ?? '',
|
|
|
+ 'image' => $info['image'] ?? '',
|
|
|
+ 'license' => $info['license'] ?? '',
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
|