ExtensionManager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\System;
  4. use Composer\InstalledVersions;
  5. use RuntimeException;
  6. use SixShop\Core\Contracts\ExtensionInterface;
  7. use SixShop\Core\Helper;
  8. use SixShop\Core\Service\CoreService;
  9. use SixShop\Payment\Contracts\PaymentExtensionInterface;
  10. use SixShop\System\Config\ExtensionConfig;
  11. use SixShop\System\Enum\ExtensionStatusEnum;
  12. use SixShop\System\Model\ExtensionConfigModel;
  13. use SixShop\System\Model\ExtensionModel;
  14. use think\db\Query;
  15. use think\exception\ValidateException;
  16. use think\facade\Db;
  17. use think\facade\Event;
  18. use think\facade\Log;
  19. use think\facade\Validate;
  20. use think\Service;
  21. class ExtensionManager extends Service
  22. {
  23. /**
  24. * @var array 扩展列表
  25. */
  26. private array $extensionList = [];
  27. /**
  28. * @var array 分类列表
  29. */
  30. private array $categoryMap = [];
  31. /**
  32. * 安装扩展
  33. */
  34. public function install(string $extensionID): void
  35. {
  36. $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
  37. if ($extensionModel->status === ExtensionStatusEnum::INSTALLED) {
  38. throw new RuntimeException("{$extensionID}扩展已安装");
  39. }
  40. $this->app->make(Migrate::class, [$this->app, $extensionID])->install();
  41. $extension = $this->getExtension($extensionID);
  42. $extension->install();
  43. $config = $this->getExtensionConfig($extensionID);
  44. if (empty($config)) {
  45. $updateData = [];
  46. $formConfig = $extension->getConfig();
  47. foreach ($formConfig as $item) {
  48. if (isset($item['value'])) {
  49. $updateData[$item['field']] = $item['value'];
  50. }
  51. }
  52. if (!empty($updateData)) {
  53. $this->saveConfig($extensionID, $updateData);
  54. }
  55. }
  56. $extensionModel->status = ExtensionStatusEnum::INSTALLED;
  57. $extensionModel->save();
  58. }
  59. public function getExtension(string $extensionID): ExtensionInterface|PaymentExtensionInterface
  60. {
  61. return $this->app->get('extension.' . $extensionID);
  62. }
  63. public function getExtensionConfig(string $extensionID, string $key = '', bool $onlyValue = true): mixed
  64. {
  65. $extensionConfig = ExtensionConfigModel::where('extension_id', $extensionID)->when($key, function (Query $query) use ($key) {
  66. $query->where('key', $key);
  67. })->column(['value', 'type',], 'key', true);
  68. if (count($extensionConfig) === 0) {
  69. return $key ? null : [];
  70. }
  71. if ($onlyValue) {
  72. $extensionConfig = array_map(fn($item) => $item['value'], $extensionConfig);
  73. }
  74. return $key != '' ? $extensionConfig[$key] : $extensionConfig;
  75. }
  76. public function saveConfig(string $extensionID, array $data): bool
  77. {
  78. $config = array_merge(ExtensionConfig::BASE, $this->getExtension($extensionID)->getConfig());
  79. $updateData = [];
  80. foreach ($config as $item) {
  81. if (isset($item['field'])) {
  82. if (isset($data[$item['field']])) {
  83. $updateData[] = [
  84. 'extension_id' => $extensionID,
  85. 'key' => $item['field'],
  86. 'value' => $data[$item['field']],
  87. 'type' => $item['type'],
  88. 'title' => $item['title']
  89. ];
  90. }
  91. } else {
  92. if (isset($item['children'])) {
  93. foreach ($item['children'] as $childItem) {
  94. if (isset($childItem['field'], $data[$childItem['field']])) {
  95. $updateData[] = [
  96. 'extension_id' => $extensionID,
  97. 'key' => $childItem['field'],
  98. 'value' => $data[$childItem['field']],
  99. 'type' => $childItem['type'],
  100. 'title' => $childItem['title']
  101. ];
  102. }
  103. if (isset($childItem['children'])) {
  104. foreach ($childItem['children'] as $grandChildItem) {
  105. if (isset($grandChildItem['field'], $data[$grandChildItem['field']])) {
  106. $updateData[] = [
  107. 'extension_id' => $extensionID,
  108. 'key' => $grandChildItem['field'],
  109. 'value' => $data[$grandChildItem['field']],
  110. 'type' => $grandChildItem['type'],
  111. 'title' => $grandChildItem['title'],
  112. ];
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. if (!empty($updateData)) {
  121. Db::transaction(function () use ($updateData) {
  122. foreach ($updateData as $item) {
  123. $configModel = ExtensionConfigModel::where([
  124. 'extension_id' => $item['extension_id'],
  125. 'key' => $item['key']
  126. ])->findOrEmpty();
  127. $configModel->save($item);
  128. Event::trigger('after_write_extension_config:' . $item['extension_id'] . ':' . $item['key'], $item);
  129. }
  130. Event::trigger('after_write_extension_config:' . $item['extension_id'], array_column($updateData, null, 'key'));
  131. });
  132. }
  133. return true;
  134. }
  135. /**
  136. * 卸载扩展
  137. */
  138. public function uninstall(string $extensionID): void
  139. {
  140. $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
  141. if ($extensionModel->status === ExtensionStatusEnum::UNINSTALLED) {
  142. throw new RuntimeException("{$extensionID}扩展未安装");
  143. }
  144. $this->app->make(Migrate::class, [$this->app, $extensionID])->uninstall();
  145. $this->getExtension($extensionID)->uninstall();
  146. $extensionModel->status = ExtensionStatusEnum::UNINSTALLED;
  147. $extensionModel->save();
  148. }
  149. /**
  150. * 启用扩展
  151. */
  152. public function enable(string $extensionID): void
  153. {
  154. $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
  155. match ($extensionModel->status) {
  156. ExtensionStatusEnum::UNINSTALLED => throw new RuntimeException("{$extensionID}扩展未安装"),
  157. ExtensionStatusEnum::ENABLED => throw new RuntimeException("{$extensionID}扩展已启用"),
  158. default => null,
  159. };
  160. $extensionModel->status = ExtensionStatusEnum::ENABLED;
  161. $extensionModel->save();
  162. }
  163. /**
  164. * 禁用扩展
  165. */
  166. public function disable(string $extensionID): void
  167. {
  168. $extensionModel = ExtensionModel::where(['id' => $extensionID])->findOrFail();
  169. if ($extensionModel->status != ExtensionStatusEnum::ENABLED) {
  170. throw new RuntimeException("{$extensionID}扩展未启用");
  171. }
  172. $extensionModel->status = ExtensionStatusEnum::DISABLED;
  173. $extensionModel->save();
  174. }
  175. /**
  176. * 获取扩展信息
  177. */
  178. public function getInfo(string $name): ExtensionModel
  179. {
  180. return $this->extensionList[$name] ?? ($this->extensionList[$name] = $this->app->cache->remember(
  181. sprintf(ExtensionModel::EXTENSION_INFO_CACHE_KEY, $name),
  182. function () use ($name) {
  183. return $this->initExtensionInfo($name);
  184. }));
  185. }
  186. private function initExtensionInfo(string $name): ExtensionModel
  187. {
  188. $categoryMap = $this->getCategoryMap();
  189. $extensionInfo = $this->getExtension($name)->getInfo();
  190. try {
  191. Validate::rule([
  192. 'id' => 'require|max:50',
  193. 'name' => 'require|max:100',
  194. 'is_core' => 'in:0,1',
  195. 'category' => 'in:' . implode(',', array_keys($categoryMap)),
  196. 'description' => 'max:65535',
  197. 'version' => 'max:20',
  198. 'core_version' => 'max:20',
  199. 'author' => 'require|max:100',
  200. 'email' => 'email|max:100',
  201. 'website' => 'url|max:255',
  202. 'image' => 'url|max:255',
  203. 'license' => 'max:50',
  204. ])->failException()->check($extensionInfo);
  205. } catch (ValidateException $exception) {
  206. Log::warning('module(' . $name . ') info error:' . $exception->getError());
  207. }
  208. if (!isset($extensionInfo['id']) || $extensionInfo['id'] !== $name) {
  209. throw new RuntimeException("{$name}扩展id({$extensionInfo['id']})与目录名不一致");
  210. }
  211. if (!isset($extensionInfo['version'])) {
  212. $extensionInfo['version'] = '1.0.0';
  213. }
  214. if (!isset($extensionInfo['core_version'])) {
  215. $extensionInfo['core_version'] = '^1.0';
  216. }
  217. $extension = ExtensionModel::where(['id' => $name])->append(['status_text'])->findOrEmpty();
  218. if ($extension->isEmpty()) {
  219. $extensionInfo['status'] = 1; // 下载的扩展默认未安装
  220. if (isset($extensionInfo['is_core']) && $extensionInfo['is_core'] == 1) {
  221. $extensionInfo['status'] = 3; // 核心扩展默认启用
  222. }
  223. $extension->save($extensionInfo);
  224. }
  225. $extension['category_text'] = $categoryMap[$extension['category']] ?? '未知';
  226. return $this->extensionList[$name] = $extension;
  227. }
  228. /**
  229. * @return array
  230. */
  231. public function getCategoryMap(): array
  232. {
  233. if (empty($this->categoryMap)) {
  234. $this->categoryMap = array_to_map($this->getExtensionConfig('system', 'category'), 'code', 'text');
  235. }
  236. return $this->categoryMap;
  237. }
  238. public function getExtensionList(): array
  239. {
  240. foreach (Helper::extension_name_list() as $name) {
  241. $this->app->cache->set(sprintf(ExtensionModel::EXTENSION_INFO_CACHE_KEY, $name), $this->initExtensionInfo($name));
  242. }
  243. return $this->extensionList;
  244. }
  245. public function getExtensionConfigForm(string $extensionID): array
  246. {
  247. $config = array_merge(ExtensionConfig::BASE, array_values($this->getExtension($extensionID)->getConfig()));
  248. $extensionConfig = ExtensionConfigModel::where('extension_id', $extensionID)->column(['value', 'type',], 'key', true);
  249. foreach ($config as $key => &$item) {
  250. if (isset($item['field'])) {
  251. if (isset($extensionConfig[$item['field']])) {
  252. $config[$key]['value'] = $extensionConfig[$item['field']]['value'];
  253. }
  254. } else {
  255. if (isset($item['children'])) {
  256. foreach ($item['children'] as $childKey => &$childItem) {
  257. if (isset($childItem['field'], $extensionConfig[$childItem['field']])) {
  258. $config[$key]['children'][$childKey]['value'] = $extensionConfig[$childItem['field']]['value'];
  259. }
  260. if (isset($childItem['children'])) {
  261. foreach ($childItem['children'] as $grandChildKey => $grandChildItem) {
  262. if (isset($grandChildItem['field'], $extensionConfig[$grandChildItem['field']])) {
  263. $config[$key]['children'][$childKey]['children'][$grandChildKey]['value'] = $extensionConfig[$grandChildItem['field']]['value'];
  264. }
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. Event::trigger('after_read_extension_config', [$config, $extensionID]);
  272. return $config;
  273. }
  274. public function migrations(string $id)
  275. {
  276. return app(Migrate::class, [$this->app, $id])->getMigrationList();
  277. }
  278. public function refresh(string $id): void
  279. {
  280. // 获取当前版本更新版本信息
  281. $currentVersion = '0.0.0';
  282. if (isset(CoreService::$extensionComposerMap[$id])) {
  283. $packageName = CoreService::$extensionComposerMap[$id]['name'];
  284. $currentVersion = InstalledVersions::getPrettyVersion($packageName);
  285. }
  286. ExtensionModel::where([
  287. ['id', '=', $id],
  288. ['version', '<>', $currentVersion],
  289. ])->update(['version' => $currentVersion]);
  290. }
  291. }