This commit is contained in:
2023-10-08 22:16:32 +08:00
parent ce9d172016
commit 8278d62c10
+70 -38
View File
@@ -66,9 +66,11 @@ namespace {$namespace};
} }
} 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;
@@ -106,45 +108,45 @@ class {$controllerName}Controller extends Controller
} }
$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);
} // }
//
$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\');
$addRouter = 'Router::group([\'prefix\' => \'' . $tableName . '\',\'namespace\' => \'' . $namespace . '\'], function () { // Router::get(\'list\', \'' . $controllerName . 'Controller@actionList\');
Router::post(\'add\', \'' . $controllerName . 'Controller@actionAdd\'); // Router::post(\'update\', \'' . $controllerName . 'Controller@actionUpdate\');
Router::get(\'list\', \'' . $controllerName . 'Controller@actionList\'); // Router::post(\'auditing\', \'' . $controllerName . 'Controller@actionAuditing\');
Router::post(\'update\', \'' . $controllerName . 'Controller@actionUpdate\'); // Router::post(\'batch-auditing\', \'' . $controllerName . 'Controller@actionBatchAuditing\');
Router::post(\'auditing\', \'' . $controllerName . 'Controller@actionAuditing\'); // Router::post(\'batch-delete\', \'' . $controllerName . 'Controller@actionBatchDelete\');
Router::post(\'batch-auditing\', \'' . $controllerName . 'Controller@actionBatchAuditing\'); // Router::post(\'delete\', \'' . $controllerName . 'Controller@actionDelete\');
Router::post(\'batch-delete\', \'' . $controllerName . 'Controller@actionBatchDelete\'); // Router::get(\'detail\', \'' . $controllerName . 'Controller@actionDetail\');
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);
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'; $file = $path['path'] . '/' . $controllerName . 'Controller.php';
if (file_exists($file)) { if (file_exists($file)) {
@@ -191,10 +193,11 @@ class {$controllerName}Controller extends Controller
* @param $className * @param $className
* @param null $object * @param null $object
* @param $path * @param $path
* @param string $tableName
* @return string * @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 = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
@@ -208,6 +211,7 @@ class {$controllerName}Controller extends Controller
* @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 . '();
@@ -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 '
/** /**
* @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));
@@ -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 '
/** /**
* @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\', []);
@@ -270,10 +292,11 @@ class {$controllerName}Controller extends Controller
* @param $className * @param $className
* @param null $object * @param null $object
* @param array $path * @param array $path
* @param string $tableName
* @return string * @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 = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
@@ -285,6 +308,7 @@ class {$controllerName}Controller extends Controller
* @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));
@@ -305,10 +329,11 @@ class {$controllerName}Controller extends Controller
* @param $className * @param $className
* @param null $object * @param null $object
* @param array $path * @param array $path
* @param string $tableName
* @return string * @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 = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
@@ -320,6 +345,7 @@ class {$controllerName}Controller extends Controller
* @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\', []);
@@ -341,10 +367,11 @@ class {$controllerName}Controller extends Controller
* @param $className * @param $className
* @param $managerName * @param $managerName
* @param array $path * @param array $path
* @param string $tableName
* @return string * @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 = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
@@ -356,6 +383,7 @@ class {$controllerName}Controller extends Controller
* @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\'));
@@ -372,10 +400,11 @@ class {$controllerName}Controller extends Controller
* @param $className * @param $className
* @param $managerName * @param $managerName
* @param $path * @param $path
* @param string $tableName
* @return string * @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 = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
@@ -387,6 +416,7 @@ class {$controllerName}Controller extends Controller
* @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);
@@ -408,10 +438,11 @@ class {$controllerName}Controller extends Controller
* @param $className * @param $className
* @param $managerName * @param $managerName
* @param array $path * @param array $path
* @param string $tableName
* @return string * @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 = str_replace(CONTROLLER_PATH, '', $path['path']);
$_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className); $_path = lcfirst(rtrim($_path, '/')) . '/' . lcfirst($className);
@@ -423,6 +454,7 @@ class {$controllerName}Controller extends Controller
* @return ResponseInterface * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
#[Get(\'' . $this->db->database . '/' . $tableName . '/list\')]
public function actionList(): ResponseInterface public function actionList(): ResponseInterface
{ {
//分页处理 //分页处理