Skip to content

Commit

Permalink
Merge pull request #214 from apiato/API-1026-Broken-multi-action-Cont…
Browse files Browse the repository at this point in the history
…roller-generator

fix(generator): Broken multi-action Controller generator
  • Loading branch information
Mohammad-Alavi authored Dec 8, 2024
2 parents 41af64d + 52f567d commit a246d55
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/Generator/Commands/ContainerApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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'],
Expand Down
16 changes: 11 additions & 5 deletions src/Generator/Commands/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
10 changes: 5 additions & 5 deletions src/Generator/Stubs/controllers/api/crud.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Stubs/routes/api.mac.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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}}']);

File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions src/Generator/Stubs/routes/web.sac.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use App\Containers\{{section-name}}\{{container-name}}\UI\WEB\Controllers\{{controller-name}};
use Illuminate\Support\Facades\Route;

Route::{{http-verb}}('{{endpoint-url}}', {{controller-name}}::class)
->middleware(['auth:{{auth-middleware}}']);

0 comments on commit a246d55

Please sign in to comment.