Skip to content

Commit 56f1911

Browse files
committed
feat: controller related things completed (wip)
1 parent 484d65a commit 56f1911

18 files changed

+720
-32
lines changed

config/laravel_generator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@
136136

137137
'save_schema_file' => true,
138138

139-
'localized' => false,
139+
'localized' => true,
140140

141141
'repository_pattern' => true,
142142

143-
'resources' => false,
143+
'resources' => true,
144144

145145
'factory' => false,
146146

src/Commands/BaseCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function generateAPIItems()
109109
$apiTestGenerator = app(APITestGenerator::class);
110110
$apiTestGenerator->generate();
111111
}
112+
112113
if ($this->config->options->resources) {
113114
$apiResourceGenerator = app(APIResourceGenerator::class);
114115
$apiResourceGenerator->generate();

src/Generators/API/APIControllerGenerator.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace InfyOm\Generator\Generators\API;
44

5+
use Illuminate\Support\Str;
56
use InfyOm\Generator\Generators\BaseGenerator;
67

78
class APIControllerGenerator extends BaseGenerator
@@ -16,34 +17,39 @@ public function __construct()
1617
$this->fileName = $this->config->modelNames->name.'APIController.php';
1718
}
1819

19-
public function generate()
20+
public function variables(): array
21+
{
22+
return array_merge([], $this->fillDocs());
23+
}
24+
25+
public function getViewName(): string
2026
{
2127
if ($this->config->options->repositoryPattern) {
2228
$templateName = 'repository.controller';
2329
} else {
2430
$templateName = 'model.controller';
2531
}
2632

27-
if ($this->config->isLocalizedTemplates()) {
28-
$templateName .= '_locale';
29-
}
30-
3133
if ($this->config->options->resources) {
3234
$templateName .= '_resource';
3335
}
3436

35-
$templateData = get_template("api.controller.$templateName", 'laravel-generator');
37+
return $templateName;
38+
}
39+
40+
public function generate()
41+
{
42+
$viewName = $this->getViewName();
3643

37-
$templateData = fill_template($this->config->dynamicVars, $templateData);
38-
$templateData = $this->fillDocs($templateData);
44+
$templateData = view('laravel-generator::api.controller.'.$viewName, $this->variables())->render();
3945

4046
g_filesystem()->createFile($this->path.$this->fileName, $templateData);
4147

4248
$this->config->commandComment(PHP_EOL.'API Controller created: ');
4349
$this->config->commandInfo($this->fileName);
4450
}
4551

46-
private function fillDocs(string $templateData): string
52+
private function fillDocs(): array
4753
{
4854
$methods = ['controller', 'index', 'store', 'show', 'update', 'destroy'];
4955

@@ -55,14 +61,13 @@ private function fillDocs(string $templateData): string
5561
$templateType = 'laravel-generator';
5662
}
5763

64+
$variables = [];
5865
foreach ($methods as $method) {
59-
$key = '$DOC_'.strtoupper($method).'$';
60-
$docTemplate = get_template($templatePrefix.'.'.$method, $templateType);
61-
$docTemplate = fill_template($this->config->dynamicVars, $docTemplate);
62-
$templateData = str_replace($key, $docTemplate, $templateData);
66+
$variable = 'doc'.Str::title($method);
67+
$variables[$variable] = view($templateType.'::'.$templatePrefix.'.'.$method)->render();
6368
}
6469

65-
return $templateData;
70+
return $variables;
6671
}
6772

6873
public function rollback()

src/Generators/API/APIRequestGenerator.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public function generate()
2828

2929
private function generateCreateRequest()
3030
{
31-
$templateData = get_template('api.request.create_request', 'laravel-generator');
32-
33-
$templateData = fill_template($this->config->dynamicVars, $templateData);
31+
$templateData = view('laravel-generator::api.request.create', $this->variables())->render();
3432

3533
g_filesystem()->createFile($this->path.$this->createFileName, $templateData);
3634

@@ -40,13 +38,12 @@ private function generateCreateRequest()
4038

4139
private function generateUpdateRequest()
4240
{
43-
$modelGenerator = new ModelGenerator();
41+
$modelGenerator = app(ModelGenerator::class);
4442
$rules = $modelGenerator->generateUniqueRules();
45-
$this->config->addDynamicVariable('$UNIQUE_RULES$', $rules);
46-
47-
$templateData = get_template('api.request.update_request', 'laravel-generator');
4843

49-
$templateData = fill_template($this->config->dynamicVars, $templateData);
44+
$templateData = view('laravel-generator::api.request.update', [
45+
'uniqueRules' => $rules
46+
])->render();
5047

5148
g_filesystem()->createFile($this->path.$this->updateFileName, $templateData);
5249

src/Generators/API/APIResourceGenerator.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ public function __construct()
1616
$this->fileName = $this->config->modelNames->name.'Resource.php';
1717
}
1818

19-
public function generate()
19+
public function variables(): array
2020
{
21-
$templateData = get_template('api.resource.api_resource', 'laravel-generator');
21+
return [
22+
'fields' => implode(','.infy_nl_tab(1, 3), $this->generateResourceFields())
23+
];
24+
}
2225

23-
$templateData = fill_template($this->config->dynamicVars, $templateData);
26+
public function generate()
27+
{
2428

25-
$templateData = str_replace(
26-
'$RESOURCE_FIELDS$',
27-
implode(','.infy_nl_tab(1, 3), $this->generateResourceFields()),
28-
$templateData
29-
);
29+
$templateData = view('laravel-generator::api.resource.resource', $this->variables())->render();
3030

3131
g_filesystem()->createFile($this->path.$this->fileName, $templateData);
3232

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
@php
2+
echo "<?php".PHP_EOL;
3+
@endphp
4+
5+
namespace {{ $config->namespaces->apiController }};
6+
7+
use {{ $config->namespaces->apiRequest }}\Create{{ $config->modelNames->name }}APIRequest;
8+
use {{ $config->namespaces->apiRequest }}\Update{{ $config->modelNames->name }}APIRequest;
9+
use {{ $config->namespaces->model }}\{{ $config->modelNames->name }};
10+
use Illuminate\Http\JsonResponse;
11+
use Illuminate\Http\Request;
12+
use {{ $config->namespaces->app }}\Http\Controllers\AppBaseController;
13+
14+
{!! $docController !!}
15+
class {{ $config->modelNames->name }}APIController extends AppBaseController
16+
{
17+
{!! $docIndex !!}
18+
public function index(Request $request): JsonResponse
19+
{
20+
$query = {{ $config->modelNames->name }}::query();
21+
22+
if ($request->get('skip')) {
23+
$query->skip($request->get('skip'));
24+
}
25+
if ($request->get('limit')) {
26+
$query->limit($request->get('limit'));
27+
}
28+
29+
${{ $config->modelNames->camelPlural }} = $query->get();
30+
31+
@if($config->options->localized)
32+
return $this->sendResponse(
33+
${{ $config->modelNames->camelPlural }}->toArray(),
34+
__('messages.retrieved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.plural')])
35+
);
36+
@else
37+
return $this->sendResponse(${{ $config->modelNames->camelPlural }}->toArray(), '{{ $config->modelNames->humanPlural }} retrieved successfully');
38+
@endif
39+
}
40+
41+
{!! $docStore !!}
42+
public function store(Create{{ $config->modelNames->name }}APIRequest $request): JsonResponse
43+
{
44+
$input = $request->all();
45+
46+
/** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */
47+
${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::create($input);
48+
49+
@if($config->options->localized)
50+
return $this->sendResponse(
51+
${{ $config->modelNames->camel }}->toArray(),
52+
__('messages.saved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
53+
);
54+
@else
55+
return $this->sendResponse(${{ $config->modelNames->camel }}->toArray(), '{{ $config->modelNames->human }} saved successfully');
56+
@endif
57+
}
58+
59+
{!! $docShow !!}
60+
public function show($id): JsonResponse
61+
{
62+
/** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */
63+
${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::find($id);
64+
65+
if (empty(${{ $config->modelNames->camel }})) {
66+
@if($config->options->localized)
67+
return $this->sendError(
68+
__('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
69+
);
70+
@else
71+
return $this->sendError('{{ $config->modelNames->human }} not found');
72+
@endif
73+
}
74+
75+
@if($config->options->localized)
76+
return $this->sendResponse(
77+
${{ $config->modelNames->camel }}->toArray(),
78+
__('messages.retrieved', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
79+
);
80+
@else
81+
return $this->sendResponse(${{ $config->modelNames->camel }}->toArray(), '{{ $config->modelNames->human }} retrieved successfully');
82+
@endif
83+
}
84+
85+
{!! $docUpdate !!}
86+
public function update($id, Update{{ $config->modelNames->name }}APIRequest $request): JsonResponse
87+
{
88+
/** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */
89+
${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::find($id);
90+
91+
if (empty(${{ $config->modelNames->camel }})) {
92+
@if($config->options->localized)
93+
return $this->sendError(
94+
__('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
95+
);
96+
@else
97+
return $this->sendError('{{ $config->modelNames->human }} not found');
98+
@endif
99+
}
100+
101+
${{ $config->modelNames->camel }}->fill($request->all());
102+
${{ $config->modelNames->camel }}->save();
103+
104+
@if($config->options->localized)
105+
return $this->sendResponse(
106+
${{ $config->modelNames->camel }}->toArray(),
107+
__('messages.updated', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
108+
);
109+
@else
110+
return $this->sendResponse(${{ $config->modelNames->camel }}->toArray(), '{{ $config->modelNames->name }} updated successfully');
111+
@endif
112+
}
113+
114+
{!! $docDestroy !!}
115+
public function destroy($id): JsonResponse
116+
{
117+
/** @var {{ $config->modelNames->name }} ${{ $config->modelNames->camel }} */
118+
${{ $config->modelNames->camel }} = {{ $config->modelNames->name }}::find($id);
119+
120+
if (empty(${{ $config->modelNames->camel }})) {
121+
@if($config->options->localized)
122+
return $this->sendError(
123+
__('messages.not_found', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
124+
);
125+
@else
126+
return $this->sendError('{{ $config->modelNames->human }} not found');
127+
@endif
128+
}
129+
130+
${{ $config->modelNames->camel }}->delete();
131+
132+
@if($config->options->localized)
133+
return $this->sendResponse(
134+
$id,
135+
__('messages.deleted', ['model' => __('models/{{ $config->modelNames->camelPlural }}.singular')])
136+
);
137+
@else
138+
return $this->sendSuccess('{{ $config->modelNames->human }} deleted successfully');
139+
@endif
140+
}
141+
}

0 commit comments

Comments
 (0)