VersionUpdate.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\command;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Option;
  15. use think\console\Output;
  16. use think\Exception;
  17. use think\facade\Db;
  18. class VersionUpdate extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('version:update')
  23. ->setDescription('crmeb_merchant 版本更新命令')
  24. ->addOption('package', 'p', Option::VALUE_REQUIRED, '指定更新包的路径');
  25. }
  26. protected function execute(Input $input, Output $output)
  27. {
  28. $flag = $output->confirm($input, '更新之前请务必做好数据库和代码的备份,防止数据或代码在更新中被覆盖 !!!', false);
  29. if (!$flag) return;
  30. $flag = $output->confirm($input, '请确保swoole服务和队列服务已关闭,防止意外报错', false);
  31. if (!$flag) return;
  32. $version = str_replace('CRMEB-MERCHANT-v', '', get_crmeb_version('no'));
  33. ini_set('memory_limit', '-1');
  34. set_time_limit(0);
  35. $packagePath = $input->getOption('package') ?: 'auto_update.zip';
  36. $updateFilePath = app()->getRootPath() . ltrim($packagePath, '/= ');
  37. $updatePath = dirname($updateFilePath);
  38. $unzipPath = $updatePath . '/_update_runtime_' . str_replace('.', '_', $version);
  39. if (!is_file($updateFilePath)) {
  40. $output->warning($updateFilePath . ' 更新包不存在');
  41. return;
  42. }
  43. $zip = new \ZipArchive();
  44. if ($zip->open($updateFilePath) === true) {
  45. $zip->extractTo($unzipPath);
  46. $zip->close();
  47. } else {
  48. $output->warning($updateFilePath . ' 更新包打开失败');
  49. return;
  50. }
  51. $unlink = function () use ($unzipPath) {
  52. @unlink($unzipPath . '/update.sql');
  53. @unlink($unzipPath . '/update.zip');
  54. @unlink($unzipPath . '/.env');
  55. @rmdir($unzipPath);
  56. };
  57. if (!is_file($unzipPath . '/.env')) {
  58. $output->warning('文件不完整');
  59. $unlink();
  60. return;
  61. }
  62. $env = parse_ini_file($unzipPath . '/.env', true) ?: [];
  63. if (($env['NAME'] ?? '') !== 'CRMEB-MERCHANT' || ((($env['TYPE'] ?? '') === 'MODEL' && ($env['VERSION'] ?? '') !== $version) || (($env['OLD_VERSION'] ?? '') && ($env['OLD_VERSION'] ?? '') !== $version))) {
  64. $output->warning('版本号对比失败,请检查当前版本号(.version/更新包)是否被修改');
  65. $unlink();
  66. return;
  67. }
  68. if (is_file($unzipPath . '/update.sql')) {
  69. $str = preg_replace('/--.*/i', '', file_get_contents($unzipPath . '/update.sql'));
  70. $str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
  71. $sqlList = explode(";\n", $str);
  72. } else {
  73. $sqlList = [];
  74. }
  75. $output->info('开始更新');
  76. try {
  77. Db::transaction(function () use ($output, $unzipPath, $sqlList) {
  78. foreach ($sqlList as $sql) {
  79. $sql = trim($sql, " \xEF\xBB\xBF\r\n");
  80. if (!$sql) continue;
  81. Db::query($sql . ';');
  82. }
  83. if (count($sqlList)) {
  84. $output->info('数据库更新成功');
  85. }
  86. $zip = new \ZipArchive();
  87. if ($zip->open($unzipPath . '/update.zip') === true) {
  88. $zip->extractTo(app()->getRootPath());
  89. $zip->close();
  90. } else {
  91. throw new Exception('更新文件覆盖失败');
  92. }
  93. });
  94. } catch (\Throwable $e) {
  95. $output->warning('更新失败:' . $e->getMessage());
  96. $unlink();
  97. return;
  98. }
  99. $unlink();
  100. $output->info('版本更新成功, 请重启swoole服务和队列服务');
  101. update_crmeb_compiled();
  102. }
  103. }