优化代码生成

This commit is contained in:
JaguarJack 2020-04-29 11:41:54 +08:00
parent f8fb57c9c6
commit b30e57bf01
4 changed files with 40 additions and 12 deletions

View File

@ -10,6 +10,13 @@ use catcher\generate\factory\SQL;
class Generator
{
/**
* generate
*
* @time 2020年04月29日
* @param $params
* @return array
*/
public function done($params)
{
$params = \json_decode($params['data'], true);
@ -44,11 +51,29 @@ class Generator
return $message;
}
public function preview($type, $params)
/**
* preview
*
* @time 2020年04月29日
* @param $params
* @return bool|string|string[]
*/
public function preview($params)
{
$class = ucfirst($type);
$params = \json_decode($params['data'], true);
return (new $class)->getContent($params);
$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;
}
}
@ -61,12 +86,14 @@ class Generator
*/
protected function parseParams($params)
{
if (!$params['controller']['module'] ?? false) {
$module = $params['controller']['module'] ?? false;
if (!$module) {
throw new FailedException('请设置模块');
}
$controller = [
'module' => $params['controller']['module'],
'module' => $module,
'model' => $params['controller']['model'] ?? '',
'controller' => $params['controller']['controller'] ?? '',
'restful' => $params['controller']['restful'],
@ -74,8 +101,8 @@ class Generator
];
$model = [
'table' => $params['controller']['table'],
'model' => $params['controller']['model'],
'table' => $params['controller']['table'] ?? '',
'model' => $params['controller']['model'] ?? '',
'sql' => $params['model']['data'],
'extra' => $params['model']['extra'],
];

View File

@ -35,7 +35,7 @@ class Controller extends Factory
* @param $params
* @return bool|string|string[]
*/
protected function getContent($params)
public function getContent($params)
{
if (!$params['controller']) {
throw new FailedException('params has lost');

View File

@ -39,7 +39,7 @@ class Model extends Factory
[$modelName, $namespace] = $this->parseFilename($params['model']);
if (!$modelName) {
throw new FailedException('create Model Failed');
throw new FailedException('model name not set');
}
$content = $template->useTrait($extra['soft_delete']) .

View File

@ -55,12 +55,13 @@ TMP;
*/
public function name($name)
{
if ($name) {
return <<<TMP
protected \$name = '{$name}';
TMP;
}
}
/**