生成代码新增回滚功能
This commit is contained in:
parent
42e75f50ff
commit
359381fec6
@ -6,7 +6,9 @@ use catcher\exceptions\FailedException;
|
||||
use catcher\generate\factory\Controller;
|
||||
use catcher\generate\factory\Migration;
|
||||
use catcher\generate\factory\Model;
|
||||
use catcher\generate\factory\Route;
|
||||
use catcher\generate\factory\SQL;
|
||||
use think\facade\Db;
|
||||
|
||||
class Generator
|
||||
{
|
||||
@ -24,30 +26,42 @@ class Generator
|
||||
[$controller, $model] = $this->parseParams($params);
|
||||
|
||||
$message = [];
|
||||
|
||||
$files = [];
|
||||
$migration = '';
|
||||
$table = null;
|
||||
|
||||
try {
|
||||
if ($params['create_controller']) {
|
||||
if ((new Controller)->done($controller)) {
|
||||
$files[] = (new Controller)->done($controller);
|
||||
array_push($message, 'controller created successfully');
|
||||
}
|
||||
}
|
||||
|
||||
if ($params['create_table']) {
|
||||
if ((new SQL)->done($model)) {
|
||||
$table = (new SQL)->done($model);
|
||||
array_push($message, 'table created successfully');
|
||||
}
|
||||
}
|
||||
|
||||
if ($params['create_model']) {
|
||||
if ((new Model)->done($model)) {
|
||||
$files[] = (new Model)->done($model);
|
||||
array_push($message, 'model created successfully');
|
||||
}
|
||||
}
|
||||
|
||||
if ($params['create_migration']) {
|
||||
if ((new Migration)->done([$controller['module'], $model['table']])) {
|
||||
$migration = (new Migration)->done([$controller['module'], $model['table']]);
|
||||
array_push($message, 'migration created successfully');
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->rollback($files, $migration, $table);
|
||||
throw new FailedException($exception->getMessage());
|
||||
}
|
||||
|
||||
// 只有最后成功才写入 route
|
||||
(new Route())->controller($params['controller'])
|
||||
->restful($params['restful'])
|
||||
->methods((new Controller())->parseOtherMethods($params['other_function']))
|
||||
->done();
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
@ -115,4 +129,36 @@ class Generator
|
||||
|
||||
return [$controller, $model];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 回滚
|
||||
*
|
||||
* @param $files
|
||||
* @param $migration
|
||||
* @param $table
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function rollback($files, $migration, $table)
|
||||
{
|
||||
if ((new SQL())->hasTableExists($table)) {
|
||||
Db::query(sprintf('drop table %s', $table));
|
||||
}
|
||||
|
||||
foreach ($files as $file) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
if ($migration && unlink($migration)) {
|
||||
$model = new class extends \think\Model {
|
||||
protected $name = 'migrations';
|
||||
};
|
||||
|
||||
$model->order('version', 'desc')->find()->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,9 @@ class Controller extends Factory
|
||||
public function done($params)
|
||||
{
|
||||
// 写入成功之后
|
||||
if (file_put_contents($this->getGeneratePath($params['controller']), $this->getContent($params))) {
|
||||
return (new Route())->controller($params['controller'])
|
||||
->restful($params['restful'])
|
||||
->methods($this->parseOtherMethods($params['other_function']))
|
||||
->done();
|
||||
$controllerPath = $this->getGeneratePath($params['controller']);
|
||||
if (file_put_contents($controllerPath, $this->getContent($params))) {
|
||||
return $controllerPath;
|
||||
}
|
||||
|
||||
throw new FailedException($params['controller'] . ' generate failed~');
|
||||
|
@ -89,7 +89,7 @@ abstract class Factory
|
||||
* @param $table
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasTableExists($table)
|
||||
public function hasTableExists($table)
|
||||
{
|
||||
$tables = Db::getConnection()->getTables();
|
||||
|
||||
|
@ -48,6 +48,6 @@ class Migration extends Factory
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return $file;
|
||||
}
|
||||
}
|
@ -12,13 +12,15 @@ class Model extends Factory
|
||||
{
|
||||
$content = $this->getContent($params);
|
||||
|
||||
file_put_contents($this->getGeneratePath($params['model']), $content);
|
||||
$modelPath = $this->getGeneratePath($params['model']);
|
||||
|
||||
if (!file_exists($this->getGeneratePath($params['model']))) {
|
||||
file_put_contents($modelPath, $content);
|
||||
|
||||
if (!file_exists($modelPath)) {
|
||||
throw new FailedException('create model failed');
|
||||
}
|
||||
|
||||
return true;
|
||||
return $modelPath;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,7 +19,7 @@ class SQL extends Factory
|
||||
throw new FailedException(sprintf('create table [%s] failed', $params['table']));
|
||||
}
|
||||
|
||||
return true;
|
||||
return $params['table'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user