This commit is contained in:
2023-10-08 22:16:32 +08:00
parent ce9d172016
commit 8278d62c10
+72 -40
View File
@@ -66,9 +66,11 @@ namespace {$namespace};
}
} else {
$import = "use Exception;
use Kiri\Router\Annotate\Post;
use Kiri\Router\Annotate\Get;
use Kiri\Core\Str;
use Kiri\Core\Json;
use Kiri\Router\Base\Controller;
use Kiri\Core\Json;
use {$model_namespace}\\{$managerName};
use Kiri\Router\Validator\Validator;
use Psr\Http\Message\ResponseInterface;
@@ -106,45 +108,45 @@ class {$controllerName}Controller extends Controller
}
$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) {
if (str_contains($html, ' function ' . $val . '(')) {
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 .= '
}';
$file = APP_PATH . 'routes/' . $this->input->getOption('database') . '.php';
if (!file_exists($file)) {
touch($file);
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, '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);
}
$tableName = str_replace($this->db->tablePrefix, '', $this->tableName);
$tableName = str_replace('_', '-', $tableName);
$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);
}
// $file = APP_PATH . 'routes/' . $this->input->getOption('database') . '.php';
// if (!file_exists($file)) {
// touch($file);
// 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, '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);
// }
//
//
// $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);
// }
$file = $path['path'] . '/' . $controllerName . 'Controller.php';
if (file_exists($file)) {
@@ -191,10 +193,11 @@ class {$controllerName}Controller extends Controller
* @param $className
* @param null $object
* @param $path
* @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);
@@ -208,6 +211,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface
* @throws Exception
*/
#[Post(\'' . $this->db->database . '/' . $tableName . '/add\')]
public function actionAdd(): ResponseInterface
{
$model = new ' . $className . '();
@@ -221,13 +225,22 @@ class {$controllerName}Controller extends Controller
}
public function controllerMethodAuditing($fields, $className, $object, $path): string
/**
* @param $fields
* @param $className
* @param $object
* @param $path
* @param string $tableName
* @return string
*/
public function controllerMethodAuditing($fields, $className, $object, $path, string $tableName): string
{
return '
/**
* @return ResponseInterface
* @throws Exception
*/
#[Post(\'' . $this->db->database . '/' . $tableName . '/auditing\')]
public function actionAuditing(): ResponseInterface
{
$model = ' . $className . '::findOne($this->request->post(\'id\', 0));
@@ -243,13 +256,22 @@ class {$controllerName}Controller extends Controller
}
public function controllerMethodBatchAuditing($fields, $className, $object, $path): string
/**
* @param $fields
* @param $className
* @param $object
* @param $path
* @param string $tableName
* @return string
*/
public function controllerMethodBatchAuditing($fields, $className, $object, $path, string $tableName): string
{
return '
/**
* @return ResponseInterface
* @throws Exception
*/
#[Post(\'' . $this->db->database . '/' . $tableName . '/batch/auditing\')]
public function actionBatchAuditing(): ResponseInterface
{
$ids = $this->request->post(\'ids\', []);
@@ -270,10 +292,11 @@ class {$controllerName}Controller extends Controller
* @param $className
* @param null $object
* @param array $path
* @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);
@@ -285,6 +308,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface
* @throws Exception
*/
#[Post(\'' . $this->db->database . '/' . $tableName . '/update\')]
public function actionUpdate(): ResponseInterface
{
$model = ' . $className . '::findOne($this->request->post(\'id\', 0));
@@ -305,10 +329,11 @@ class {$controllerName}Controller extends Controller
* @param $className
* @param null $object
* @param array $path
* @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);
@@ -320,6 +345,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface
* @throws Exception
*/
#[Post(\'' . $this->db->database . '/' . $tableName . '/batch/delete\')]
public function actionBatchDelete(): ResponseInterface
{
$_key = $this->request->post(\'ids\', []);
@@ -341,10 +367,11 @@ class {$controllerName}Controller extends Controller
* @param $className
* @param $managerName
* @param array $path
* @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);
@@ -356,6 +383,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface
* @throws Exception
*/
#[Get(\'' . $this->db->database . '/' . $tableName . '/detail\')]
public function actionDetail(): ResponseInterface
{
$model = ' . $managerName . '::findOne($this->request->query(\'id\'));
@@ -372,10 +400,11 @@ class {$controllerName}Controller extends Controller
* @param $className
* @param $managerName
* @param $path
* @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);
@@ -387,6 +416,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface
* @throws Exception
*/
#[Post(\'' . $this->db->database . '/' . $tableName . '/delete\')]
public function actionDelete(): ResponseInterface
{
$_key = $this->request->post(\'id\', 0);
@@ -408,10 +438,11 @@ class {$controllerName}Controller extends Controller
* @param $className
* @param $managerName
* @param array $path
* @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);
@@ -423,6 +454,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface
* @throws Exception
*/
#[Get(\'' . $this->db->database . '/' . $tableName . '/list\')]
public function actionList(): ResponseInterface
{
//分页处理
@@ -570,11 +602,11 @@ class {$controllerName}Controller extends Controller
#[In([' . $number[0] . '])]' : '') . ($_field['required'] == 'true' ? '
#[Required]' : '') . '
#[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,
'bool', 'boolean' => false,
default => '\'\'',
}).';
}) . ';
';
}