catchAdmin/extend/catcher/command/ModelGeneratorCommand.php

62 lines
1.8 KiB
PHP
Raw Normal View History

2019-12-06 09:17:40 +08:00
<?php
namespace catcher\command;
use catcher\CatchAdmin;
2020-05-28 06:38:10 +08:00
use catcher\generate\factory\Model;
2019-12-06 09:17:40 +08:00
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
2020-05-28 06:38:10 +08:00
use think\console\input\Option;
2019-12-06 09:17:40 +08:00
use think\console\input\Option as InputOption;
use think\console\Output;
use think\facade\Db;
use think\helper\Str;
class ModelGeneratorCommand extends Command
{
protected function configure()
{
$this->setName('create:model')
->addArgument('module', Argument::REQUIRED, 'module name')
2020-01-23 16:55:51 +08:00
->addArgument('model', Argument::REQUIRED, 'model name')
2020-05-28 06:38:10 +08:00
->addOption('softDelete', '-d', Option::VALUE_REQUIRED, 'soft delete')
2019-12-06 09:17:40 +08:00
->setDescription('create model');
}
protected function execute(Input $input, Output $output)
{
$model = ucfirst($input->getArgument('model'));
$module = strtolower($input->getArgument('module'));
2020-05-28 06:38:10 +08:00
$softDelete = $input->getOption('softDelete');
2019-12-06 09:17:40 +08:00
2020-05-28 06:38:10 +08:00
$params = [
'model' => 'catchAdmin\\'.$module.'\\model\\'.$model,
'table' => Str::snake($model),
'extra' => [
'soft_delete' => $softDelete ? true : false,
],
];
2019-12-06 09:17:40 +08:00
$modelFile= CatchAdmin::getModuleModelDirectory($module) . $model . '.php';
2020-04-17 17:10:39 +08:00
$asn = 'Y';
2020-05-28 06:38:10 +08:00
2020-04-17 17:10:39 +08:00
if (file_exists($modelFile)) {
$asn = $this->output->ask($this->input, "Model File {$model} already exists.Are you sure to overwrite, the content will be lost(Y/N)");
}
if (strtolower($asn) == 'n') {
exit(0);
}
2020-05-28 06:38:10 +08:00
(new Model())->done($params);
2019-12-06 09:17:40 +08:00
if (file_exists($modelFile)) {
$output->info(sprintf('%s Create Successfully!', $modelFile));
} else {
$output->error(sprintf('%s Create Failed!', $modelFile));
}
}
}