| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- declare(strict_types=1);
- namespace SixShop\System;
- use Composer\InstalledVersions;
- use RuntimeException;
- use SixShop\Core\Contracts\ExtensionInterface;
- use SixShop\Core\Helper;
- use SixShop\Core\Service\CoreService;
- use SixShop\Payment\Contracts\PaymentExtensionInterface;
- use SixShop\System\Config\ExtensionConfig;
- use SixShop\System\Enum\ExtensionStatusEnum;
- use SixShop\System\Model\ExtensionConfigModel;
- use SixShop\System\Model\ExtensionModel;
- use think\db\Query;
- use think\exception\ValidateException;
- use think\facade\Db;
- use think\facade\Event;
- use think\facade\Log;
- use think\facade\Validate;
- use think\Service;
- class ExtensionManager extends Service
- {
- /**
- * @var array 扩展列表
- */
- private array $extensionList = [];
- /**
- * @var array 分类列表
- */
- private array $categoryMap = [];
- /**
- * 安装扩展
- */
- public function install(string $extensionID): void
- {
- $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
- if ($extensionModel->status === ExtensionStatusEnum::INSTALLED) {
- throw new RuntimeException("{$extensionID}扩展已安装");
- }
- $this->app->make(Migrate::class, [$this->app, $extensionID])->install();
- $extension = $this->getExtension($extensionID);
- $extension->install();
- $config = $this->getExtensionConfig($extensionID);
- if (empty($config)) {
- $updateData = [];
- $formConfig = $extension->getConfig();
- foreach ($formConfig as $item) {
- if (isset($item['value'])) {
- $updateData[$item['field']] = $item['value'];
- }
- }
- if (!empty($updateData)) {
- $this->saveConfig($extensionID, $updateData);
- }
- }
- $extensionModel->status = ExtensionStatusEnum::INSTALLED;
- $extensionModel->save();
- }
- public function getExtension(string $extensionID): ExtensionInterface|PaymentExtensionInterface
- {
- return $this->app->get('extension.' . $extensionID);
- }
- public function getExtensionConfig(string $extensionID, string $key = '', bool $onlyValue = true): mixed
- {
- $extensionConfig = ExtensionConfigModel::where('extension_id', $extensionID)->when($key, function (Query $query) use ($key) {
- $query->where('key', $key);
- })->column(['value', 'type',], 'key', true);
- if (count($extensionConfig) === 0) {
- return $key ? null : [];
- }
- if ($onlyValue) {
- $extensionConfig = array_map(fn($item) => $item['value'], $extensionConfig);
- }
- return $key != '' ? $extensionConfig[$key] : $extensionConfig;
- }
- public function saveConfig(string $extensionID, array $data): bool
- {
- $config = array_merge(ExtensionConfig::BASE, $this->getExtension($extensionID)->getConfig());
- $updateData = [];
- foreach ($config as $item) {
- if (isset($item['field'])) {
- if (isset($data[$item['field']])) {
- $updateData[] = [
- 'extension_id' => $extensionID,
- 'key' => $item['field'],
- 'value' => $data[$item['field']],
- 'type' => $item['type'],
- 'title' => $item['title']
- ];
- }
- } else {
- if (isset($item['children'])) {
- foreach ($item['children'] as $childItem) {
- if (isset($childItem['field'], $data[$childItem['field']])) {
- $updateData[] = [
- 'extension_id' => $extensionID,
- 'key' => $childItem['field'],
- 'value' => $data[$childItem['field']],
- 'type' => $childItem['type'],
- 'title' => $childItem['title']
- ];
- }
- if (isset($childItem['children'])) {
- foreach ($childItem['children'] as $grandChildItem) {
- if (isset($grandChildItem['field'], $data[$grandChildItem['field']])) {
- $updateData[] = [
- 'extension_id' => $extensionID,
- 'key' => $grandChildItem['field'],
- 'value' => $data[$grandChildItem['field']],
- 'type' => $grandChildItem['type'],
- 'title' => $grandChildItem['title'],
- ];
- }
- }
- }
- }
- }
- }
- }
- if (!empty($updateData)) {
- Db::transaction(function () use ($updateData) {
- foreach ($updateData as $item) {
- $configModel = ExtensionConfigModel::where([
- 'extension_id' => $item['extension_id'],
- 'key' => $item['key']
- ])->findOrEmpty();
- $configModel->save($item);
- Event::trigger('after_write_extension_config:' . $item['extension_id'] . ':' . $item['key'], $item);
- }
- Event::trigger('after_write_extension_config:' . $item['extension_id'], array_column($updateData, null, 'key'));
- });
- }
- return true;
- }
- /**
- * 卸载扩展
- */
- public function uninstall(string $extensionID): void
- {
- $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
- if ($extensionModel->status === ExtensionStatusEnum::UNINSTALLED) {
- throw new RuntimeException("{$extensionID}扩展未安装");
- }
- $this->app->make(Migrate::class, [$this->app, $extensionID])->uninstall();
- $this->getExtension($extensionID)->uninstall();
- $extensionModel->status = ExtensionStatusEnum::UNINSTALLED;
- $extensionModel->save();
- }
- /**
- * 启用扩展
- */
- public function enable(string $extensionID): void
- {
- $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
- match ($extensionModel->status) {
- ExtensionStatusEnum::UNINSTALLED => throw new RuntimeException("{$extensionID}扩展未安装"),
- ExtensionStatusEnum::ENABLED => throw new RuntimeException("{$extensionID}扩展已启用"),
- default => null,
- };
- $extensionModel->status = ExtensionStatusEnum::ENABLED;
- $extensionModel->save();
- }
- /**
- * 禁用扩展
- */
- public function disable(string $extensionID): void
- {
- $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
- if ($extensionModel->status != ExtensionStatusEnum::ENABLED) {
- throw new RuntimeException("{$extensionID}扩展未启用");
- }
- $extensionModel->status = ExtensionStatusEnum::DISABLED;
- $extensionModel->save();
- }
- /**
- * 获取扩展信息
- */
- public function getInfo(string $name): ExtensionModel
- {
- return $this->extensionList[$name] ?? ($this->extensionList[$name] = $this->app->cache->remember(
- sprintf(ExtensionModel::EXTENSION_INFO_CACHE_KEY, $name),
- function () use ($name) {
- return $this->initExtensionInfo($name);
- }));
- }
- private function initExtensionInfo(string $name): ExtensionModel
- {
- $categoryMap = $this->getCategoryMap();
- $extensionInfo = $this->getExtension($name)->getInfo();
- try {
- Validate::rule([
- 'id' => 'require|max:50',
- 'name' => 'require|max:100',
- 'is_core' => 'in:0,1',
- 'category' => 'in:' . implode(',', array_keys($categoryMap)),
- 'description' => 'max:65535',
- 'version' => 'max:20',
- 'core_version' => 'max:20',
- 'author' => 'require|max:100',
- 'email' => 'email|max:100',
- 'website' => 'url|max:255',
- 'image' => 'url|max:255',
- 'license' => 'max:50',
- ])->failException()->check($extensionInfo);
- } catch (ValidateException $exception) {
- Log::warning('module(' . $name . ') info error:' . $exception->getError());
- }
- if (!isset($extensionInfo['id']) || $extensionInfo['id'] !== $name) {
- throw new RuntimeException("{$name}扩展id({$extensionInfo['id']})与目录名不一致");
- }
- if (!isset($extensionInfo['version'])) {
- $extensionInfo['version'] = '1.0.0';
- }
- if (!isset($extensionInfo['core_version'])) {
- $extensionInfo['core_version'] = '^1.0';
- }
- $extension = ExtensionModel::where(['id' => $name])->append(['status_text'])->findOrEmpty();
- if ($extension->isEmpty()) {
- $extensionInfo['status'] = 1; // 下载的扩展默认未安装
- if (isset($extensionInfo['is_core']) && $extensionInfo['is_core'] == 1) {
- $extensionInfo['status'] = 3; // 核心扩展默认启用
- }
- $extension->save($extensionInfo);
- }
- $extension['category_text'] = $categoryMap[$extension['category']] ?? '未知';
- return $this->extensionList[$name] = $extension;
- }
- /**
- * @return array
- */
- public function getCategoryMap(): array
- {
- if (empty($this->categoryMap)) {
- $this->categoryMap = array_to_map($this->getExtensionConfig('system', 'category'), 'code', 'text');
- }
- return $this->categoryMap;
- }
- public function getExtensionList(): array
- {
- foreach (Helper::extension_name_list() as $name) {
- $this->app->cache->set(sprintf(ExtensionModel::EXTENSION_INFO_CACHE_KEY, $name), $this->initExtensionInfo($name));
- }
- return $this->extensionList;
- }
- public function getExtensionConfigForm(string $extensionID): array
- {
- $config = array_merge(ExtensionConfig::BASE, array_values($this->getExtension($extensionID)->getConfig()));
- $extensionConfig = ExtensionConfigModel::where('extension_id', $extensionID)->column(['value', 'type',], 'key', true);
- foreach ($config as $key => &$item) {
- if (isset($item['field'])) {
- if (isset($extensionConfig[$item['field']])) {
- $config[$key]['value'] = $extensionConfig[$item['field']]['value'];
- }
- } else {
- if (isset($item['children'])) {
- foreach ($item['children'] as $childKey => &$childItem) {
- if (isset($childItem['field'], $extensionConfig[$childItem['field']])) {
- $config[$key]['children'][$childKey]['value'] = $extensionConfig[$childItem['field']]['value'];
- }
- if (isset($childItem['children'])) {
- foreach ($childItem['children'] as $grandChildKey => $grandChildItem) {
- if (isset($grandChildItem['field'], $extensionConfig[$grandChildItem['field']])) {
- $config[$key]['children'][$childKey]['children'][$grandChildKey]['value'] = $extensionConfig[$grandChildItem['field']]['value'];
- }
- }
- }
- }
- }
- }
- }
- Event::trigger('after_read_extension_config', [$config, $extensionID]);
- return $config;
- }
- public function migrations(string $id)
- {
- return app(Migrate::class, [$this->app, $id])->getMigrationList();
- }
- public function refresh(string $id): void
- {
- // 获取当前版本更新版本信息
- $currentVersion = '0.0.0';
- if (isset(CoreService::$extensionComposerMap[$id])) {
- $packageName = CoreService::$extensionComposerMap[$id]['name'];
- $currentVersion = InstalledVersions::getPrettyVersion($packageName);
- }
- ExtensionModel::where([
- ['id', '=', $id],
- ['version', '<>', $currentVersion],
- ])->update(['version' => $currentVersion]);
- }
- }
|