Upgrade2.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. $this->deleteDirectory(public_path().'dist/');//删除旧的网站文件
  52. //拷贝覆盖
  53. $this->copy();
  54. //删除下载的更新包
  55. $this->delZip();
  56. //更新完后的一些操作
  57. }
  58. //如果有数据库的更新资源则更新程序代码
  59. if (strlen($this->update_sql_url) > 1) {
  60. $this->updateSql();
  61. }
  62. //退出
  63. return true;
  64. }
  65. private function fileDownload(): bool
  66. {
  67. try {
  68. $f = fopen($this->update_download_url, 'r');
  69. $w = fopen($this->archiveFile, 'wb+');
  70. do {
  71. $a = fread($f, 1024);
  72. fwrite($w, $a);
  73. } while ($a);
  74. fclose($w);
  75. fclose($f);
  76. } catch (ErrorException $e) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. //删除升级包
  82. function delZip()
  83. {
  84. if (file_exists($this->archiveFile)) {
  85. unlink($this->archiveFile);
  86. }
  87. }
  88. //解压
  89. private function unzip($archiveFile, $extractPath): bool
  90. {
  91. $zip = new ZipArchive();
  92. if ($zip->open($archiveFile) === TRUE) {
  93. $zip->extractTo($extractPath, null);
  94. $zip->close();
  95. } else {
  96. return false;
  97. }
  98. return true;
  99. }
  100. //升级的数据库
  101. function updateSql()
  102. {
  103. $f = fopen($this->update_sql_url, 'r');
  104. $sql = "";
  105. do {
  106. $sqlTmp = fread($f, 1024);
  107. $sql = $sql . $sqlTmp;
  108. } while ($sqlTmp);
  109. fclose($f);
  110. // 解析SQL文件内容并执行
  111. $sql_statements = explode(';', trim($sql));
  112. foreach ($sql_statements as $sql_statement) {
  113. if (!empty($sql_statement)) {
  114. try {
  115. \think\facade\Db::execute($sql_statement);
  116. } catch (Exception $e) {
  117. }
  118. }
  119. }
  120. }
  121. //递归删除目录
  122. function deleteDirectory($dir)
  123. {
  124. if (!is_dir($dir)) {
  125. return;
  126. }
  127. $files = scandir($dir);
  128. foreach ($files as $file) {
  129. if ($file != '.' && $file != '..') {
  130. if (is_dir("$dir/$file")) {
  131. $this->deleteDirectory("$dir/$file");
  132. } else {
  133. unlink("$dir/$file");
  134. }
  135. }
  136. }
  137. rmdir($dir);
  138. }
  139. // 递归复制目录及其内容
  140. function copyDir($source, $dest)
  141. {
  142. if (!is_dir($dest)) {
  143. mkdir($dest, 0777, true);
  144. }
  145. $files = scandir($source);
  146. foreach ($files as $file) {
  147. if ($file !== '.' && $file !== '..') {
  148. $src = $source . '/' . $file;
  149. $dst = $dest . '/' . $file;
  150. if (is_dir($src)) {
  151. $this->copyDir($src, $dst);
  152. } else {
  153. copy($src, $dst);
  154. }
  155. }
  156. }
  157. }
  158. //覆盖原来的程序
  159. private function copy()
  160. {
  161. //移动覆盖
  162. $this->copyDir("{$this->extractPath}mtab/", "{$this->root_path}");
  163. //删除解压目录
  164. $this->deleteDirectory("{$this->extractPath}mtab");
  165. }
  166. }