UpgradeService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\upgrade;
  12. use app\dict\addon\AddonDict;
  13. use app\model\addon\Addon;
  14. use app\service\admin\install\InstallSystemService;
  15. use app\service\admin\sys\ConfigService;
  16. use app\service\core\addon\CoreAddonCloudService;
  17. use app\service\core\addon\CoreAddonInstallService;
  18. use app\service\core\addon\CoreAddonService;
  19. use app\service\core\addon\CoreDependService;
  20. use app\service\core\addon\WapTrait;
  21. use app\service\core\channel\CoreH5Service;
  22. use app\service\core\menu\CoreMenuService;
  23. use app\service\core\niucloud\CoreModuleService;
  24. use app\service\core\schedule\CoreScheduleInstallService;
  25. use core\base\BaseAdminService;
  26. use core\exception\CommonException;
  27. use core\util\niucloud\BaseNiucloudClient;
  28. use think\facade\Cache;
  29. use think\facade\Db;
  30. /**
  31. * 框架及插件升级
  32. * @package app\service\core\upgrade
  33. */
  34. class UpgradeService extends BaseAdminService
  35. {
  36. use WapTrait;
  37. use ExecuteSqlTrait;
  38. protected $upgrade_dir;
  39. protected $root_path;
  40. protected $cache_key = 'upgrade';
  41. protected $upgrade_task = null;
  42. protected $addon = '';
  43. private $steps = [
  44. 'requestUpgrade' => ['step' => 'requestUpgrade', 'title' => '请求升级'],
  45. 'downloadFile' => ['step' => 'downloadFile', 'title' => '下载更新文件'],
  46. 'backupCode' => ['step' => 'backupCode', 'title' => '备份源码'],
  47. 'backupSql' => ['step' => 'backupSql', 'title' => '备份数据库'],
  48. 'coverCode' => ['step' => 'coverCode', 'title' => '合并更新文件'],
  49. 'handleUniapp' => ['step' => 'handleUniapp', 'title' => '处理uniapp'],
  50. 'refreshMenu' => ['step' => 'refreshMenu', 'title' => '刷新菜单'],
  51. 'installSchedule' => ['step' => 'installSchedule', 'title' => '安装计划任务'],
  52. 'upgradeComplete' => ['step' => 'upgradeComplete', 'title' => '升级完成']
  53. ];
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. $this->root_path = dirname(root_path()) . DIRECTORY_SEPARATOR;
  58. $this->upgrade_dir = $this->root_path . 'upgrade' . DIRECTORY_SEPARATOR;
  59. $this->upgrade_task = Cache::get($this->cache_key);
  60. }
  61. /**
  62. * 升级前环境检测
  63. * @param string $addon
  64. * @return void
  65. */
  66. public function upgradePreCheck(string $addon = '') {
  67. $niucloud_dir = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR;
  68. $admin_dir = $this->root_path . 'admin' . DIRECTORY_SEPARATOR;
  69. $web_dir = $this->root_path . 'web' . DIRECTORY_SEPARATOR;
  70. $wap_dir = $this->root_path . 'uni-app' . DIRECTORY_SEPARATOR;
  71. try {
  72. if (!is_dir($admin_dir)) throw new CommonException('ADMIN_DIR_NOT_EXIST');
  73. if (!is_dir($web_dir)) throw new CommonException('WEB_DIR_NOT_EXIST');
  74. if (!is_dir($wap_dir)) throw new CommonException('UNIAPP_DIR_NOT_EXIST');
  75. } catch (\Exception $e) {
  76. throw new CommonException($e->getMessage());
  77. }
  78. $data = [
  79. // 目录检测
  80. 'dir' => [
  81. // 要求可读权限
  82. 'is_readable' => [],
  83. // 要求可写权限
  84. 'is_write' => []
  85. ]
  86. ];
  87. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $niucloud_dir), 'status' => is_readable($niucloud_dir)];
  88. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $admin_dir), 'status' => is_readable($admin_dir)];
  89. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $web_dir), 'status' => is_readable($web_dir)];
  90. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $wap_dir), 'status' => is_readable($wap_dir)];
  91. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $niucloud_dir), 'status' => is_write($niucloud_dir) ];
  92. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $admin_dir), 'status' => is_write($admin_dir) ];
  93. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $web_dir), 'status' => is_write($web_dir) ];
  94. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $wap_dir), 'status' => is_write($wap_dir) ];
  95. $check_res = array_merge(
  96. array_column($data['dir']['is_readable'], 'status'),
  97. array_column($data['dir']['is_write'], 'status')
  98. );
  99. // 是否通过校验
  100. $data['is_pass'] = !in_array(false, $check_res);
  101. return $data;
  102. }
  103. /**
  104. * 升级
  105. * @param $addon
  106. * @return array
  107. */
  108. public function upgrade(string $addon = '') {
  109. if ($this->upgrade_task) throw new CommonException('UPGRADE_TASK_EXIST');
  110. $upgrade = [
  111. 'product_key' => BaseNiucloudClient::PRODUCT,
  112. 'framework_version' => config('version.version')
  113. ];
  114. if (!$addon) {
  115. $upgrade['app_key'] = AddonDict::FRAMEWORK_KEY;
  116. $upgrade['version'] = config('version.version');
  117. } else {
  118. $upgrade['app_key'] = $addon;
  119. $upgrade['version'] = (new Addon())->where([ ['key', '=', $addon] ])->value('version');
  120. }
  121. $response = (new CoreAddonCloudService())->upgradeAddon($upgrade);
  122. if (isset($response['code']) && $response['code'] == 0) throw new CommonException($response['msg']);
  123. try {
  124. $key = uniqid();
  125. $upgrade_dir = $this->upgrade_dir . $key . DIRECTORY_SEPARATOR;
  126. if (!is_dir($upgrade_dir)) {
  127. dir_mkdir($upgrade_dir);
  128. }
  129. $upgrade_tsak = [
  130. 'key' => $key,
  131. 'upgrade' => $upgrade,
  132. 'steps' => $this->steps,
  133. 'step' => 'requestUpgrade',
  134. 'executed' => ['requestUpgrade'],
  135. 'log' => [ $this->steps['requestUpgrade']['title'] ],
  136. 'params' => ['token' => $response['token'] ],
  137. 'upgrade_content' => $this->getUpgradeContent($addon)
  138. ];
  139. Cache::set($this->cache_key, $upgrade_tsak);
  140. return $upgrade_tsak;
  141. } catch (\Exception $e) {
  142. throw new CommonException($e->getMessage());
  143. }
  144. }
  145. /**
  146. * 执行升级
  147. * @return true
  148. */
  149. public function execute() {
  150. if (!$this->upgrade_task) return true;
  151. $steps = isset($this->upgrade_task['steps']) ? array_keys($this->upgrade_task['steps']) : array_keys($this->steps);
  152. $index = array_search($this->upgrade_task['step'], $steps);
  153. $step = $steps[ $index + 1 ] ?? '';
  154. $params = $this->upgrade_task['params'] ?? [];
  155. if ($step) {
  156. try {
  157. $res = $this->$step(...$params);
  158. if (is_array($res)) {
  159. $this->upgrade_task['params'] = $res;
  160. } else {
  161. $this->upgrade_task['step'] = $step;
  162. $this->upgrade_task['params'] = [];
  163. $this->upgrade_task['executed'][] = $step;
  164. $this->upgrade_task['log'][] = $this->steps[$step]['title'];
  165. }
  166. Cache::set($this->cache_key, $this->upgrade_task);
  167. } catch (\Exception $e) {
  168. $this->upgrade_task['step'] = $step;
  169. $this->upgrade_task['error'][] = '升级失败,失败原因:' . $e->getMessage().$e->getFile().$e->getLine();
  170. Cache::set($this->cache_key, $this->upgrade_task);
  171. $this->upgradeErrorHandle();
  172. }
  173. return true;
  174. } else {
  175. return true;
  176. }
  177. }
  178. /**
  179. * 下载升级文件
  180. * @param string $token
  181. * @param string $dir
  182. * @param int $index
  183. * @param $step
  184. * @return true|null
  185. */
  186. public function downloadFile(string $token, string $dir = '', int $index = -1, $step = 0, $length = 0) {
  187. if (!$dir) {
  188. $dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'download' . DIRECTORY_SEPARATOR;
  189. dir_mkdir($dir);
  190. }
  191. $res = (new CoreAddonCloudService())->downloadUpgradeFile($token, $dir, $index, $step, $length);
  192. return $res;
  193. }
  194. /**
  195. * 备份源码
  196. * @return true
  197. */
  198. public function backupCode() {
  199. (new BackupService())->backupCode();
  200. return true;
  201. }
  202. /**
  203. * 备份数据库
  204. * @return true
  205. */
  206. public function backupSql() {
  207. (new BackupService())->backupSql();
  208. return true;
  209. }
  210. /**
  211. * 覆盖更新升级的代码
  212. * @return void
  213. */
  214. public function coverCode($index = 0) {
  215. $this->upgrade_task['is_cover'] = 1;
  216. $addon = $this->upgrade_task['upgrade']['app_key'];
  217. $version_list = array_reverse($this->upgrade_task['upgrade_content']['version_list']);
  218. $code_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'download' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  219. $version_item = $version_list[$index];
  220. $version_no = $version_item['version_no'];
  221. $to_dir = $addon == AddonDict::FRAMEWORK_KEY ? rtrim($this->root_path, DIRECTORY_SEPARATOR) : $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'addon' . DIRECTORY_SEPARATOR . $addon;
  222. // 获取文件变更记录
  223. if (file_exists($code_dir . $version_no . '.txt')) {
  224. $change = array_filter(explode("\n", file_get_contents($code_dir . $version_no . '.txt')));
  225. foreach ($change as &$item) {
  226. list($operation, $md5, $file) = $item = explode(' ', $item);
  227. if ($operation == '-') {
  228. @unlink($to_dir . $file);
  229. }
  230. }
  231. // 合并依赖
  232. $this->installDepend($code_dir . $version_no, array_column($change, 2));
  233. }
  234. // 覆盖文件
  235. if (is_dir($code_dir . $version_no)) {
  236. // 忽略环境变量文件
  237. $exclude_files = ['.env.development', '.env.production', '.env', '.env.dev', '.env.product'];
  238. dir_copy($code_dir . $version_no, $to_dir, exclude_files:$exclude_files);
  239. if ($addon != AddonDict::FRAMEWORK_KEY) {
  240. (new CoreAddonInstallService($addon))->installDir();
  241. }
  242. }
  243. $upgrade_file_dir = 'v' . str_replace('.', '', $version_no);
  244. if ($addon == AddonDict::FRAMEWORK_KEY) {
  245. $class_path = "\\app\\upgrade\\{$upgrade_file_dir}\\Upgrade";
  246. $sql_file = root_path() . 'app' . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . $upgrade_file_dir . DIRECTORY_SEPARATOR . 'upgrade.sql';
  247. } else {
  248. $class_path = "\\addon\\{$addon}\\app\\upgrade\\{$upgrade_file_dir}\\Upgrade";
  249. $sql_file = root_path() . 'addon' . DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . $upgrade_file_dir . DIRECTORY_SEPARATOR . 'upgrade.sql';
  250. }
  251. // 执行升级sql
  252. if (file_exists($sql_file)) {
  253. $this->executeSql($sql_file);
  254. }
  255. // 执行升级方法
  256. if (class_exists($class_path)) {
  257. (new $class_path())->handle();
  258. }
  259. $index ++;
  260. if ($index < count($version_list)) {
  261. return compact('index');
  262. } else {
  263. return true;
  264. }
  265. }
  266. /**
  267. * 合并依赖
  268. * @param string $version_no
  269. * @return void
  270. */
  271. public function installDepend(string $dir, array $change_files) {
  272. $addon = $this->upgrade_task['upgrade']['app_key'];
  273. $depend_service = new CoreDependService();
  274. if ($addon == AddonDict::FRAMEWORK_KEY) {
  275. $composer = '/niucloud/composer.json';
  276. $admin_package = '/admin/package.json';
  277. $web_package = '/web/package.json';
  278. $uniapp_package = '/uni-app/package.json';
  279. } else {
  280. $composer = "/niucloud/addon/{$addon}/package/composer.json";
  281. $admin_package = "/niucloud/addon/{$addon}/package/admin-package.json";
  282. $web_package = "/niucloud/addon/{$addon}/package/web-package.json";
  283. $uniapp_package = "/niucloud/addon/{$addon}/package/uni-app-package.json";
  284. }
  285. if (in_array($composer, $change_files)) {
  286. $original = $depend_service->getComposerContent();
  287. $new = $depend_service->jsonFileToArray($dir . $composer);
  288. foreach ($new as $name => $value) {
  289. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  290. }
  291. $depend_service->writeArrayToJsonFile($original, $dir . $composer);
  292. }
  293. if (in_array($admin_package, $change_files)) {
  294. $original = $depend_service->getNpmContent('admin');
  295. $new = $depend_service->jsonFileToArray($dir . $admin_package);
  296. foreach ($new as $name => $value) {
  297. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  298. }
  299. $depend_service->writeArrayToJsonFile($original, $dir . $admin_package);
  300. }
  301. if (in_array($web_package, $change_files)) {
  302. $original = $depend_service->getNpmContent('web');
  303. $new = $depend_service->jsonFileToArray($dir . $web_package);
  304. foreach ($new as $name => $value) {
  305. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  306. }
  307. $depend_service->writeArrayToJsonFile($original, $dir . $web_package);
  308. }
  309. if (in_array($uniapp_package, $change_files)) {
  310. $original = $depend_service->getNpmContent('uni-app');
  311. $new = $depend_service->jsonFileToArray($dir . $uniapp_package);
  312. foreach ($new as $name => $value) {
  313. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  314. }
  315. $depend_service->writeArrayToJsonFile($original, $dir . $uniapp_package);
  316. }
  317. }
  318. /**
  319. * 处理手机端
  320. * @param string $verson_no
  321. * @return true
  322. */
  323. public function handleUniapp() {
  324. $code_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'download' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  325. $exclude_files = ['.env.development', '.env.production', 'manifest.json'];
  326. dir_copy($code_dir . 'uni-app', $this->root_path . 'uni-app', exclude_files:$exclude_files);
  327. $addon_list = (new CoreAddonService())->getInstallAddonList();
  328. $depend_service = new CoreDependService();
  329. if (!empty($addon_list)) {
  330. foreach ($addon_list as $addon => $item) {
  331. $this->addon = $addon;
  332. // 编译 diy-group 自定义组件代码文件
  333. $this->compileDiyComponentsCode($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $addon);
  334. // 编译 pages.json 页面路由代码文件
  335. $this->installPageCode($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR);
  336. // 编译 加载插件标题语言包
  337. $this->compileLocale($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $addon);
  338. // 合并插件依赖
  339. $addon_uniapp_package = str_replace('/', DIRECTORY_SEPARATOR, project_path() . "niucloud/addon/{$addon}/package/uni-app-package.json");
  340. if (file_exists($addon_uniapp_package)) {
  341. $original = $depend_service->getNpmContent('uni-app');
  342. $new = $depend_service->jsonFileToArray($addon_uniapp_package);
  343. foreach ($new as $name => $value) {
  344. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  345. }
  346. $uniapp_package = $this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'package.json';
  347. $depend_service->writeArrayToJsonFile($original, $uniapp_package);
  348. }
  349. }
  350. }
  351. $map = (new ConfigService())->getMap();
  352. ( new CoreH5Service() )->mapKeyChange($map[ 'key' ]);
  353. return true;
  354. }
  355. /**
  356. * 执行升级sql
  357. * @param string $sql_file
  358. * @return true
  359. */
  360. private function executeSql(string $sql_file) {
  361. $sql_content = file_get_contents($sql_file);
  362. if (!empty($sql_content)) {
  363. $prefix = config('database.connections.mysql.prefix');
  364. $sql_data = array_filter($this->getSqlQuery($sql_content));
  365. if (!empty($sql_data)) {
  366. foreach ($sql_data as $sql) {
  367. $sql = $prefix ? $this->handleSqlPrefix($sql, $prefix) : $sql;
  368. Db::query($sql);
  369. }
  370. }
  371. }
  372. return true;
  373. }
  374. /**
  375. * 刷新菜单
  376. * @return void
  377. */
  378. public function refreshMenu() {
  379. if ($this->upgrade_task['upgrade']['app_key'] == AddonDict::FRAMEWORK_KEY) {
  380. (new InstallSystemService())->installMenu();
  381. } else {
  382. (new CoreMenuService())->refreshAddonMenu($this->upgrade_task['upgrade']['app_key']);
  383. }
  384. return true;
  385. }
  386. /**
  387. * 安装计划任务
  388. * @return true
  389. */
  390. public function installSchedule() {
  391. if ($this->upgrade_task['upgrade']['app_key'] == AddonDict::FRAMEWORK_KEY) {
  392. (new CoreScheduleInstallService())->installSystemSchedule();
  393. } else {
  394. (new CoreScheduleInstallService())->installAddonSchedule($this->upgrade_task['upgrade']['app_key']);
  395. }
  396. return true;
  397. }
  398. /**
  399. * 更新完成
  400. * @return void
  401. */
  402. public function upgradeComplete() {
  403. $addon = $this->upgrade_task['upgrade']['app_key'];
  404. if ($addon != AddonDict::FRAMEWORK_KEY) {
  405. $core_addon_service = new CoreAddonService();
  406. $install_data = $core_addon_service->getAddonConfig($addon);
  407. $install_data['icon'] = 'addon/' . $addon . '/icon.png';
  408. $core_addon_service->set($install_data);
  409. }
  410. $this->clearUpgradeTask(5);
  411. return true;
  412. }
  413. /**
  414. * 升级出错之后的处理
  415. * @return true|void
  416. */
  417. public function upgradeErrorHandle() {
  418. $steps = [];
  419. $steps[$this->upgrade_task['step']] = [];
  420. if (isset($this->upgrade_task['is_cover'])) {
  421. $steps['restoreCode'] = ['step' => 'restoreCode', 'title' => '恢复源码备份'];
  422. $steps['restoreSql'] = ['step' => 'restoreSql', 'title' => '恢复数据库备份'];
  423. }
  424. $steps['restoreComplete'] = ['step' => 'restoreComplete', 'title' => '备份恢复完成'];
  425. $this->upgrade_task['steps'] = $steps;
  426. Cache::set($this->cache_key, $this->upgrade_task);
  427. }
  428. /**
  429. * 恢复源码
  430. * @return void
  431. */
  432. public function restoreCode() {
  433. try {
  434. (new RestoreService())->restoreCode();
  435. return true;
  436. } catch (\Exception $e) {
  437. $this->upgrade_task['error'][] = '源码备份恢复失败稍后请手动恢复,失败原因:' . $e->getMessage().$e->getFile().$e->getLine();
  438. Cache::set($this->cache_key, $this->upgrade_task);
  439. return true;
  440. }
  441. }
  442. /**
  443. * 恢复数据库
  444. * @return void
  445. */
  446. public function restoreSql() {
  447. try {
  448. (new RestoreService())->restoreSql();
  449. return true;
  450. } catch (\Exception $e) {
  451. $this->upgrade_task['error'][] = '数据库备份恢复失败稍后请手动恢复,失败原因:' . $e->getMessage().$e->getFile().$e->getLine();
  452. Cache::set($this->cache_key, $this->upgrade_task);
  453. return true;
  454. }
  455. }
  456. public function restoreComplete() {
  457. $this->clearUpgradeTask(5);
  458. return true;
  459. }
  460. /**
  461. * 获取升级内容
  462. * @param string $addon
  463. * @return array|\core\util\niucloud\Response|object|\Psr\Http\Message\ResponseInterface
  464. * @throws \GuzzleHttp\Exception\GuzzleException
  465. */
  466. public function getUpgradeContent(string $addon = '') {
  467. $upgrade = [
  468. 'product_key' => BaseNiucloudClient::PRODUCT
  469. ];
  470. if (!$addon) {
  471. $upgrade['app_key'] = AddonDict::FRAMEWORK_KEY;
  472. $upgrade['version'] = config('version.version');
  473. } else {
  474. $upgrade['app_key'] = $addon;
  475. $upgrade['version'] = (new Addon())->where([ ['key', '=', $addon] ])->value('version');
  476. }
  477. return (new CoreModuleService())->getUpgradeContent($upgrade)['data'] ?? [];
  478. }
  479. /**
  480. * 获取正在进行的升级任务
  481. * @return mixed|null
  482. */
  483. public function getUpgradeTask() {
  484. return $this->upgrade_task;
  485. }
  486. /**
  487. * 清除升级任务
  488. * @return true
  489. */
  490. public function clearUpgradeTask(int $delayed = 0) {
  491. if ($delayed) {
  492. Cache::set($this->cache_key, $this->upgrade_task, $delayed);
  493. } else {
  494. Cache::set($this->cache_key, null);
  495. }
  496. return true;
  497. }
  498. /**
  499. * 获取插件定义的package目录
  500. * @param string $addon
  501. * @return string
  502. */
  503. public function geAddonPackagePath(string $addon)
  504. {
  505. return root_path() . 'addon' .DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . 'package' . DIRECTORY_SEPARATOR;
  506. }
  507. }