This commit is contained in:
2023-10-08 22:16:32 +08:00
parent ce9d172016
commit 8278d62c10
+384 -352
View File
@@ -15,78 +15,80 @@ use ReflectionException;
class GiiController extends GiiBase class GiiController extends GiiBase
{ {
public string $className = ''; public string $className = '';
public array $fields = []; public array $fields = [];
/** /**
* GiiController constructor. * GiiController constructor.
* @param $className * @param $className
* @param $fields * @param $fields
*/ */
public function __construct($className, $fields, $tableName) public function __construct($className, $fields, $tableName)
{ {
$this->className = $className; $this->className = $className;
$this->fields = $fields; $this->fields = $fields;
$this->tableName = $tableName; $this->tableName = $tableName;
} }
/** /**
* @return string|bool * @return string|bool
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function generate(): string|bool public function generate(): string|bool
{ {
$path = $this->getControllerPath(); $path = $this->getControllerPath();
$modelPath = $this->getModelPath(); $modelPath = $this->getModelPath();
$managerName = $this->className; $managerName = $this->className;
$namespace = rtrim($path['namespace'], '\\'); $namespace = rtrim($path['namespace'], '\\');
$model_namespace = rtrim($modelPath['namespace'], '\\'); $model_namespace = rtrim($modelPath['namespace'], '\\');
$class = ''; $class = '';
$controller = str_replace('\\\\', '\\', "$namespace\\{$managerName}Controller"); $controller = str_replace('\\\\', '\\', "$namespace\\{$managerName}Controller");
$html = "<?php $html = "<?php
namespace {$namespace}; namespace {$namespace};
"; ";
if (file_exists($path['path'] . '/' . $managerName . 'Controller.php')) { if (file_exists($path['path'] . '/' . $managerName . 'Controller.php')) {
try { try {
$class = new \ReflectionClass($controller); $class = new \ReflectionClass($controller);
$import = $this->getImports($path['path'] . '/' . $managerName . 'Controller.php', $class); $import = $this->getImports($path['path'] . '/' . $managerName . 'Controller.php', $class);
} catch (\Throwable $Exception) { } catch (\Throwable $Exception) {
error($Exception); error($Exception);
exit(); exit();
} }
} else { } else {
$import = "use Exception; $import = "use Exception;
use Kiri\Router\Annotate\Post;
use Kiri\Router\Annotate\Get;
use Kiri\Core\Str; use Kiri\Core\Str;
use Kiri\Core\Json;
use Kiri\Router\Base\Controller; use Kiri\Router\Base\Controller;
use Kiri\Core\Json;
use {$model_namespace}\\{$managerName}; use {$model_namespace}\\{$managerName};
use Kiri\Router\Validator\Validator; use Kiri\Router\Validator\Validator;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
"; ";
} }
if (!empty($import)) { if (!empty($import)) {
$html .= $import; $html .= $import;
} }
$controllerName = $managerName; $controllerName = $managerName;
$historyModel = "use {$model_namespace}\\{$managerName};"; $historyModel = "use {$model_namespace}\\{$managerName};";
if (!str_contains($html, $historyModel)) { if (!str_contains($html, $historyModel)) {
$html .= $historyModel; $html .= $historyModel;
} }
$html .= " $html .= "
/** /**
* Class {$controllerName}Controller * Class {$controllerName}Controller
@@ -99,115 +101,117 @@ class {$controllerName}Controller extends Controller
"; ";
$funcNames = []; $funcNames = [];
if (is_object($class)) { if (is_object($class)) {
$html .= $this->getClassProperty($class); $html .= $this->getClassProperty($class);
$html .= $this->getClassMethods($class); $html .= $this->getClassMethods($class);
} }
$default = ['actionAdd', 'actionUpdate', 'actionAuditing', 'actionBatchAuditing', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList']; $default = ['actionAdd', 'actionUpdate', 'actionAuditing', 'actionBatchAuditing', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList'];
$tableName = str_replace($this->db->tablePrefix, '', $this->tableName);
$tableName = str_replace('_', '-', $tableName);
foreach ($default as $key => $val) { foreach ($default as $key => $val) {
if (str_contains($html, ' function ' . $val . '(')) { if (str_contains($html, ' function ' . $val . '(')) {
continue; continue;
} }
$html .= $this->{'controllerMethod' . str_replace('action', '', $val)}($this->fields, $managerName, $managerName, $path) . "\n"; $html .= $this->{'controllerMethod' . str_replace('action', '', $val)}($this->fields, $managerName, $managerName, $path, $tableName) . "\n";
} }
$html .= ' $html .= '
}'; }';
$file = APP_PATH . 'routes/' . $this->input->getOption('database') . '.php'; // $file = APP_PATH . 'routes/' . $this->input->getOption('database') . '.php';
if (!file_exists($file)) { // if (!file_exists($file)) {
touch($file); // touch($file);
file_put_contents($file, '<?php' . PHP_EOL); // file_put_contents($file, '<?php' . PHP_EOL);
file_put_contents($file, PHP_EOL, FILE_APPEND); // file_put_contents($file, PHP_EOL, FILE_APPEND);
file_put_contents($file, PHP_EOL, FILE_APPEND); // file_put_contents($file, PHP_EOL, FILE_APPEND);
file_put_contents($file, 'use Kiri\Message\Handler\Router;' . PHP_EOL, FILE_APPEND); // file_put_contents($file, 'use Kiri\Message\Handler\Router;' . PHP_EOL, FILE_APPEND);
file_put_contents($file, PHP_EOL, FILE_APPEND); // file_put_contents($file, PHP_EOL, FILE_APPEND);
file_put_contents($file, PHP_EOL, FILE_APPEND); // file_put_contents($file, PHP_EOL, FILE_APPEND);
} // }
//
//
// $addRouter = 'Router::group([\'prefix\' => \'' . $tableName . '\',\'namespace\' => \'' . $namespace . '\'], function () {
// Router::post(\'add\', \'' . $controllerName . 'Controller@actionAdd\');
// Router::get(\'list\', \'' . $controllerName . 'Controller@actionList\');
// Router::post(\'update\', \'' . $controllerName . 'Controller@actionUpdate\');
// Router::post(\'auditing\', \'' . $controllerName . 'Controller@actionAuditing\');
// Router::post(\'batch-auditing\', \'' . $controllerName . 'Controller@actionBatchAuditing\');
// Router::post(\'batch-delete\', \'' . $controllerName . 'Controller@actionBatchDelete\');
// Router::post(\'delete\', \'' . $controllerName . 'Controller@actionDelete\');
// Router::get(\'detail\', \'' . $controllerName . 'Controller@actionDetail\');
//});
//';
// if (!str_contains($this->clearBlank(file_get_contents($file)), $this->clearBlank($addRouter))) {
// file_put_contents($file, $addRouter, FILE_APPEND);
// }
$tableName = str_replace($this->db->tablePrefix, '', $this->tableName); $file = $path['path'] . '/' . $controllerName . 'Controller.php';
$tableName = str_replace('_', '-', $tableName); if (file_exists($file)) {
unlink($file);
}
$addRouter = 'Router::group([\'prefix\' => \'' . $tableName . '\',\'namespace\' => \'' . $namespace . '\'], function () { Kiri::writeFile($file, $html);
Router::post(\'add\', \'' . $controllerName . 'Controller@actionAdd\'); return $controllerName . 'Controller.php';
Router::get(\'list\', \'' . $controllerName . 'Controller@actionList\'); }
Router::post(\'update\', \'' . $controllerName . 'Controller@actionUpdate\');
Router::post(\'auditing\', \'' . $controllerName . 'Controller@actionAuditing\');
Router::post(\'batch-auditing\', \'' . $controllerName . 'Controller@actionBatchAuditing\');
Router::post(\'batch-delete\', \'' . $controllerName . 'Controller@actionBatchDelete\');
Router::post(\'delete\', \'' . $controllerName . 'Controller@actionDelete\');
Router::get(\'detail\', \'' . $controllerName . 'Controller@actionDetail\');
});
';
if (!str_contains($this->clearBlank(file_get_contents($file)), $this->clearBlank($addRouter))) {
file_put_contents($file, $addRouter, FILE_APPEND);
}
$file = $path['path'] . '/' . $controllerName . 'Controller.php';
if (file_exists($file)) {
unlink($file);
}
Kiri::writeFile($file, $html);
return $controllerName . 'Controller.php';
}
/** /**
* @return array * @return array
*/ */
private function getControllerPath(): array private function getControllerPath(): array
{ {
$dbName = $this->db->id; $dbName = $this->db->id;
if (empty($dbName) || $dbName == 'db') { if (empty($dbName) || $dbName == 'db') {
$dbName = ''; $dbName = '';
} }
$module = empty($this->module) ? '' : $this->module; $module = empty($this->module) ? '' : $this->module;
$modelPath['namespace'] = $this->controllerNamespace . $module; $modelPath['namespace'] = $this->controllerNamespace . $module;
$modelPath['path'] = $this->controllerPath . $module; $modelPath['path'] = $this->controllerPath . $module;
if (!is_dir($modelPath['path'])) { if (!is_dir($modelPath['path'])) {
mkdir($modelPath['path']); mkdir($modelPath['path']);
} }
if (!empty($dbName)) { if (!empty($dbName)) {
$modelPath['namespace'] = $this->controllerNamespace . ucfirst($dbName); $modelPath['namespace'] = $this->controllerNamespace . ucfirst($dbName);
$modelPath['path'] = $this->controllerPath . ucfirst($dbName); $modelPath['path'] = $this->controllerPath . ucfirst($dbName);
} }
$modelPath['namespace'] = rtrim($modelPath['namespace'], '\\'); $modelPath['namespace'] = rtrim($modelPath['namespace'], '\\');
$modelPath['path'] = rtrim($modelPath['path'], '\\'); $modelPath['path'] = rtrim($modelPath['path'], '\\');
if (!is_dir($modelPath['path'])) { if (!is_dir($modelPath['path'])) {
mkdir($modelPath['path']); mkdir($modelPath['path']);
} }
return $modelPath; return $modelPath;
} }
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
* @param null $object * @param null $object
* @param $path * @param $path
* @return string * @param string $tableName
* 新增 * @return string
*/ * 新增
public function controllerMethodAdd($fields, $className, $object, $path): string */
{ public function controllerMethodAdd($fields, $className, $object, $path, string $tableName): string
$_path = str_replace(CONTROLLER_PATH, '', $path['path']); {
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
// $this->getData($path, $className, $fields); // $this->getData($path, $className, $fields);
return ' return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Post(\'' . $this->db->database . '/' . $tableName . '/add\')]
public function actionAdd(): ResponseInterface public function actionAdd(): ResponseInterface
{ {
$model = new ' . $className . '(); $model = new ' . $className . '();
@@ -218,16 +222,25 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]); return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]);
} }
}'; }';
} }
public function controllerMethodAuditing($fields, $className, $object, $path): string /**
{ * @param $fields
return ' * @param $className
* @param $object
* @param $path
* @param string $tableName
* @return string
*/
public function controllerMethodAuditing($fields, $className, $object, $path, string $tableName): string
{
return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Post(\'' . $this->db->database . '/' . $tableName . '/auditing\')]
public function actionAuditing(): ResponseInterface public function actionAuditing(): ResponseInterface
{ {
$model = ' . $className . '::findOne($this->request->post(\'id\', 0)); $model = ' . $className . '::findOne($this->request->post(\'id\', 0));
@@ -240,16 +253,25 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]); return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]);
} }
}'; }';
} }
public function controllerMethodBatchAuditing($fields, $className, $object, $path): string /**
{ * @param $fields
return ' * @param $className
* @param $object
* @param $path
* @param string $tableName
* @return string
*/
public function controllerMethodBatchAuditing($fields, $className, $object, $path, string $tableName): string
{
return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Post(\'' . $this->db->database . '/' . $tableName . '/batch/auditing\')]
public function actionBatchAuditing(): ResponseInterface public function actionBatchAuditing(): ResponseInterface
{ {
$ids = $this->request->post(\'ids\', []); $ids = $this->request->post(\'ids\', []);
@@ -262,29 +284,31 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'message\' => \'ok\']); return $this->response->json([\'code\' => 0, \'message\' => \'ok\']);
} }
}'; }';
} }
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
* @param null $object * @param null $object
* @param array $path * @param array $path
* @return string * @param string $tableName
* 构建更新 * @return string
*/ * 构建更新
public function controllerMethodUpdate($fields, $className, $object = NULL, $path = []): string */
{ public function controllerMethodUpdate($fields, $className, $object = NULL, array $path = [], string $tableName = ''): string
$_path = str_replace(CONTROLLER_PATH, '', $path['path']); {
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
return ' return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Post(\'' . $this->db->database . '/' . $tableName . '/update\')]
public function actionUpdate(): ResponseInterface public function actionUpdate(): ResponseInterface
{ {
$model = ' . $className . '::findOne($this->request->post(\'id\', 0)); $model = ' . $className . '::findOne($this->request->post(\'id\', 0));
@@ -298,28 +322,30 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]); return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]);
} }
}'; }';
} }
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
* @param null $object * @param null $object
* @param array $path * @param array $path
* @return string * @param string $tableName
* 构建更新 * @return string
*/ * 构建更新
public function controllerMethodBatchDelete($fields, $className, $object = NULL, array $path = []): string */
{ public function controllerMethodBatchDelete($fields, $className, $object = NULL, array $path = [], string $tableName = ''): string
$_path = str_replace(CONTROLLER_PATH, '', $path['path']); {
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
return ' return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Post(\'' . $this->db->database . '/' . $tableName . '/batch/delete\')]
public function actionBatchDelete(): ResponseInterface public function actionBatchDelete(): ResponseInterface
{ {
$_key = $this->request->post(\'ids\', []); $_key = $this->request->post(\'ids\', []);
@@ -334,28 +360,30 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'param\' => $_key]); return $this->response->json([\'code\' => 0, \'param\' => $_key]);
} }
}'; }';
} }
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
* @param $managerName * @param $managerName
* @param array $path * @param array $path
* @return string * @param string $tableName
* 构建详情 * @return string
*/ * 构建详情
public function controllerMethodDetail($fields, $className, $managerName, $path = []): string */
{ public function controllerMethodDetail($fields, $className, $managerName, array $path = [], string $tableName = ''): string
$_path = str_replace(CONTROLLER_PATH, '', $path['path']); {
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
return ' return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Get(\'' . $this->db->database . '/' . $tableName . '/detail\')]
public function actionDetail(): ResponseInterface public function actionDetail(): ResponseInterface
{ {
$model = ' . $managerName . '::findOne($this->request->query(\'id\')); $model = ' . $managerName . '::findOne($this->request->query(\'id\'));
@@ -365,28 +393,30 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]); return $this->response->json([\'code\' => 0, \'param\' => $model->toArray()]);
} }
}'; }';
} }
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
* @param $managerName * @param $managerName
* @param $path * @param $path
* @return string * @param string $tableName
* 构建删除操作 * @return string
*/ * 构建删除操作
public function controllerMethodDelete($fields, $className, $managerName, $path): string */
{ public function controllerMethodDelete($fields, $className, $managerName, $path, string $tableName = ''): string
$_path = str_replace(CONTROLLER_PATH, '', $path['path']); {
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
return ' return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Post(\'' . $this->db->database . '/' . $tableName . '/delete\')]
public function actionDelete(): ResponseInterface public function actionDelete(): ResponseInterface
{ {
$_key = $this->request->post(\'id\', 0); $_key = $this->request->post(\'id\', 0);
@@ -401,28 +431,30 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'message\' => \'ok\']); return $this->response->json([\'code\' => 0, \'message\' => \'ok\']);
} }
}'; }';
} }
/** /**
* @param $fields * @param $fields
* @param $className * @param $className
* @param $managerName * @param $managerName
* @param array $path * @param array $path
* @return string * @param string $tableName
* 构建查询列表 * @return string
*/ * 构建查询列表
public function controllerMethodList($fields, $className, $managerName, $path = []): string */
{ public function controllerMethodList($fields, $className, $managerName, array $path = [], string $tableName = ''): string
$_path = str_replace(CONTROLLER_PATH, '', $path['path']); {
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
$_path = ltrim($_path, '/'); $_path = ltrim($_path, '/');
return ' return '
/** /**
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Get(\'' . $this->db->database . '/' . $tableName . '/list\')]
public function actionList(): ResponseInterface public function actionList(): ResponseInterface
{ {
//分页处理 //分页处理
@@ -457,61 +489,61 @@ class {$controllerName}Controller extends Controller
return $this->response->json([\'code\' => 0, \'param\' => $data, \'count\' => $count]); return $this->response->json([\'code\' => 0, \'param\' => $data, \'count\' => $count]);
} }
'; ';
} }
private function getData($path, $formClass, $fields): string private function getData($path, $formClass, $fields): string
{ {
$html = ''; $html = '';
$length = $this->getMaxLength($fields); $length = $this->getMaxLength($fields);
$class = ''; $class = '';
$header = []; $header = [];
$toArray = []; $toArray = [];
foreach ($fields as $key => $val) { foreach ($fields as $key => $val) {
if (str_starts_with($val['Type'], 'enum')) { if (str_starts_with($val['Type'], 'enum')) {
preg_match('/\(.*\)/', $val['Type'], $number); preg_match('/\(.*\)/', $val['Type'], $number);
$number[0] = trim($number[0], '()'); $number[0] = trim($number[0], '()');
$values = explode(',', $number[0]); $values = explode(',', $number[0]);
$number[1] = 0; $number[1] = 0;
foreach ($values as $evalue) { foreach ($values as $evalue) {
$evalue = trim($evalue, '\''); $evalue = trim($evalue, '\'');
$leng = mb_strlen($evalue); $leng = mb_strlen($evalue);
if ($number[1] < $leng) { if ($number[1] < $leng) {
$number[1] = $leng; $number[1] = $leng;
} }
} }
$type = strtolower(preg_replace('/\(\'\w+\'(,\'\w+\')*\)/', '', $val['Type'])); $type = strtolower(preg_replace('/\(\'\w+\'(,\'\w+\')*\)/', '', $val['Type']));
$first = preg_replace('/\s+\w+/', '', $type); $first = preg_replace('/\s+\w+/', '', $type);
} else { } else {
preg_match('/\((\d+)(,(\d+))*\)/', $val['Type'], $number); preg_match('/\((\d+)(,(\d+))*\)/', $val['Type'], $number);
$type = strtolower(preg_replace('/\(\d+(,\d+)*\)/', '', $val['Type'])); $type = strtolower(preg_replace('/\(\d+(,\d+)*\)/', '', $val['Type']));
$first = preg_replace('/\s+\w+/', '', $type); $first = preg_replace('/\s+\w+/', '', $type);
} }
if ($val['Extra'] == 'auto_increment') continue; if ($val['Extra'] == 'auto_increment') continue;
if ($type == 'timestamp') continue; if ($type == 'timestamp') continue;
$_field = []; $_field = [];
$_field['required'] = $this->checkIsRequired($val); $_field['required'] = $this->checkIsRequired($val);
foreach ($this->type as $_key => $value) { foreach ($this->type as $_key => $value) {
if (!in_array(strtolower($first), $value)) continue; if (!in_array(strtolower($first), $value)) continue;
$comment = $val['Comment']; $comment = $val['Comment'];
$_field['type'] = $_key; $_field['type'] = $_key;
$toArray[] = ' $toArray[] = '
\'' . str_pad($val['Field'] . '\'', $length, ' ', STR_PAD_RIGHT) . ' => $this->' . $val['Field'] . ','; \'' . str_pad($val['Field'] . '\'', $length, ' ', STR_PAD_RIGHT) . ' => $this->' . $val['Field'] . ',';
if ($type == 'date' || $type == 'datetime' || $type == 'time') { if ($type == 'date' || $type == 'datetime' || $type == 'time') {
if (!in_array('use Kiri\Router\Validator\Inject\Length;', $header)) { if (!in_array('use Kiri\Router\Validator\Inject\Length;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\Length;'; $header[] = 'use Kiri\Router\Validator\Inject\Length;';
} }
$class .= match ($type) { $class .= match ($type) {
'date' => ' 'date' => '
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
@@ -519,7 +551,7 @@ class {$controllerName}Controller extends Controller
public string $' . $val['Field'] . ' = \'1960-06-01\'; public string $' . $val['Field'] . ' = \'1960-06-01\';
', ',
'time' => ' 'time' => '
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
@@ -527,7 +559,7 @@ class {$controllerName}Controller extends Controller
public string $' . $val['Field'] . ' = \'00:00\'; public string $' . $val['Field'] . ' = \'00:00\';
', ',
default => ' default => '
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
@@ -535,65 +567,65 @@ class {$controllerName}Controller extends Controller
public string $' . $val['Field'] . ' = \'\'; public string $' . $val['Field'] . ' = \'\';
', ',
}; };
} else if ($type == 'json' || $type == 'text' || $type == 'longtext') { } else if ($type == 'json' || $type == 'text' || $type == 'longtext') {
$class .= ' $class .= '
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/ */
public string $' . $val['Field'] . ' = \'\'; public string $' . $val['Field'] . ' = \'\';
'; ';
} else { } else {
if (isset($number[0])) { if (isset($number[0])) {
if (strpos(',', $number[0])) { if (strpos(',', $number[0])) {
$_field['min'] = $number[1]; $_field['min'] = $number[1];
$_field['max'] = $number[3]; $_field['max'] = $number[3];
} else { } else {
$_field['min'] = 0; $_field['min'] = 0;
$_field['max'] = $number[1]; $_field['max'] = $number[1];
} }
} }
if ($type == 'enum' && !in_array('use Kiri\Router\Validator\Inject\In;', $header)) { if ($type == 'enum' && !in_array('use Kiri\Router\Validator\Inject\In;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\In;'; $header[] = 'use Kiri\Router\Validator\Inject\In;';
} }
if ($_field['required'] == 'true' && !in_array('use Kiri\Router\Validator\Inject\Required;', $header)) { if ($_field['required'] == 'true' && !in_array('use Kiri\Router\Validator\Inject\Required;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\Required;'; $header[] = 'use Kiri\Router\Validator\Inject\Required;';
} }
if (!in_array('use Kiri\Router\Validator\Inject\MaxLength;', $header)) { if (!in_array('use Kiri\Router\Validator\Inject\MaxLength;', $header)) {
$header[] = 'use Kiri\Router\Validator\Inject\MaxLength;'; $header[] = 'use Kiri\Router\Validator\Inject\MaxLength;';
} }
$class .= ' $class .= '
/** /**
* ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . ' * ' . (empty($comment) ? '这批懒的很,没写注释' : $comment) . '
*/' . ($type == 'enum' ? ' */' . ($type == 'enum' ? '
#[In([' . $number[0] . '])]' : '') . ($_field['required'] == 'true' ? ' #[In([' . $number[0] . '])]' : '') . ($_field['required'] == 'true' ? '
#[Required]' : '') . ' #[Required]' : '') . '
#[MaxLength(' . ($number[1] ?? 0) . ')] #[MaxLength(' . ($number[1] ?? 0) . ')]
public ' . $_key . ' $' . $val['Field'] . ' = '.(match ($type) { public ' . $_key . ' $' . $val['Field'] . ' = ' . (match ($type) {
'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp', 'year' => 0, 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp', 'year' => 0,
'bool', 'boolean' => false, 'bool', 'boolean' => false,
default => '\'\'', default => '\'\'',
}).'; }) . ';
'; ';
} }
} }
$this->rules[$val['Field']] = $_field; $this->rules[$val['Field']] = $_field;
} }
$namespace = str_replace('Controller', 'Form', $path['namespace']); $namespace = str_replace('Controller', 'Form', $path['namespace']);
$path = str_replace('Controller', 'Form', $path['path']); $path = str_replace('Controller', 'Form', $path['path']);
if (!is_dir($_SERVER['PWD'] . '/app/Form/')) { if (!is_dir($_SERVER['PWD'] . '/app/Form/')) {
mkdir($_SERVER['PWD'] . '/app/Form/'); mkdir($_SERVER['PWD'] . '/app/Form/');
} }
if (!is_dir($path)) { if (!is_dir($path)) {
mkdir($path); mkdir($path);
} }
if (!file_exists($path . '/' . $formClass . 'Form.php')) { if (!file_exists($path . '/' . $formClass . 'Form.php')) {
touch($path . '/' . $formClass . 'Form.php'); touch($path . '/' . $formClass . 'Form.php');
} }
file_put_contents($path . '/' . $formClass . 'Form.php', '<?php file_put_contents($path . '/' . $formClass . 'Form.php', '<?php
namespace ' . $namespace . '; namespace ' . $namespace . ';
@@ -641,59 +673,59 @@ class ' . $formClass . 'Form implements ToArray, \JsonSerializable, \Stringable
} }
}'); }');
return $html; return $html;
} }
/** /**
* @param $fields * @param $fields
* @return int * @return int
*/ */
private function getMaxLength($fields): int private function getMaxLength($fields): int
{ {
$length = 0; $length = 0;
foreach ($fields as $key => $val) { foreach ($fields as $key => $val) {
if (mb_strlen($val['Field'] . ' >=') > $length) $length = mb_strlen($val['Field'] . ' >='); if (mb_strlen($val['Field'] . ' >=') > $length) $length = mb_strlen($val['Field'] . ' >=');
} }
return $length; return $length;
} }
/** /**
* @param $fields * @param $fields
* @return string * @return string
*/ */
private function getWhere($fields): string private function getWhere($fields): string
{ {
$html = ''; $html = '';
$length = $this->getMaxLength($fields); $length = $this->getMaxLength($fields);
foreach ($fields as $key => $val) { foreach ($fields as $key => $val) {
preg_match('/\d+/', $val['Type'], $number); preg_match('/\d+/', $val['Type'], $number);
$type = strtolower(preg_replace('/\(\d+\)/', '', $val['Type'])); $type = strtolower(preg_replace('/\(\d+\)/', '', $val['Type']));
$first = preg_replace('/\s+\w+/', '', $type); $first = preg_replace('/\s+\w+/', '', $type);
if ($type == 'timestamp') continue; if ($type == 'timestamp') continue;
if ($type == 'json') continue; if ($type == 'json') continue;
foreach ($this->type as $_key => $value) { foreach ($this->type as $_key => $value) {
if (!in_array(strtolower($first), $value)) continue; if (!in_array(strtolower($first), $value)) continue;
$comment = '//' . $val['Comment']; $comment = '//' . $val['Comment'];
if ($type == 'date' || $type == 'datetime' || $type == 'time') { if ($type == 'date' || $type == 'datetime' || $type == 'time') {
$_tps = '$this->request->query(\'' . $val['Field'] . '\', null)'; $_tps = '$this->request->query(\'' . $val['Field'] . '\', null)';
$html .= ' $html .= '
$pWhere[\'' . str_pad($val['Field'] . ' <=\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment; $pWhere[\'' . str_pad($val['Field'] . ' <=\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment;
$html .= ' $html .= '
$pWhere[\'' . str_pad($val['Field'] . ' >=\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment; $pWhere[\'' . str_pad($val['Field'] . ' >=\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment;
} else { } else {
$_tps = '$this->request->query(\'' . $val['Field'] . '\', null)'; $_tps = '$this->request->query(\'' . $val['Field'] . '\', null)';
$html .= ' $html .= '
$pWhere[\'' . str_pad($val['Field'] . '\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment; $pWhere[\'' . str_pad($val['Field'] . '\']', $length, ' ', STR_PAD_RIGHT) . ' = ' . str_pad($_tps . ';', 60, ' ', STR_PAD_RIGHT) . $comment;
} }
} }
} }
return $html; return $html;
} }
} }