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;
|
2021-03-14 07:41:15 +08:00
|
|
|
use catcher\generate\support\Table;
|
2020-11-19 17:31:31 +08:00
|
|
|
use catcher\library\Composer;
|
2021-03-14 07:41:15 +08:00
|
|
|
use catcher\Utils;
|
2020-04-28 16:34:17 +08:00
|
|
|
|
|
|
|
class Generator
|
|
|
|
{
|
2020-11-19 17:31:31 +08:00
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
const NEED_PACKAGE = 'jaguarjack/file-generate';
|
2020-11-19 17:31:31 +08:00
|
|
|
|
2020-04-29 11:41:54 +08:00
|
|
|
/**
|
|
|
|
* generate
|
|
|
|
*
|
|
|
|
* @time 2020年04月29日
|
|
|
|
* @param $params
|
|
|
|
* @return array
|
2021-03-14 07:41:15 +08:00
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
2020-04-29 11:41:54 +08:00
|
|
|
*/
|
2021-03-14 07:41:15 +08:00
|
|
|
public function done($params): array
|
2020-04-28 16:34:17 +08:00
|
|
|
{
|
2020-11-19 17:31:31 +08:00
|
|
|
// 判断是否安装了扩展包
|
|
|
|
if (!(new Composer)->hasPackage(self::NEED_PACKAGE)) {
|
|
|
|
throw new FailedException(
|
2021-06-10 19:38:44 +08:00
|
|
|
sprintf('you must use [ composer require --dev %s:dev-master]', self::NEED_PACKAGE)
|
2020-11-19 17:31:31 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:34:17 +08:00
|
|
|
$params = \json_decode($params['data'], true);
|
|
|
|
|
2020-04-28 22:02:03 +08:00
|
|
|
[$controller, $model] = $this->parseParams($params);
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
$message = $files = [];
|
2020-07-11 10:59:57 +08:00
|
|
|
|
|
|
|
$migration = '';
|
|
|
|
|
|
|
|
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']) {
|
2021-03-14 07:41:15 +08:00
|
|
|
(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
|
|
|
|
2020-09-17 21:15:24 +08:00
|
|
|
// 只有创建了 Controller 最后成功才写入 route
|
|
|
|
if ($params['create_controller']) {
|
2021-03-14 07:41:15 +08:00
|
|
|
(new Route)->controller($controller['controller'])
|
2020-09-17 21:15:24 +08:00
|
|
|
->restful($controller['restful'])
|
|
|
|
->done();
|
|
|
|
}
|
2021-03-14 07:41:15 +08:00
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
if (!$exception instanceof TableExistException) {
|
|
|
|
$this->rollback($files, $migration);
|
|
|
|
}
|
2020-07-13 17:05:34 +08:00
|
|
|
|
2021-03-14 07:41:15 +08:00
|
|
|
throw new FailedException($exception->getMessage());
|
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[]
|
2021-06-10 19:38:44 +08:00
|
|
|
* @throws \JaguarJack\Generate\Exceptions\TypeNotFoundException
|
2020-04-29 11:41:54 +08:00
|
|
|
*/
|
|
|
|
public function preview($params)
|
2020-04-28 16:34:17 +08:00
|
|
|
{
|
2020-09-04 19:28:30 +08:00
|
|
|
$params = \json_decode($params['data'], true);
|
|
|
|
|
2020-04-29 11:41:54 +08:00
|
|
|
[$controller, $model] = $this->parseParams($params);
|
|
|
|
|
2021-06-10 19:38:44 +08:00
|
|
|
switch ($params['type']) {
|
2020-04-29 11:41:54 +08:00
|
|
|
case 'controller':
|
2021-03-14 07:41:15 +08:00
|
|
|
return (new Controller)->getContent($controller);
|
2020-04-29 11:41:54 +08:00
|
|
|
case 'model':
|
2021-03-14 07:41:15 +08:00
|
|
|
return (new Model)->getContent($model);
|
2020-04-29 11:41:54 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-04-28 22:02:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse params
|
|
|
|
*
|
|
|
|
* @time 2020年04月28日
|
|
|
|
* @param $params
|
|
|
|
* @return array[]
|
|
|
|
*/
|
2021-03-14 07:41:15 +08:00
|
|
|
protected function parseParams($params): array
|
2020-04-28 22:02:03 +08:00
|
|
|
{
|
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'],
|
|
|
|
];
|
|
|
|
|
2020-05-20 11:03:21 +08:00
|
|
|
$table = $params['controller']['table'] ?? '';
|
|
|
|
|
2021-03-14 07:41:15 +08:00
|
|
|
if ($table) {
|
|
|
|
$table = Utils::tableWithPrefix($table);
|
2020-05-20 11:03:21 +08:00
|
|
|
}
|
2021-03-14 07:41:15 +08:00
|
|
|
|
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-09-04 19:28:30 +08:00
|
|
|
'sql' => $params['table_fields'],
|
|
|
|
'extra' => $params['table_extra'],
|
2020-04-28 22:02:03 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
return [$controller, $model];
|
2020-04-28 16:34:17 +08:00
|
|
|
}
|
2020-07-11 10:59:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 回滚
|
|
|
|
*
|
|
|
|
* @param $files
|
|
|
|
* @param $migration
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
*/
|
2021-03-14 07:41:15 +08:00
|
|
|
protected function rollback($files, $migration)
|
2020-07-11 10:59:57 +08:00
|
|
|
{
|
2021-03-14 07:41:15 +08:00
|
|
|
if (Table::exist()) {
|
|
|
|
Table::drop();
|
2020-07-11 10:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|