代码生成器
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
use JaguarJack\Generator\Template\Controller as Template;
|
||||
use catcher\generate\template\Controller as Template;
|
||||
use think\helper\Str;
|
||||
|
||||
class Controller extends Factory
|
||||
@@ -16,9 +16,29 @@ class Controller extends Factory
|
||||
* @return bool|string|string[]
|
||||
*/
|
||||
public function done($params)
|
||||
{
|
||||
// 写入成功之后
|
||||
if (file_put_contents($this->getGeneratePath($params['controller']), $this->getContent($params))) {
|
||||
return (new Route())->controller($params['controller'])
|
||||
->restful($params['restful'])
|
||||
->methods($this->parseOtherMethods($params['other_function']))
|
||||
->done();
|
||||
}
|
||||
|
||||
throw new FailedException($params['controller'] . ' generate failed~');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取内容
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $params
|
||||
* @return bool|string|string[]
|
||||
*/
|
||||
protected function getContent($params)
|
||||
{
|
||||
if (!$params['controller']) {
|
||||
return false;
|
||||
throw new FailedException('params has lost~');
|
||||
}
|
||||
|
||||
$template = new Template();
|
||||
@@ -30,23 +50,30 @@ class Controller extends Factory
|
||||
|
||||
// parse model
|
||||
[$model, $modelNamespace] = $this->parseFilename($params['model']);
|
||||
|
||||
$use = implode(';',[
|
||||
'use ' . $params['model'] .' as '. $model . 'Model',
|
||||
]) . ';';
|
||||
|
||||
$content = $template->header() .
|
||||
$template->nameSpace($namespace) .
|
||||
str_replace('{USE}', $model ? 'use ' . $params['model'] .';' : '', $template->uses()) .
|
||||
$template->createClass($className);
|
||||
$template->nameSpace($namespace) .
|
||||
str_replace('{USE}', $model ? $use : '', $template->uses()) .
|
||||
$template->createClass($className);
|
||||
|
||||
|
||||
$content = str_replace('{CONTENT}', ($model ? $template->construct($model) : '') . rtrim($this->content($params, $template), "\r\n"), $content);
|
||||
|
||||
// 写入成功之后
|
||||
if (file_put_contents($this->getGeneratePath($params['controller']), $content)) {
|
||||
(new Route())->controller($params['controller'])
|
||||
->restful($params['restful'])
|
||||
->methods($this->parseOtherMethods($params['other_function']))
|
||||
->done();
|
||||
}
|
||||
return str_replace('{CONTENT}', ($model ? $template->construct($model.'Model') : '') . rtrim($this->content($params, $template), "\r\n"), $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse use
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $params
|
||||
* @return string
|
||||
*/
|
||||
protected function parseUse($params)
|
||||
{
|
||||
|
||||
}
|
||||
/**
|
||||
* content
|
||||
*
|
||||
@@ -77,25 +104,6 @@ class Controller extends Factory
|
||||
|
||||
return $content;
|
||||
}
|
||||
/**
|
||||
* parse filename
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $filename
|
||||
* @return array
|
||||
*/
|
||||
public function parseFilename($filename)
|
||||
{
|
||||
$namespace = explode('\\', $filename);
|
||||
|
||||
$className = ucfirst(array_pop($namespace));
|
||||
|
||||
$namespace = implode('\\', $namespace);
|
||||
|
||||
return [$className, $namespace];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* parse $method
|
||||
|
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use catcher\CatchAdmin;
|
||||
|
||||
@@ -63,4 +63,22 @@ abstract class Factory
|
||||
|
||||
return root_path() . $psr4[$projectRootNamespace.'\\'] . DIRECTORY_SEPARATOR. $module . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse filename
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $filename
|
||||
* @return array
|
||||
*/
|
||||
public function parseFilename($filename)
|
||||
{
|
||||
$namespace = explode('\\', $filename);
|
||||
|
||||
$className = ucfirst(array_pop($namespace));
|
||||
|
||||
$namespace = implode('\\', $namespace);
|
||||
|
||||
return [$className, $namespace];
|
||||
}
|
||||
}
|
39
extend/catcher/generate/factory/Migration.php
Normal file
39
extend/catcher/generate/factory/Migration.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use catcher\CatchAdmin;
|
||||
use catcher\exceptions\FailedException;
|
||||
use JaguarJack\MigrateGenerator\MigrateGenerator;
|
||||
|
||||
|
||||
class Migration extends Factory
|
||||
{
|
||||
public function done($params)
|
||||
{
|
||||
[$module, $tableName] = $params;
|
||||
|
||||
// TODO: Implement done() method.
|
||||
$migrationPath = CatchAdmin::directory() . $module . DIRECTORY_SEPARATOR.
|
||||
'database' . DIRECTORY_SEPARATOR . 'migration' .DIRECTORY_SEPARATOR;
|
||||
|
||||
CatchAdmin::makeDirectory($migrationPath);
|
||||
|
||||
$migrateGenerator = (new MigrateGenerator('thinkphp'));
|
||||
|
||||
$tables = $migrateGenerator->getDatabase()->getAllTables($tableName);
|
||||
|
||||
$file = $migrationPath . date('YmdHis') . '_'. $tableName . '.php';
|
||||
|
||||
foreach ($tables as $table) {
|
||||
if ($table->getName() == $tableName) {
|
||||
file_put_contents($file, $migrateGenerator->getMigrationContent($table));
|
||||
if (!file_exists($file)) {
|
||||
throw new FailedException('migration generate failed');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -1 +1,71 @@
|
||||
<?php
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\generate\template\Model as Template;
|
||||
use think\facade\Db;
|
||||
|
||||
class Model extends Factory
|
||||
{
|
||||
public function done($params)
|
||||
{
|
||||
// TODO: Implement done() method.
|
||||
$template = new Template();
|
||||
|
||||
$extra = $params['extra'];
|
||||
|
||||
$table = $params['table'];
|
||||
|
||||
[$modelName, $namespace] = $this->parseFilename($params['model']);
|
||||
|
||||
if (!$modelName) {
|
||||
throw new FailedException('create Model Failed');
|
||||
}
|
||||
|
||||
$content = $template->useTrait($extra['soft_delete']) .
|
||||
$template->name($table) .
|
||||
$template->field($this->parseField($table));
|
||||
|
||||
$class = $template->header() .
|
||||
$template->nameSpace($namespace) .
|
||||
$template->uses($extra['soft_delete']) .
|
||||
$template->createModel($modelName, $table);
|
||||
|
||||
|
||||
$file = $this->getGeneratePath($params['model']);
|
||||
|
||||
file_put_contents($file, str_replace('{CONTENT}', $content, $class));
|
||||
|
||||
if (!file_exists($file)) {
|
||||
throw new FailedException('create model failed');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* parse field
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $table
|
||||
* @return string
|
||||
*/
|
||||
protected function parseField($table)
|
||||
{
|
||||
$columns = Db::query('show full columns from ' .
|
||||
config('database.connections.mysql.prefix') . $table);
|
||||
|
||||
$new = [];
|
||||
foreach ($columns as $field) {
|
||||
$new[$field['Field']] = $field['Comment'];
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
foreach ($new as $field => $comment) {
|
||||
$fields[] = sprintf("'%s', // %s", $field, $comment);
|
||||
}
|
||||
|
||||
return implode("\r\n\t\t", $fields);
|
||||
}
|
||||
}
|
@@ -1 +0,0 @@
|
||||
<?php
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
use JaguarJack\Generator\Template\Content;
|
||||
use catcher\generate\template\Content;
|
||||
|
||||
class Route extends Factory
|
||||
{
|
||||
|
@@ -1,7 +1,10 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
namespace catcher\generate\factory;
|
||||
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
use think\facade\Db;
|
||||
|
||||
class SQL
|
||||
{
|
||||
protected $index = '';
|
||||
@@ -9,19 +12,43 @@ class SQL
|
||||
|
||||
public function done($params)
|
||||
{
|
||||
if ($params['controller']['table'] ?? false) {
|
||||
return false;
|
||||
|
||||
//dd($this->createSQL($params));
|
||||
Db::execute($this->createSQL($params));
|
||||
|
||||
// 判断表是否创建成功
|
||||
if (!$this->hasTableExists($params['table'])) {
|
||||
throw new FailedException(sprintf('create table [%s] failed',$params['table']));
|
||||
}
|
||||
|
||||
$table = \config('database.connections.mysql.prefix') . $params['controller']['table'];
|
||||
return true;
|
||||
}
|
||||
|
||||
$extra = $params['model']['extra'];
|
||||
/**
|
||||
* create table sql
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $params
|
||||
* @return string
|
||||
*/
|
||||
protected function createSQL($params)
|
||||
{
|
||||
if (!$params['table'] ?? false) {
|
||||
throw new FailedException('table name has lost~');
|
||||
}
|
||||
|
||||
$table = \config('database.connections.mysql.prefix') . $params['table'];
|
||||
|
||||
if ($this->hasTableExists($table)) {
|
||||
throw new FailedException(sprintf('table [%s] has existed', $params['table']));
|
||||
}
|
||||
|
||||
$extra = $params['extra'];
|
||||
// 主键
|
||||
$createSql = $this->primaryKey($extra['primary_key']);
|
||||
// 字段
|
||||
$ifHaveNotFields = true;
|
||||
foreach ($params['model']['data'] as $sql) {
|
||||
foreach ($params['sql'] as $sql) {
|
||||
if (!$sql['field'] || !$sql['type']) {
|
||||
continue;
|
||||
}
|
||||
@@ -30,7 +57,7 @@ class SQL
|
||||
}
|
||||
// 如果没有设置数据库字段
|
||||
if ($ifHaveNotFields) {
|
||||
return false;
|
||||
throw new FailedException('Do you have set mysql fields?');
|
||||
}
|
||||
// 创建时间
|
||||
if ($extra['created_at'] ?? false) {
|
||||
@@ -44,11 +71,11 @@ class SQL
|
||||
if ($this->index) {
|
||||
$createSql .= $this->index;
|
||||
}
|
||||
$createSql = rtrim($createSql, ',' . PHP_EOL);
|
||||
// 创建表 SQL
|
||||
return $this->createTable($table, $createSql, $extra['engine'], 'utf8mb4', $extra['comment']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* parse sql
|
||||
*
|
||||
@@ -69,7 +96,7 @@ class SQL
|
||||
$sql['type'],
|
||||
$sql['length'] ? sprintf('(%s)', $sql['length']) : '',
|
||||
$sql['unsigned'] ? 'unsigned' : '',
|
||||
$sql['default'] ? 'default ' . $sql['default']: '',
|
||||
$sql['default'] ?? '',
|
||||
$sql['nullable'] ? 'not null' : '',
|
||||
$sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : ''
|
||||
]) . ','. PHP_EOL;
|
||||
@@ -139,13 +166,26 @@ class SQL
|
||||
protected function parseIndex($index, $field)
|
||||
{
|
||||
if ($index == 'unique') {
|
||||
$this->index .= "unique index unique_ . $field($field)";
|
||||
$this->index .= "unique index unique_$field($field)," . PHP_EOL;
|
||||
} elseif ($index == 'index') {
|
||||
$this->index .= "index($field)";
|
||||
$this->index .= "index($field),". PHP_EOL;
|
||||
} elseif ($index == 'fulltext') {
|
||||
$this->index .= "fulltext key fulltext_ .$field($field)";
|
||||
$this->index .= "fulltext key fulltext_$field($field)," . PHP_EOL;
|
||||
} elseif ($index == 'spatial') {
|
||||
$this->index .= "spatial index spatial_.$field($field)";
|
||||
$this->index .= "spatial index spatial_$field($field),". PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $table
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasTableExists($table)
|
||||
{
|
||||
$tables = Db::getConnection()->getTables();
|
||||
|
||||
return in_array($table, $tables);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user