Upgrade2.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. ini_set('max_execution_time', 0);
  3. ini_set('memory_limit', '500M');
  4. class Upgrade2
  5. {
  6. protected string $archiveFile = "";//升级文件地址
  7. protected string $extractPath = "";//解压目录地址
  8. protected string $root_path = "";//程序根目录
  9. public string $update_download_url = "";//升级zip文件下载地址
  10. public string $update_sql_url = "";//升级sql脚本文件地址
  11. public string $update_script = "";//升级后执行的脚本地址
  12. //构造方法初始化一些数据
  13. function __construct($update_download_url = null, $update_sql_url = null, $update_script = null)
  14. {
  15. $this->archiveFile = runtime_path() . 'mtab.zip';
  16. $this->extractPath = runtime_path();
  17. $this->root_path = root_path();
  18. if ($update_download_url !== null) {
  19. $this->update_download_url = $update_download_url;
  20. }
  21. if ($update_sql_url !== null) {
  22. $this->update_sql_url = $update_sql_url;
  23. }
  24. if ($update_script !== null) {
  25. $this->update_script = $update_script;
  26. }
  27. }
  28. //运行入口
  29. function run()
  30. {
  31. return $this->startUpgrade();
  32. }
  33. //新的进程启动升级
  34. private function startUpgrade()
  35. {
  36. //如果有程序代码的更新资源则更新程序代码
  37. if (strlen($this->update_download_url) > 1) {
  38. //如果有遗留的解压资源则删除
  39. $this->deleteDirectory("{$this->extractPath}mtab");
  40. //如果存在旧的升级包则删除
  41. $this->delZip();
  42. //下载远程更新包
  43. if(!$this->fileDownload()){
  44. return "资源下载失败";
  45. }
  46. //解压升级包
  47. if (!$this->unzip($this->archiveFile, $this->extractPath)) {
  48. $this->delZip();
  49. return '升级资源包解压失败';
  50. }
  51. //拷贝覆盖
  52. $this->copy();
  53. //删除下载的更新包
  54. $this->delZip();
  55. //更新完后的一些操作
  56. }
  57. //如果有数据库的更新资源则更新程序代码
  58. if (strlen($this->update_sql_url) > 1) {
  59. $this->updateSql();
  60. }
  61. //退出
  62. return true;
  63. }
  64. private function fileDownload(): bool
  65. {
  66. try {
  67. $f = fopen($this->update_download_url, 'r');
  68. $w = fopen($this->archiveFile, 'wb+');
  69. do {
  70. $a = fread($f, 1024);
  71. fwrite($w, $a);
  72. } while ($a);
  73. fclose($w);
  74. fclose($f);
  75. } catch (ErrorException $e) {
  76. return false;
  77. }
  78. return true;
  79. }
  80. //删除升级包
  81. function delZip()
  82. {
  83. if (file_exists($this->archiveFile)) {
  84. unlink($this->archiveFile);
  85. }
  86. }
  87. //解压
  88. private function unzip($archiveFile, $extractPath): bool
  89. {
  90. $zip = new ZipArchive();
  91. if ($zip->open($archiveFile) === TRUE) {
  92. $zip->extractTo($extractPath, null);
  93. $zip->close();
  94. } else {
  95. return false;
  96. }
  97. return true;
  98. }
  99. //升级的数据库
  100. function updateSql()
  101. {
  102. $f = fopen($this->update_sql_url, 'r');
  103. $sql = "";
  104. do {
  105. $sqlTmp = fread($f, 1024);
  106. $sql = $sql . $sqlTmp;
  107. } while ($sqlTmp);
  108. fclose($f);
  109. // 解析SQL文件内容并执行
  110. $sql_statements = explode(';', trim($sql));
  111. foreach ($sql_statements as $sql_statement) {
  112. if (!empty($sql_statement)) {
  113. try {
  114. \think\facade\Db::query($sql_statement);
  115. } catch (Exception $e) {
  116. }
  117. }
  118. }
  119. }
  120. //递归删除目录
  121. function deleteDirectory($dir)
  122. {
  123. if (!is_dir($dir)) {
  124. return;
  125. }
  126. $files = scandir($dir);
  127. foreach ($files as $file) {
  128. if ($file != '.' && $file != '..') {
  129. if (is_dir("$dir/$file")) {
  130. $this->deleteDirectory("$dir/$file");
  131. } else {
  132. unlink("$dir/$file");
  133. }
  134. }
  135. }
  136. rmdir($dir);
  137. }
  138. // 递归复制目录及其内容
  139. function copyDir($source, $dest)
  140. {
  141. if (!is_dir($dest)) {
  142. mkdir($dest, 0777, true);
  143. }
  144. $files = scandir($source);
  145. foreach ($files as $file) {
  146. if ($file !== '.' && $file !== '..') {
  147. $src = $source . '/' . $file;
  148. $dst = $dest . '/' . $file;
  149. if (is_dir($src)) {
  150. $this->copyDir($src, $dst);
  151. } else {
  152. copy($src, $dst);
  153. }
  154. }
  155. }
  156. }
  157. //覆盖原来的程序
  158. private function copy()
  159. {
  160. //移动覆盖
  161. $this->copyDir("{$this->extractPath}mtab/", "{$this->root_path}");
  162. //删除解压目录
  163. $this->deleteDirectory("{$this->extractPath}mtab");
  164. }
  165. }