2020-04-30 14:51:32 +08:00
|
|
|
<?php
|
2020-11-29 09:29:14 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-30 14:52:01 +08:00
|
|
|
namespace catcher\library;
|
|
|
|
|
|
|
|
use catcher\CatchAdmin;
|
|
|
|
use catcher\exceptions\FailedException;
|
2020-05-22 15:45:33 +08:00
|
|
|
use catcher\facade\Http;
|
2020-04-30 14:52:01 +08:00
|
|
|
use function GuzzleHttp\Psr7\stream_for;
|
2020-08-10 11:15:48 +08:00
|
|
|
use catcher\facade\FileSystem;
|
2020-04-30 14:52:01 +08:00
|
|
|
|
|
|
|
class Compress
|
|
|
|
{
|
2020-07-11 15:02:05 +08:00
|
|
|
protected $savePath;
|
|
|
|
|
2020-08-10 11:15:48 +08:00
|
|
|
protected $zip;
|
|
|
|
|
2020-04-30 14:52:01 +08:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (!extension_loaded('zip')) {
|
|
|
|
throw new FailedException('you should install extension [zip]');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 压缩模块包
|
|
|
|
*
|
|
|
|
* @time 2020年04月29日
|
|
|
|
* @param $moduleName
|
|
|
|
* @param string $zipPath
|
|
|
|
* @return bool
|
2020-08-10 11:15:48 +08:00
|
|
|
* @throws \Exception
|
2020-04-30 14:52:01 +08:00
|
|
|
*/
|
2020-08-10 11:15:48 +08:00
|
|
|
public function moduleToZip(string $moduleName, string $zipPath = '')
|
2020-04-30 14:52:01 +08:00
|
|
|
{
|
|
|
|
if (!is_dir(CatchAdmin::directory() . $moduleName)) {
|
|
|
|
throw new FailedException(sprintf('module 【%s】not found~', $moduleName));
|
|
|
|
}
|
|
|
|
|
2020-08-10 11:15:48 +08:00
|
|
|
(new Zip())->make($zipPath ? : CatchAdmin::directory() . $moduleName . '.zip', \ZipArchive::CREATE)
|
|
|
|
->folder($moduleName)
|
|
|
|
->addFiles(FileSystem::allFiles(CatchAdmin::moduleDirectory($moduleName)))
|
|
|
|
->close();
|
2020-07-14 17:35:16 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-30 14:52:01 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* download zip
|
|
|
|
*
|
|
|
|
* @time 2020年04月30日
|
2020-05-22 15:45:33 +08:00
|
|
|
* @param $remotePackageUrl
|
2020-04-30 14:52:01 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-11 21:44:39 +08:00
|
|
|
public function download($remotePackageUrl = '')
|
2020-04-30 14:52:01 +08:00
|
|
|
{
|
2020-08-10 11:15:48 +08:00
|
|
|
$response = Http::options([
|
2020-07-11 21:44:39 +08:00
|
|
|
'save_to' => stream_for(fopen($this->savePath, 'w+'))
|
2020-05-22 15:45:33 +08:00
|
|
|
])
|
|
|
|
->get($remotePackageUrl);
|
|
|
|
|
|
|
|
return $response->ok();
|
2020-04-30 14:52:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新
|
|
|
|
*
|
|
|
|
* @time 2020年04月30日
|
|
|
|
* @param $moduleName
|
2020-07-11 16:13:06 +08:00
|
|
|
* @return bool
|
2020-04-30 14:52:01 +08:00
|
|
|
*/
|
|
|
|
public function update($moduleName)
|
|
|
|
{
|
|
|
|
// 备份
|
|
|
|
$backupPath = $this->backup($moduleName);
|
|
|
|
try {
|
2020-07-11 21:44:39 +08:00
|
|
|
$this->moduleUnzip($moduleName, $this->savePath);
|
2020-04-30 14:52:01 +08:00
|
|
|
} catch (\Exception $exception) {
|
2020-08-10 11:15:48 +08:00
|
|
|
// 更新失败先删除原目录
|
|
|
|
FileSystem::deleteDirectory(CatchAdmin::moduleDirectory($moduleName));
|
|
|
|
// 解压备份文件
|
2020-04-30 14:52:01 +08:00
|
|
|
$this->moduleUnzip($moduleName, $backupPath);
|
2020-08-10 11:15:48 +08:00
|
|
|
// 删除备份文件
|
|
|
|
FileSystem::delete($backupPath);
|
2020-07-11 16:13:06 +08:00
|
|
|
return false;
|
2020-04-30 14:52:01 +08:00
|
|
|
}
|
2020-08-10 11:15:48 +08:00
|
|
|
// 删除备份文件
|
|
|
|
FileSystem::delete($backupPath);
|
2020-07-11 16:13:06 +08:00
|
|
|
return true;
|
2020-04-30 14:52:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* overwrite package
|
|
|
|
*
|
|
|
|
* @time 2019年12月16日
|
|
|
|
* @param $moduleName
|
|
|
|
* @param $zipPath
|
|
|
|
* @return bool
|
2020-08-10 11:15:48 +08:00
|
|
|
* @throws \Exception
|
2020-04-30 14:52:01 +08:00
|
|
|
*/
|
|
|
|
public function moduleUnzip($moduleName, $zipPath)
|
|
|
|
{
|
2020-08-10 11:15:48 +08:00
|
|
|
try {
|
|
|
|
(new Zip())->make($zipPath)->extractTo(CatchAdmin::moduleDirectory($moduleName) . $moduleName)->close();
|
2020-04-30 14:52:01 +08:00
|
|
|
return true;
|
2020-08-10 11:15:48 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new FailedException('更新失败');
|
2020-04-30 14:52:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除目录
|
|
|
|
*
|
|
|
|
* @time 2020年04月29日
|
|
|
|
* @param $packageDir
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-06-25 09:25:02 +08:00
|
|
|
public function rmDir($packageDir)
|
2020-04-30 14:52:01 +08:00
|
|
|
{
|
|
|
|
$fileSystemIterator = new \FilesystemIterator($packageDir);
|
|
|
|
try {
|
|
|
|
foreach ($fileSystemIterator as $fileSystem) {
|
|
|
|
if ($fileSystem->isDir()) {
|
|
|
|
if ((new \FilesystemIterator($fileSystem->getPathName()))->valid()) {
|
|
|
|
$this->rmDir($fileSystem->getPathName());
|
|
|
|
} else {
|
|
|
|
rmdir($fileSystem->getPathName());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unlink($fileSystem->getPathName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
throw new FailedException($exception->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
rmdir($packageDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年04月29日
|
|
|
|
* @param $path
|
|
|
|
* @param string $moduleName
|
|
|
|
* @param $tempExtractToPath
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function copyFileToModule($path, $moduleName, $tempExtractToPath)
|
|
|
|
{
|
|
|
|
$fileSystemIterator = new \FilesystemIterator($path . $moduleName ? : '');
|
|
|
|
|
|
|
|
foreach ($fileSystemIterator as $fileSystem) {
|
|
|
|
if ($fileSystem->isDir()) {
|
|
|
|
$this->copyFileToModule($fileSystem->getPathname(), '', $tempExtractToPath);
|
|
|
|
} else {
|
|
|
|
// 原模块文件
|
|
|
|
$originModuleFile = str_replace($tempExtractToPath, CatchAdmin::directory(), $fileSystem->getPathname());
|
|
|
|
// md5 校验 文件是否修改过
|
|
|
|
if (md5_file($originModuleFile) != md5_file($fileSystem->getPathname())) {
|
|
|
|
if (!copy($fileSystem->getPathname(), $originModuleFile)) {
|
|
|
|
throw new FailedException('更新失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 备份原文件
|
|
|
|
*
|
|
|
|
* @time 2020年04月30日
|
|
|
|
* @param $moduleName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function backup($moduleName)
|
|
|
|
{
|
|
|
|
$backup = $this->getModuleBackupPath($moduleName);
|
|
|
|
|
|
|
|
CatchAdmin::makeDirectory($backup);
|
|
|
|
|
|
|
|
$this->moduleToZip($moduleName, $backup . $moduleName. '.zip');
|
|
|
|
|
|
|
|
return $backup . $moduleName . '.zip';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取备份地址
|
|
|
|
*
|
|
|
|
* @time 2020年04月30日
|
|
|
|
* @param $moduleName
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getModuleBackupPath($moduleName)
|
|
|
|
{
|
|
|
|
return $backup = runtime_path('module' . DIRECTORY_SEPARATOR . 'backup_'.$moduleName);
|
|
|
|
}
|
2020-07-11 15:02:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 保存地址
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* @return $this
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @date 2020/7/11
|
|
|
|
*/
|
|
|
|
public function savePath($path)
|
|
|
|
{
|
|
|
|
$this->savePath = $path;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-04-30 14:52:01 +08:00
|
|
|
}
|