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

75 lines
2.2 KiB
PHP
Raw Normal View History

2020-04-29 15:05:49 +08:00
<?php
declare (strict_types = 1);
namespace catcher\command\Tools;
2020-06-25 08:36:29 +08:00
use catcher\CatchAdmin;
2020-05-22 14:09:59 +08:00
use catcher\facade\Http;
2020-04-29 15:06:13 +08:00
use catcher\Tree;
2020-05-22 14:09:59 +08:00
use catcher\Utils;
2020-04-29 15:05:49 +08:00
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;
2020-04-29 15:06:13 +08:00
class ExportDataCommand extends Command
2020-04-29 15:05:49 +08:00
{
protected $table;
protected function configure()
{
// 指令配置
2020-04-29 15:06:13 +08:00
$this->setName('export')
->addArgument('table', Argument::REQUIRED, 'export tables')
->addOption('pid', '-p', Option::VALUE_REQUIRED, 'parent level name')
2020-06-25 08:36:29 +08:00
->addOption('module', '-m', Option::VALUE_REQUIRED, 'module name')
2020-04-29 15:06:13 +08:00
->setDescription('Just for catchAdmin export data');
2020-04-29 15:05:49 +08:00
}
protected function execute(Input $input, Output $output)
{
2020-05-22 14:09:59 +08:00
$table = Utils::tablePrefix() . $input->getArgument('table');
2020-04-29 15:06:13 +08:00
$parent = $input->getOption('pid');
2020-06-25 08:36:29 +08:00
$module = $input->getOption('module');
2020-04-29 15:06:13 +08:00
2020-06-25 08:36:29 +08:00
if ($module) {
$data = Db::name($table)->where('deleted_at', 0)
->where('module', $module)
->select()
->toArray();
2020-09-05 16:01:18 +08:00
2020-06-25 08:36:29 +08:00
} else {
2020-09-05 16:01:18 +08:00
$data = Db::name($table)->where('deleted_at', 0)
->select()
->toArray();
2020-06-25 08:36:29 +08:00
}
2020-04-29 15:06:13 +08:00
if ($parent) {
$data = Tree::done($data, 0, $parent);
}
2020-06-25 08:36:29 +08:00
if ($module) {
$data = 'return ' . var_export($data, true) . ';';
$this->exportSeed($data, $module);
} else {
file_put_contents(root_path() . DIRECTORY_SEPARATOR . $table . '.php', "<?php\r\n return " . var_export($data, true) . ';');
}
2020-04-29 15:05:49 +08:00
$output->info('succeed!');
}
2020-06-25 08:36:29 +08:00
protected function exportSeed($data, $module)
{
$stub = file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'permissionSeed.stub');
2020-09-05 17:18:31 +08:00
$class = ucfirst($module) . 'MenusSeed';
$stub = str_replace('{CLASS}', $class, $stub);
file_put_contents(CatchAdmin::moduleSeedsDirectory($module) . $class .'.php', str_replace('{DATA}', $data, $stub));
2020-06-25 08:36:29 +08:00
}
2020-04-29 15:05:49 +08:00
}
2020-04-29 15:06:13 +08:00