新增导入树状数据方法

This commit is contained in:
JaguarJack
2020-04-29 15:05:49 +08:00
parent 76855d6b5d
commit 514bb6b520
5 changed files with 70 additions and 1 deletions

View File

@@ -0,0 +1,185 @@
<?php
declare (strict_types = 1);
namespace catcher\command;
use catcher\CatchAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class BackupCommand extends Command
{
protected $table;
protected function configure()
{
// 指令配置
$this->setName('backup:data')
->addArgument('tables', Argument::REQUIRED, 'backup tables')
->addOption('zip', '-z',Option::VALUE_NONE, 'is need zip')
->setDescription('backup data you need');
}
protected function execute(Input $input, Output $output)
{
$tables = $this->input->getArgument('tables');
$isZip = $this->input->getOption('zip');
$this->generator(explode(',', $tables), CatchAdmin::backupDirectory());
if ($isZip) {
$this->zip();
}
$output->info('succeed!');
}
/**
*
* @time 2019年09月30日
* @param $tables
* @param $format
* @param $path
* @return void
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function generator($tables, $path): void
{
foreach ($tables as $table) {
$this->table = $table;
$this->createDataFile();
}
}
/**
* 创建数据文件
*
* @time 2019年09月27日
* @param $format
* @return void
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public function createDataFile(): void
{
$file = CatchAdmin::backupDirectory() . $this->table . '.sql';
$handle = fopen($file, 'wb+');
fwrite($handle, $begin = "BEGIN;\r\n", \strlen($begin));
$this->createClass($this->table, $handle);
fwrite($handle, $end = 'COMMIT;', \strlen($end));
fclose($handle);
}
/**
* 创建了临时模型
*
* @time 2019年09月27日
* @param $table
* @param $format
* @param $handle
* @return void
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
protected function createClass($table, $handle)
{
$this->setUnbuffered();
// 防止 IO 多次写入
$buffer = [];
// 记录中记录
$total = Db::table($table)->count();
$limit = 1000;
// 生成器减少内存
while ($total > 0) {
$items = Db::table($table)->limit($limit)->select();
$this->writeIn($handle, $items);
$total -= $limit;
}
}
/**
* sql 文件格式
*
* @time 2019年09月27日
* @param $handle
* @param $data
* @return void
*/
protected function writeIn($handle, $datas)
{
$values = '';
$sql = '';
foreach ($datas as $data) {
foreach ($data as $value) {
$values .= sprintf("'%s'", $value) . ',';
}
$sql .= sprintf('INSERT INTO `%s` VALUE (%s);' . "\r\n", $this->table, rtrim($values, ','));
$values = '';
}
fwrite($handle, $sql, strlen($sql));
}
/**
* 设置未缓存模式
*
* @time 2019年09月29日
* @return void
*/
protected function setUnbuffered()
{
$connections = \config('database.connections');
$connections['mysql']['params'] = [
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false
];
\config([
'connections' => $connections,
],'database.connections');
}
protected function zip()
{
if (extension_loaded('zip')) {
$files = glob(CatchAdmin::backupDirectory() . '*.sql');
$zip = new \ZipArchive();
$backupPath = runtime_path('database/');
CatchAdmin::makeDirectory($backupPath);
$zip->open($backupPath . 'backup.zip', \ZipArchive::CREATE);
$zip->addEmptyDir('backup');
foreach ($files as $file) {
$zip->addFile($file, 'backup/'. basename($file));
}
$zip->close();
foreach ($files as $file) {
unlink($file);
}
rmdir(CatchAdmin::backupDirectory());
}
}
}

View File

@@ -0,0 +1,149 @@
<?php
declare (strict_types = 1);
namespace catcher\command;
use catcher\CatchAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\input\Option as InputOption;
use think\console\Output;
class CompressPackageCommand extends Command
{
protected function configure()
{
// 指令配置
$this->setName('package:zip')
->addArgument('package', Argument::REQUIRED, 'package name')
->addOption('compress', 'c', InputOption::VALUE_REQUIRED, 'zip or unzip')
->setDescription('compress package to zip');
}
protected function execute(Input $input, Output $output)
{
$package = $this->input->getArgument('package');
$compress = $this->input->getOption('compress');
if (!extension_loaded('zip')) {
$this->output->error('you should install extension [zip]');
exit;
}
if ($compress == 'false') {
$this->unzip();
} else {
$this->zip($package);
}
}
/**
* 压缩包
*
* @time 2019年12月16日
* @param $package
* @return void
*/
protected function zip($package): void
{
if (!is_dir(CatchAdmin::directory() . $package)) {
$package = $this->output->ask($this->input, sprintf('Can not find [%s] in catch directory, you should input package again', $package));
}
if (!is_dir(CatchAdmin::directory() . $package)) {
$this->output->error('check package exists?');exit;
}
$files = $this->getFilesFromDir(CatchAdmin::directory() . $package);
$packageZip = new \ZipArchive();
$packageZip->open(CatchAdmin::directory() . $package . '.zip', \ZipArchive::CREATE);
$packageZip->addEmptyDir($package);
foreach ($files as $file) {
$baseName = basename($file);
$localName = str_replace([CatchAdmin::directory(), $baseName], ['', ''], $file);
$packageZip->addFile($file, $localName . $baseName);
}
$packageZip->close();
$this->output->info(sprintf('%s.zip compress successfully!', $package));
}
/**
* overwrite package
*
* @time 2019年12月16日
* @return bool
*/
protected function unzip()
{
$zip = new \ZipArchive();
//dd($zip->open(CatchAdmin::directory() . 'permissions.zip'));
$res = $zip->open(CatchAdmin::directory() . 'permissions.zip');
$extractToPath = CatchAdmin::directory() . 'goods';
if (is_dir($extractToPath)) {
$this->rmDir($extractToPath);
}
$extractToPath = CatchAdmin::makeDirectory($extractToPath);
if ($res === true) {
$zip->extractTo($extractToPath);
$zip->close();
$this->output->info('unzip successfully');
return true;
}
$this->output->error('unzip failed');
}
protected function rmDir($packageDir)
{
$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);
}
/**
* 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;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare (strict_types = 1);
namespace catcher\command\Tools;
use catcher\CatchAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class BackUpDataCommand extends Command
{
protected $table;
protected function configure()
{
// 指令配置
$this->setName('backup:data')
->addArgument('tables', Argument::REQUIRED, 'backup tables')
->addOption('zip', '-z',Option::VALUE_NONE, 'is need zip')
->setDescription('backup data you need');
}
protected function execute(Input $input, Output $output)
{
$output->info('succeed!');
}
}