From bbfb206210ab160987fac6c257103f8adbcc2f72 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 10 Aug 2020 11:15:48 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E6=89=93=E5=8C=85?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/Compress.php | 106 ++++++---------------------- 1 file changed, 22 insertions(+), 84 deletions(-) diff --git a/extend/catcher/library/Compress.php b/extend/catcher/library/Compress.php index f936a1c..fe7f29a 100644 --- a/extend/catcher/library/Compress.php +++ b/extend/catcher/library/Compress.php @@ -4,17 +4,15 @@ namespace catcher\library; use catcher\CatchAdmin; use catcher\exceptions\FailedException; use catcher\facade\Http; -use Doctrine\DBAL\Types\DateImmutableType; -use GuzzleHttp\Client; -use GuzzleHttp\TransferStats; -use Psr\Http\Message\ResponseInterface; -use function GuzzleHttp\Psr7\str; use function GuzzleHttp\Psr7\stream_for; +use catcher\facade\FileSystem; class Compress { protected $savePath; + protected $zip; + public function __construct() { if (!extension_loaded('zip')) { @@ -29,52 +27,22 @@ class Compress * @param $moduleName * @param string $zipPath * @return bool + * @throws \Exception */ - public function moduleToZip($moduleName, $zipPath = '') + public function moduleToZip(string $moduleName, string $zipPath = '') { if (!is_dir(CatchAdmin::directory() . $moduleName)) { throw new FailedException(sprintf('module 【%s】not found~', $moduleName)); } - // zip 打包位置 默认打包在 catch 目录下 - $zipPath = $zipPath ? : CatchAdmin::directory() . $moduleName . '.zip'; - - $this->dirToZip(CatchAdmin::directory() . $moduleName, $zipPath); + (new Zip())->make($zipPath ? : CatchAdmin::directory() . $moduleName . '.zip', \ZipArchive::CREATE) + ->folder($moduleName) + ->addFiles(FileSystem::allFiles(CatchAdmin::moduleDirectory($moduleName))) + ->close(); return true; } - /** - * 打包 dir - * - * @time 2020年07月13日 - * @param $dir - * @param $zipPath - * @return bool - */ - public function dirToZip($dir, $zipPath) - { - $packageZip = new \ZipArchive(); - - $files = $this->getFilesFromDir($dir); - - $packageZip->open($zipPath, \ZipArchive::CREATE); - - // 获取 dir 目录作为 zip 的根目录 - $d = explode(DIRECTORY_SEPARATOR, rtrim($dir, DIRECTORY_SEPARATOR)); - - $packageZip->addEmptyDir(array_pop($d)); - - foreach ($files as $file) { - $baseName = basename($file); - $localName = str_replace([implode(DIRECTORY_SEPARATOR, $d), $baseName], ['', ''], $file); - $packageZip->addFile($file, $localName . $baseName); - } - - $packageZip->close(); - - return true; - } /** * download zip @@ -85,8 +53,7 @@ class Compress */ public function download($remotePackageUrl = '') { - $response = Http::timeout(10) - ->options([ + $response = Http::options([ 'save_to' => stream_for(fopen($this->savePath, 'w+')) ]) ->get($remotePackageUrl); @@ -108,11 +75,16 @@ class Compress try { $this->moduleUnzip($moduleName, $this->savePath); } catch (\Exception $exception) { + // 更新失败先删除原目录 + FileSystem::deleteDirectory(CatchAdmin::moduleDirectory($moduleName)); + // 解压备份文件 $this->moduleUnzip($moduleName, $backupPath); - $this->rmDir($this->getModuleBackupPath($moduleName)); + // 删除备份文件 + FileSystem::delete($backupPath); return false; } - + // 删除备份文件 + FileSystem::delete($backupPath); return true; } @@ -123,51 +95,18 @@ class Compress * @param $moduleName * @param $zipPath * @return bool + * @throws \Exception */ public function moduleUnzip($moduleName, $zipPath) { - $zip = new \ZipArchive(); - - // 创建解压包的临时目录 - $tempExtractToPath = runtime_path('module' . DIRECTORY_SEPARATOR . date('YmdHis')); - CatchAdmin::makeDirectory($tempExtractToPath); - // 下载 zip 包 - $res = $zip->open($zipPath); - if ($res === true) { - $zip->extractTo($tempExtractToPath); - $zip->close(); - $this->copyFileToModule($tempExtractToPath, $moduleName, $tempExtractToPath); - // 删除临时文件夹 - $this->rmDir($tempExtractToPath); + try { + (new Zip())->make($zipPath)->extractTo(CatchAdmin::moduleDirectory($moduleName) . $moduleName)->close(); return true; + } catch (\Exception $e) { + throw new FailedException('更新失败'); } - - throw new FailedException('更新失败'); } - /** - * get files from dir - * - * @time 2019年12月16日 - * @param $packageDir - * @return array - */ - protected function getFilesFromDir($packageDir): array - { - $files = []; - - $fileSystemIterator = new \FilesystemIterator($packageDir); - - foreach ($fileSystemIterator as $fileSystem) { - if ($fileSystem->isDir()) { - $files = array_merge($this->getFilesFromDir($fileSystem->getPathName()), $files); - } else { - $files[] = $fileSystem->getPathName(); - } - } - - return $files; - } /** * 删除目录 @@ -235,7 +174,6 @@ class Compress */ protected function backup($moduleName) { - $backup = $this->getModuleBackupPath($moduleName); CatchAdmin::makeDirectory($backup);