优化代码生成

This commit is contained in:
JaguarJack 2020-05-05 21:52:44 +08:00
parent 635f3e00d0
commit 72eb174946
3 changed files with 36 additions and 15 deletions

View File

@ -10,11 +10,11 @@ class Model extends Factory
{ {
public function done($params) public function done($params)
{ {
$file = $this->getGeneratePath($params['model']); $content = $this->getContent($params);
file_put_contents($file, $this->getContent($params)); file_put_contents($this->getGeneratePath($params['model']), $content);
if (!file_exists($file)) { if (!file_exists($this->getGeneratePath($params['model']))) {
throw new FailedException('create model failed'); throw new FailedException('create model failed');
} }
@ -28,7 +28,7 @@ class Model extends Factory
* @param $params * @param $params
* @return string|string[] * @return string|string[]
*/ */
public function getContent($params) public function getContent(&$params)
{ {
// TODO: Implement done() method. // TODO: Implement done() method.
$template = new Template(); $template = new Template();
@ -41,7 +41,8 @@ class Model extends Factory
// 如果填写了表名并且没有填写模型名称 使用表名作为模型名称 // 如果填写了表名并且没有填写模型名称 使用表名作为模型名称
if (!$modelName && $table) { if (!$modelName && $table) {
$modelName = Str::camel($table); $modelName = ucfirst(Str::camel($table));
$params['model'] = $params['model'] . $modelName;
} }
if (!$modelName) { if (!$modelName) {

View File

@ -29,8 +29,13 @@ class Route extends Factory
} }
} }
return file_put_contents($this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php', $router = $this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php';
$this->header() . implode(';'. PHP_EOL , $route) . ';');
if (file_exists($router)) {
return file_put_contents($router, PHP_EOL . implode(';'. PHP_EOL , $route) . ';', FILE_APPEND);
}
return file_put_contents($router, $this->header() . implode(';'. PHP_EOL , $route) . ';');
} }
/** /**

View File

@ -12,6 +12,7 @@ class SQL extends Factory
public function done($params) public function done($params)
{ {
//dd($this->createSQL($params));
Db::execute($this->createSQL($params)); Db::execute($this->createSQL($params));
// 判断表是否创建成功 // 判断表是否创建成功
@ -89,14 +90,28 @@ class SQL extends Factory
$this->parseIndex($sql['index'], $sql['field']); $this->parseIndex($sql['index'], $sql['field']);
} }
return implode(' ', [ // 字段
sprintf('`%s`', $sql['field']), $_sql[] = sprintf('`%s`', $sql['field']);
$sql['type'] . ($sql['length'] ? sprintf('(%s)', $sql['length']) : ''), // 类型
$sql['unsigned'] ? 'unsigned' : '', $_sql[] = $sql['type'] . ($sql['length'] ? sprintf('(%s)', $sql['length']) : '');
'default ' . $sql['default'],
$sql['nullable'] ? 'not null' : '', if ($sql['unsigned']) {
$sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : '' $_sql[] = 'unsigned';
]) . ','. PHP_EOL; }
// 默认值
if (!$sql['nullable']) {
$_sql[] = 'not null';
if (!$sql['default']) {
$_sql[] = ' default \'\'';
} else {
$_sql[] = ' default ' . $sql['default'];
}
}
// 字段注释
$_sql[] = $sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : '';
return implode(' ', $_sql) . ','. PHP_EOL;
} }
/** /**