update:更新打包工具
This commit is contained in:
parent
2f76d0da38
commit
bbfb206210
@ -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('更新失败');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user