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;
|
|
|
|
use catcher\generate\factory\SQL;
|
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 = [];
|
|
|
|
if ($params['create_controller']) {
|
|
|
|
if ((new Controller)->done($controller)) {
|
|
|
|
array_push($message, 'controller created successfully');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($params['create_table']) {
|
|
|
|
if ((new SQL)->done($model)) {
|
|
|
|
array_push($message, 'table created successfully');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($params['create_model']) {
|
|
|
|
if ((new Model)->done($model)) {
|
|
|
|
array_push($message, 'model created successfully');
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 16:34:17 +08:00
|
|
|
|
2020-04-28 22:02:03 +08:00
|
|
|
if ($params['create_migration']) {
|
|
|
|
if ((new Migration)->done([$controller['module'], $model['table']])) {
|
|
|
|
array_push($message, 'migration created successfully');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$model = [
|
2020-04-29 11:41:54 +08:00
|
|
|
'table' => $params['controller']['table'] ?? '',
|
|
|
|
'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
|
|
|
}
|
|
|
|
}
|