87 lines
2.2 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;
use catcher\generate\factory\SQL;
2020-04-28 16:34:17 +08:00
class Generator
{
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
}
public function preview($type, $params)
{
$class = ucfirst($type);
2020-04-28 22:02:03 +08:00
(new $class)->done($params);
}
/**
* parse params
*
* @time 2020年04月28日
* @param $params
* @return array[]
*/
protected function parseParams($params)
{
if (!$params['controller']['module'] ?? false) {
throw new FailedException('请设置模块');
}
$controller = [
'module' => $params['controller']['module'],
'model' => $params['controller']['model'] ?? '',
'controller' => $params['controller']['controller'] ?? '',
'restful' => $params['controller']['restful'],
'other_function' => $params['controller']['other_function'],
];
$model = [
'table' => $params['controller']['table'],
'model' => $params['controller']['model'],
'sql' => $params['model']['data'],
'extra' => $params['model']['extra'],
];
return [$controller, $model];
2020-04-28 16:34:17 +08:00
}
}