ExtensionManager.php 11 KB

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