优化代码生成
This commit is contained in:
parent
f8fb57c9c6
commit
b30e57bf01
@ -10,6 +10,13 @@ use catcher\generate\factory\SQL;
|
|||||||
|
|
||||||
class Generator
|
class Generator
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* generate
|
||||||
|
*
|
||||||
|
* @time 2020年04月29日
|
||||||
|
* @param $params
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function done($params)
|
public function done($params)
|
||||||
{
|
{
|
||||||
$params = \json_decode($params['data'], true);
|
$params = \json_decode($params['data'], true);
|
||||||
@ -44,11 +51,29 @@ class Generator
|
|||||||
return $message;
|
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)
|
protected function parseParams($params)
|
||||||
{
|
{
|
||||||
if (!$params['controller']['module'] ?? false) {
|
$module = $params['controller']['module'] ?? false;
|
||||||
|
|
||||||
|
if (!$module) {
|
||||||
throw new FailedException('请设置模块');
|
throw new FailedException('请设置模块');
|
||||||
}
|
}
|
||||||
|
|
||||||
$controller = [
|
$controller = [
|
||||||
'module' => $params['controller']['module'],
|
'module' => $module,
|
||||||
'model' => $params['controller']['model'] ?? '',
|
'model' => $params['controller']['model'] ?? '',
|
||||||
'controller' => $params['controller']['controller'] ?? '',
|
'controller' => $params['controller']['controller'] ?? '',
|
||||||
'restful' => $params['controller']['restful'],
|
'restful' => $params['controller']['restful'],
|
||||||
@ -74,8 +101,8 @@ class Generator
|
|||||||
];
|
];
|
||||||
|
|
||||||
$model = [
|
$model = [
|
||||||
'table' => $params['controller']['table'],
|
'table' => $params['controller']['table'] ?? '',
|
||||||
'model' => $params['controller']['model'],
|
'model' => $params['controller']['model'] ?? '',
|
||||||
'sql' => $params['model']['data'],
|
'sql' => $params['model']['data'],
|
||||||
'extra' => $params['model']['extra'],
|
'extra' => $params['model']['extra'],
|
||||||
];
|
];
|
||||||
|
@ -35,7 +35,7 @@ class Controller extends Factory
|
|||||||
* @param $params
|
* @param $params
|
||||||
* @return bool|string|string[]
|
* @return bool|string|string[]
|
||||||
*/
|
*/
|
||||||
protected function getContent($params)
|
public function getContent($params)
|
||||||
{
|
{
|
||||||
if (!$params['controller']) {
|
if (!$params['controller']) {
|
||||||
throw new FailedException('params has lost~');
|
throw new FailedException('params has lost~');
|
||||||
|
@ -39,7 +39,7 @@ class Model extends Factory
|
|||||||
[$modelName, $namespace] = $this->parseFilename($params['model']);
|
[$modelName, $namespace] = $this->parseFilename($params['model']);
|
||||||
|
|
||||||
if (!$modelName) {
|
if (!$modelName) {
|
||||||
throw new FailedException('create Model Failed');
|
throw new FailedException('model name not set');
|
||||||
}
|
}
|
||||||
|
|
||||||
$content = $template->useTrait($extra['soft_delete']) .
|
$content = $template->useTrait($extra['soft_delete']) .
|
||||||
|
@ -55,12 +55,13 @@ TMP;
|
|||||||
*/
|
*/
|
||||||
public function name($name)
|
public function name($name)
|
||||||
{
|
{
|
||||||
return <<<TMP
|
if ($name) {
|
||||||
|
return <<<TMP
|
||||||
protected \$name = '{$name}';
|
protected \$name = '{$name}';
|
||||||
|
|
||||||
|
|
||||||
TMP;
|
TMP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user