生成代码新增回滚功能

This commit is contained in:
JaguarJack 2020-07-11 10:59:57 +08:00
parent 42e75f50ff
commit 359381fec6
6 changed files with 68 additions and 22 deletions

View File

@ -6,7 +6,9 @@ use catcher\exceptions\FailedException;
use catcher\generate\factory\Controller; use catcher\generate\factory\Controller;
use catcher\generate\factory\Migration; use catcher\generate\factory\Migration;
use catcher\generate\factory\Model; use catcher\generate\factory\Model;
use catcher\generate\factory\Route;
use catcher\generate\factory\SQL; use catcher\generate\factory\SQL;
use think\facade\Db;
class Generator class Generator
{ {
@ -24,30 +26,42 @@ class Generator
[$controller, $model] = $this->parseParams($params); [$controller, $model] = $this->parseParams($params);
$message = []; $message = [];
if ($params['create_controller']) {
if ((new Controller)->done($controller)) { $files = [];
$migration = '';
$table = null;
try {
if ($params['create_controller']) {
$files[] = (new Controller)->done($controller);
array_push($message, 'controller created successfully'); array_push($message, 'controller created successfully');
} }
}
if ($params['create_table']) { if ($params['create_table']) {
if ((new SQL)->done($model)) { $table = (new SQL)->done($model);
array_push($message, 'table created successfully'); array_push($message, 'table created successfully');
} }
}
if ($params['create_model']) { if ($params['create_model']) {
if ((new Model)->done($model)) { $files[] = (new Model)->done($model);
array_push($message, 'model created successfully'); array_push($message, 'model created successfully');
} }
}
if ($params['create_migration']) { 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'); 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; return $message;
} }
@ -115,4 +129,36 @@ class Generator
return [$controller, $model]; 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();
}
}
} }

View File

@ -18,11 +18,9 @@ class Controller extends Factory
public function done($params) public function done($params)
{ {
// 写入成功之后 // 写入成功之后
if (file_put_contents($this->getGeneratePath($params['controller']), $this->getContent($params))) { $controllerPath = $this->getGeneratePath($params['controller']);
return (new Route())->controller($params['controller']) if (file_put_contents($controllerPath, $this->getContent($params))) {
->restful($params['restful']) return $controllerPath;
->methods($this->parseOtherMethods($params['other_function']))
->done();
} }
throw new FailedException($params['controller'] . ' generate failed~'); throw new FailedException($params['controller'] . ' generate failed~');

View File

@ -89,7 +89,7 @@ abstract class Factory
* @param $table * @param $table
* @return bool * @return bool
*/ */
protected function hasTableExists($table) public function hasTableExists($table)
{ {
$tables = Db::getConnection()->getTables(); $tables = Db::getConnection()->getTables();

View File

@ -48,6 +48,6 @@ class Migration extends Factory
} }
} }
return true; return $file;
} }
} }

View File

@ -12,13 +12,15 @@ class Model extends Factory
{ {
$content = $this->getContent($params); $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'); throw new FailedException('create model failed');
} }
return true; return $modelPath;
} }
/** /**

View File

@ -19,7 +19,7 @@ class SQL extends Factory
throw new FailedException(sprintf('create table [%s] failed', $params['table'])); throw new FailedException(sprintf('create table [%s] failed', $params['table']));
} }
return true; return $params['table'];
} }
/** /**