catchAdmin/extend/catcher/command/Tools/CompressPackageCommand.php

192 lines
5.8 KiB
PHP
Raw Normal View History

2019-12-16 17:25:40 +08:00
<?php
declare (strict_types = 1);
2020-04-29 15:06:13 +08:00
namespace catcher\command\Tools;
2019-12-16 17:25:40 +08:00
use catcher\CatchAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
2019-12-17 09:03:32 +08:00
use think\console\input\Option as InputOption;
2019-12-16 17:25:40 +08:00
use think\console\Output;
2019-12-17 09:03:32 +08:00
class CompressPackageCommand extends Command
2019-12-16 17:25:40 +08:00
{
protected function configure()
{
// 指令配置
$this->setName('package:zip')
->addArgument('package', Argument::REQUIRED, 'package name')
2019-12-17 09:03:32 +08:00
->addOption('compress', 'c', InputOption::VALUE_REQUIRED, 'zip or unzip')
2019-12-16 17:25:40 +08:00
->setDescription('compress package to zip');
}
protected function execute(Input $input, Output $output)
{
$package = $this->input->getArgument('package');
2019-12-17 09:03:32 +08:00
$compress = $this->input->getOption('compress');
2019-12-16 17:25:40 +08:00
2019-12-17 09:03:32 +08:00
if (!extension_loaded('zip')) {
$this->output->error('you should install extension [zip]');
exit;
2019-12-16 17:25:40 +08:00
}
2019-12-17 09:03:32 +08:00
if ($compress == 'false') {
$this->unzip();
} else {
$this->zip($package);
}
2019-12-16 17:25:40 +08:00
}
2019-12-17 09:03:32 +08:00
/**
* 压缩包
*
* @time 2019年12月16日
* @param $package
* @return void
*/
protected function zip($package): void
2019-12-16 17:25:40 +08:00
{
2019-12-17 09:03:32 +08:00
if (!is_dir(CatchAdmin::directory() . $package)) {
2019-12-22 09:37:52 +08:00
$package = $this->output->ask($this->input, sprintf('Can not find [%s] in catch directory, you should input package again', $package));
2019-12-17 09:03:32 +08:00
}
2019-12-16 17:25:40 +08:00
2019-12-17 09:03:32 +08:00
if (!is_dir(CatchAdmin::directory() . $package)) {
$this->output->error('check package exists?');exit;
}
2019-12-16 17:25:40 +08:00
2019-12-17 09:03:32 +08:00
$files = $this->getFilesFromDir(CatchAdmin::directory() . $package);
$packageZip = new \ZipArchive();
$packageZip->open(CatchAdmin::directory() . $package . '.zip', \ZipArchive::CREATE);
$packageZip->addEmptyDir($package);
2019-12-16 17:25:40 +08:00
foreach ($files as $file) {
2019-12-17 09:03:32 +08:00
$baseName = basename($file);
$localName = str_replace([CatchAdmin::directory(), $baseName], ['', ''], $file);
$packageZip->addFile($file, $localName . $baseName);
2019-12-16 17:25:40 +08:00
}
$packageZip->close();
2019-12-17 09:03:32 +08:00
$this->output->info(sprintf('%s.zip compress successfully!', $package));
2019-12-16 17:25:40 +08:00
}
2019-12-17 09:03:32 +08:00
/**
* overwrite package
*
* @time 2019年12月16日
* @return bool
*/
2019-12-16 17:25:40 +08:00
protected function unzip()
{
2019-12-17 09:03:32 +08:00
$zip = new \ZipArchive();
2020-04-29 17:35:29 +08:00
// 创建解压包的临时目录
$tempExtractToPath = runtime_path('module' . DIRECTORY_SEPARATOR . date('YmdHis'));
CatchAdmin::makeDirectory($tempExtractToPath);
/**$extractToPath = CatchAdmin::directory();
2019-12-17 09:03:32 +08:00
if (is_dir($extractToPath)) {
$this->rmDir($extractToPath);
2020-04-29 17:35:29 +08:00
}*/
2019-12-17 09:03:32 +08:00
2020-04-29 17:35:29 +08:00
$res = $zip->open(CatchAdmin::directory() . 'permissions.zip');
2019-12-17 09:03:32 +08:00
if ($res === true) {
2020-04-29 17:35:29 +08:00
$zip->extractTo($tempExtractToPath);
2019-12-17 09:03:32 +08:00
$zip->close();
$this->output->info('unzip successfully');
2020-04-29 17:35:29 +08:00
$this->copyFileToModule($tempExtractToPath, 'permissions', $tempExtractToPath);
$this->rmDir($tempExtractToPath);
$this->output->info('更新成功');
2019-12-17 09:03:32 +08:00
return true;
}
2019-12-16 17:25:40 +08:00
2019-12-17 09:03:32 +08:00
$this->output->error('unzip failed');
2019-12-16 17:25:40 +08:00
}
2020-04-29 17:35:29 +08:00
/**
* 删除目录
*
* @time 2020年04月29日
* @param $packageDir
* @return void
*/
2019-12-17 09:03:32 +08:00
protected function rmDir($packageDir)
2019-12-16 17:25:40 +08:00
{
2019-12-17 09:03:32 +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) {
$this->output->error($exception->getMessage());
exit;
}
rmdir($packageDir);
}
2020-04-29 17:35:29 +08:00
/**
*
* @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)) {
$this->output->error($originModuleFile . ' 更新失败');
} else {
$this->output->error($originModuleFile . ' 更新成功');
}
}
}
}
}
2019-12-17 09:03:32 +08:00
/**
* get files from dir
*
* @time 2019年12月16日
* @param $packageDir
* @return array
*/
protected function getFilesFromDir($packageDir): array
{
$files = [];
2019-12-16 17:25:40 +08:00
$fileSystemIterator = new \FilesystemIterator($packageDir);
foreach ($fileSystemIterator as $fileSystem) {
if ($fileSystem->isDir()) {
2019-12-17 09:03:32 +08:00
$files = array_merge($this->getFilesFromDir($fileSystem->getPathName()), $files);
2019-12-16 17:25:40 +08:00
} else {
$files[] = $fileSystem->getPathName();
}
}
2019-12-17 09:03:32 +08:00
return $files;
2019-12-16 17:25:40 +08:00
}
}