PluginsInstall.php 4.8 KB

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