From 52f567ddbea66265544a9348e7de5058c97a0a60 Mon Sep 17 00:00:00 2001 From: Mohammad Alavi Date: Sun, 8 Dec 2024 19:10:27 +0330 Subject: [PATCH] fix(generator): Broken multi-action Controller generator --- src/Generator/Commands/ContainerApiGenerator.php | 12 ++++++------ src/Generator/Commands/RouteGenerator.php | 16 +++++++++++----- src/Generator/Stubs/controllers/api/crud.stub | 10 +++++----- src/Generator/Stubs/routes/api.mac.stub | 2 +- .../Stubs/routes/{api.stub => api.sac.stub} | 0 .../Stubs/routes/{web.stub => web.mac.stub} | 0 src/Generator/Stubs/routes/web.sac.stub | 8 ++++++++ 7 files changed, 31 insertions(+), 17 deletions(-) rename src/Generator/Stubs/routes/{api.stub => api.sac.stub} (100%) rename src/Generator/Stubs/routes/{web.stub => web.mac.stub} (100%) create mode 100644 src/Generator/Stubs/routes/web.sac.stub diff --git a/src/Generator/Commands/ContainerApiGenerator.php b/src/Generator/Commands/ContainerApiGenerator.php index 2e206fac..48a0e8e8 100644 --- a/src/Generator/Commands/ContainerApiGenerator.php +++ b/src/Generator/Commands/ContainerApiGenerator.php @@ -162,7 +162,7 @@ public function getUserInputs(): array|null [ 'stub' => 'List', 'name' => 'List' . $models, - 'operation' => 'list' . $models, + 'operation' => 'list', 'verb' => 'GET', 'url' => $url, 'action' => 'List' . $models . 'Action', @@ -176,7 +176,7 @@ public function getUserInputs(): array|null [ 'stub' => 'Find', 'name' => 'Find' . $model . 'ById', - 'operation' => 'find' . $model . 'ById', + 'operation' => 'findById', 'verb' => 'GET', 'url' => $url . '/{id}', 'action' => 'Find' . $model . 'ByIdAction', @@ -190,7 +190,7 @@ public function getUserInputs(): array|null [ 'stub' => 'Create', 'name' => 'Create' . $model, - 'operation' => 'create' . $model, + 'operation' => 'create', 'verb' => 'POST', 'url' => $url, 'action' => 'Create' . $model . 'Action', @@ -204,7 +204,7 @@ public function getUserInputs(): array|null [ 'stub' => 'Update', 'name' => 'Update' . $model, - 'operation' => 'update' . $model, + 'operation' => 'update', 'verb' => 'PATCH', 'url' => $url . '/{id}', 'action' => 'Update' . $model . 'Action', @@ -218,7 +218,7 @@ public function getUserInputs(): array|null [ 'stub' => 'Delete', 'name' => 'Delete' . $model, - 'operation' => 'delete' . $model, + 'operation' => 'delete', 'verb' => 'DELETE', 'url' => $url . '/{id}', 'action' => 'Delete' . $model . 'Action', @@ -324,7 +324,7 @@ public function getUserInputs(): array|null '--container' => $containerName, '--file' => $route['name'], '--ui' => $ui, - '--operation' => $route['operation'], + '--operation' => '__invoke', '--doctype' => $doctype, '--docversion' => $version, '--url' => $route['url'], diff --git a/src/Generator/Commands/RouteGenerator.php b/src/Generator/Commands/RouteGenerator.php index b88fc46d..7b857035 100644 --- a/src/Generator/Commands/RouteGenerator.php +++ b/src/Generator/Commands/RouteGenerator.php @@ -56,20 +56,26 @@ public function getUserInputs(): array|null $ui = Str::lower($this->checkParameterOrChoice('ui', 'Select the UI for the controller', ['API', 'WEB'], 0)); $version = $this->checkParameterOrAsk('docversion', 'Enter the endpoint version (integer)', 1); $doctype = $this->checkParameterOrChoice('doctype', 'Select the type for this endpoint', ['private', 'public'], 0); - $operation = $this->checkParameterOrAsk('operation', 'Enter the name of the controller function that needs to be invoked when calling this endpoint'); - $verb = Str::upper($this->checkParameterOrAsk('verb', 'Enter the HTTP verb of this endpoint (GET, POST,...)')); + $operation = $this->checkParameterOrAsk('operation', 'Enter the name of the controller action', '__invoke'); + $verb = Str::upper($this->checkParameterOrAsk('verb', 'Enter the HTTP verb of this endpoint (GET, POST,...)', 'GET')); // Get the URI and remove the first trailing slash $url = Str::lower($this->checkParameterOrAsk('url', 'Enter the endpoint URI (foo/bar/{id})')); $url = ltrim($url, '/'); - $controllerName = $this->checkParameterOrAsk('controller', 'Enter the controller name', Str::studly($operation) . 'Controller'); + $invokable = false; + if ('__invoke' === $operation) { + $invokable = true; + } + $controllerName = $this->checkParameterOrAsk('controller', 'Enter the controller name', 'Controller'); $docUrl = preg_replace('~{(.+?)}~', ':$1', $url); $routeName = Str::lower($ui . '_' . $this->containerName . '_' . Str::snake($operation)); - // Change the stub to the currently selected UI (API / WEB) - $this->stubName = 'routes/' . $ui . '.stub'; + $this->stubName = 'routes/' . $ui . '.mac.stub'; + if ($invokable) { + $this->stubName = 'routes/' . $ui . '.sac.stub'; + } return [ 'path-parameters' => [ diff --git a/src/Generator/Stubs/controllers/api/crud.stub b/src/Generator/Stubs/controllers/api/crud.stub index 7e930f29..795194b1 100644 --- a/src/Generator/Stubs/controllers/api/crud.stub +++ b/src/Generator/Stubs/controllers/api/crud.stub @@ -31,7 +31,7 @@ class {{class-name}} extends {{base-controller}} * @throws CreateResourceFailedException * @throws IncorrectIdException */ - public function create{{model}}(Create{{model}}Request $request, Create{{model}}Action $action): JsonResponse + public function create(Create{{model}}Request $request, Create{{model}}Action $action): JsonResponse { ${{entity}} = $action->run($request); @@ -42,7 +42,7 @@ class {{class-name}} extends {{base-controller}} * @throws InvalidTransformerException * @throws NotFoundException */ - public function find{{model}}ById(Find{{model}}ByIdRequest $request, Find{{model}}ByIdAction $action): array + public function findById(Find{{model}}ByIdRequest $request, Find{{model}}ByIdAction $action): array { ${{entity}} = $action->run($request); @@ -54,7 +54,7 @@ class {{class-name}} extends {{base-controller}} * @throws CoreInternalErrorException * @throws RepositoryException */ - public function list{{models}}(List{{models}}Request $request, List{{models}}Action $action): array + public function list(List{{models}}Request $request, List{{models}}Action $action): array { ${{entities}} = $action->run($request); @@ -67,7 +67,7 @@ class {{class-name}} extends {{base-controller}} * @throws IncorrectIdException * @throws NotFoundException */ - public function update{{model}}(Update{{model}}Request $request, Update{{model}}Action $action): array + public function update(Update{{model}}Request $request, Update{{model}}Action $action): array { ${{entity}} = $action->run($request); @@ -78,7 +78,7 @@ class {{class-name}} extends {{base-controller}} * @throws DeleteResourceFailedException * @throws NotFoundException */ - public function delete{{model}}(Delete{{model}}Request $request, Delete{{model}}Action $action): JsonResponse + public function delete(Delete{{model}}Request $request, Delete{{model}}Action $action): JsonResponse { $action->run($request); diff --git a/src/Generator/Stubs/routes/api.mac.stub b/src/Generator/Stubs/routes/api.mac.stub index 4dbea61f..c04f5522 100644 --- a/src/Generator/Stubs/routes/api.mac.stub +++ b/src/Generator/Stubs/routes/api.mac.stub @@ -25,6 +25,6 @@ use App\Containers\{{section-name}}\{{container-name}}\UI\API\Controllers\{{controller-name}}; use Illuminate\Support\Facades\Route; -Route::{{http-verb}}('{{endpoint-url}}', {{controller-name}}::class) +Route::{{http-verb}}('{{endpoint-url}}', [{{controller-name}}::class, '{{operation}}']) ->middleware(['auth:{{auth-middleware}}']); diff --git a/src/Generator/Stubs/routes/api.stub b/src/Generator/Stubs/routes/api.sac.stub similarity index 100% rename from src/Generator/Stubs/routes/api.stub rename to src/Generator/Stubs/routes/api.sac.stub diff --git a/src/Generator/Stubs/routes/web.stub b/src/Generator/Stubs/routes/web.mac.stub similarity index 100% rename from src/Generator/Stubs/routes/web.stub rename to src/Generator/Stubs/routes/web.mac.stub diff --git a/src/Generator/Stubs/routes/web.sac.stub b/src/Generator/Stubs/routes/web.sac.stub new file mode 100644 index 00000000..d6ca3aa3 --- /dev/null +++ b/src/Generator/Stubs/routes/web.sac.stub @@ -0,0 +1,8 @@ +middleware(['auth:{{auth-middleware}}']); +