168 lines
4.5 KiB
PHP
Raw Normal View History

2020-04-28 16:34:17 +08:00
<?php
2020-04-28 22:02:03 +08:00
namespace catcher\generate;
2020-04-28 16:34:17 +08:00
2020-04-28 22:02:03 +08:00
use catcher\exceptions\FailedException;
use catcher\generate\factory\Controller;
use catcher\generate\factory\Migration;
use catcher\generate\factory\Model;
2020-07-11 10:59:57 +08:00
use catcher\generate\factory\Route;
2020-04-28 22:02:03 +08:00
use catcher\generate\factory\SQL;
2020-07-11 10:59:57 +08:00
use think\facade\Db;
2020-04-28 16:34:17 +08:00
class Generator
{
2020-04-29 11:41:54 +08:00
/**
* generate
*
* @time 2020年04月29日
* @param $params
* @return array
*/
2020-04-28 16:34:17 +08:00
public function done($params)
{
$params = \json_decode($params['data'], true);
2020-04-28 22:02:03 +08:00
[$controller, $model] = $this->parseParams($params);
$message = [];
2020-07-11 10:59:57 +08:00
$files = [];
$migration = '';
$table = null;
try {
if ($params['create_controller']) {
$files[] = (new Controller)->done($controller);
2020-04-28 22:02:03 +08:00
array_push($message, 'controller created successfully');
}
2020-07-11 10:59:57 +08:00
if ($params['create_table']) {
$table = (new SQL)->done($model);
2020-04-28 22:02:03 +08:00
array_push($message, 'table created successfully');
}
2020-07-11 10:59:57 +08:00
if ($params['create_model']) {
$files[] = (new Model)->done($model);
2020-04-28 22:02:03 +08:00
array_push($message, 'model created successfully');
}
2020-04-28 16:34:17 +08:00
2020-07-11 10:59:57 +08:00
if ($params['create_migration']) {
$migration = (new Migration)->done([$controller['module'], $model['table']]);
2020-04-28 22:02:03 +08:00
array_push($message, 'migration created successfully');
}
2020-07-13 17:05:34 +08:00
// 只有最后成功才写入 route
(new Route())->controller($controller['controller'])
->restful($controller['restful'])
->methods((new Controller())->parseOtherMethods($controller['other_function']))
->done();
2020-07-11 10:59:57 +08:00
} catch (\Exception $exception) {
$this->rollback($files, $migration, $table);
throw new FailedException($exception->getMessage());
2020-04-28 22:02:03 +08:00
}
2020-07-11 10:59:57 +08:00
2020-04-28 22:02:03 +08:00
return $message;
2020-04-28 16:34:17 +08:00
}
2020-04-29 11:41:54 +08:00
/**
* preview
*
* @time 2020年04月29日
* @param $params
* @return bool|string|string[]
*/
public function preview($params)
2020-04-28 16:34:17 +08:00
{
2020-04-29 11:41:54 +08:00
$params = \json_decode($params['data'], true);
2020-04-28 16:34:17 +08:00
2020-04-29 11:41:54 +08:00
$type = $params['type'];
[$controller, $model] = $this->parseParams($params);
switch ($type) {
case 'controller':
return (new Controller())->getContent($controller);
case 'model':
return (new Model())->getContent($model);
default:
break;
}
2020-04-28 22:02:03 +08:00
}
/**
* parse params
*
* @time 2020年04月28日
* @param $params
* @return array[]
*/
protected function parseParams($params)
{
2020-04-29 11:41:54 +08:00
$module = $params['controller']['module'] ?? false;
if (!$module) {
2020-04-28 22:02:03 +08:00
throw new FailedException('请设置模块');
}
$controller = [
2020-04-29 11:41:54 +08:00
'module' => $module,
2020-04-28 22:02:03 +08:00
'model' => $params['controller']['model'] ?? '',
'controller' => $params['controller']['controller'] ?? '',
'restful' => $params['controller']['restful'],
'other_function' => $params['controller']['other_function'],
];
2020-05-20 11:03:21 +08:00
$table = $params['controller']['table'] ?? '';
if ($table) {
$table = \config('database.connections.mysql.prefix') . $table;
}
2020-04-28 22:02:03 +08:00
$model = [
2020-05-20 11:03:21 +08:00
'table' => $table,
2020-04-29 11:41:54 +08:00
'model' => $params['controller']['model'] ?? '',
2020-04-28 22:02:03 +08:00
'sql' => $params['model']['data'],
'extra' => $params['model']['extra'],
];
return [$controller, $model];
2020-04-28 16:34:17 +08:00
}
2020-07-11 10:59:57 +08:00
/**
* 回滚
*
* @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';
};
2020-07-13 17:05:34 +08:00
$migration = $model->order('version', 'desc')->find();
$model->where('version', $migration->version)->delete();
2020-07-11 10:59:57 +08:00
}
}
2020-04-28 16:34:17 +08:00
}