Skip to content

Commit 84245ea

Browse files
committed
Merge branch '47-add-crud-keys' into 'master'
#47 Добавить methods=CRUD See merge request components/laravel-entity-generator!72
2 parents d009077 + bb25570 commit 84245ea

File tree

7 files changed

+136
-43
lines changed

7 files changed

+136
-43
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use RonasIT\Support\Generators\TranslationsGenerator;
2020
use RonasIT\Support\Generators\SeederGenerator;
2121
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
22+
use UnexpectedValueException;
2223

2324
/**
2425
* @property ControllerGenerator $controllerGenerator
@@ -35,6 +36,10 @@
3536
*/
3637
class MakeEntityCommand extends Command
3738
{
39+
const CRUD_OPTIONS = [
40+
'C', 'R', 'U', 'D'
41+
];
42+
3843
/**
3944
* The name and signature of the console command.
4045
*
@@ -63,6 +68,8 @@ class MakeEntityCommand extends Command
6368
{--only-tests : Set this flag if you want to create only tests. This flag is a higher priority than --without-tests.}
6469
{--only-seeder : Set this flag if you want to create only seeder.}
6570
71+
{--methods=CRUD : Set types of methods to create. Affect on routes, requests classes, controller\'s methods and tests methods.}
72+
6673
{--i|integer=* : Add integer field to entity.}
6774
{--I|integer-required=* : Add required integer field to entity. If you want to specify default value you have to do it manually.}
6875
{--f|float=* : Add float field to entity.}
@@ -178,12 +185,8 @@ protected function classExists($path, $name)
178185

179186
protected function validateInput()
180187
{
181-
if ($this->option('only-api')) {
182-
$modelName = $this->argument('name');
183-
if (!$this->classExists('services', "{$modelName}Service")) {
184-
throw new ClassNotExistsException('Cannot create API without entity.');
185-
}
186-
}
188+
$this->validateOnlyApiOption();
189+
$this->validateCrudOptions();
187190
}
188191

189192
protected function generate()
@@ -225,9 +228,15 @@ protected function runGeneration($generator)
225228
->setModel($this->argument('name'))
226229
->setFields($this->getFields())
227230
->setRelations($this->getRelations())
231+
->setCrudOptions($this->getCrudOptions())
228232
->generate();
229233
}
230234

235+
protected function getCrudOptions()
236+
{
237+
return str_split($this->option('methods'));
238+
}
239+
231240
protected function getRelations()
232241
{
233242
return [
@@ -249,5 +258,25 @@ protected function getFields()
249258
{
250259
return Arr::only($this->options(), EntityGenerator::AVAILABLE_FIELDS);
251260
}
252-
}
253261

262+
protected function validateCrudOptions()
263+
{
264+
$crudOptions = $this->getCrudOptions();
265+
266+
foreach ($crudOptions as $crudOption) {
267+
if (!in_array($crudOption, MakeEntityCommand::CRUD_OPTIONS)) {
268+
throw new UnexpectedValueException("Invalid method {$crudOption}.");
269+
}
270+
}
271+
}
272+
273+
protected function validateOnlyApiOption()
274+
{
275+
if ($this->option('only-api')) {
276+
$modelName = $this->argument('name');
277+
if (!$this->classExists('services', "{$modelName}Service")) {
278+
throw new ClassNotExistsException('Cannot create API without entity.');
279+
}
280+
}
281+
}
282+
}

src/Generators/EntityGenerator.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ abstract class EntityGenerator
1919
protected $model;
2020
protected $fields;
2121
protected $relations;
22+
protected $crudOptions;
23+
24+
/**
25+
* @param array $crudOptions
26+
* @return $this
27+
*/
28+
public function setCrudOptions($crudOptions)
29+
{
30+
$this->crudOptions = $crudOptions;
31+
32+
return $this;
33+
}
2234

2335
/**
2436
* @param string $model
@@ -101,6 +113,8 @@ protected function getStub($stub, $data = [])
101113
{
102114
$stubPath = config("entity-generator.stubs.$stub");
103115

116+
$data['options'] = $this->crudOptions;
117+
104118
return view($stubPath)->with($data)->render();
105119
}
106120

src/Generators/RequestsGenerator.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,38 @@ public function setRelations($relations)
2727

2828
public function generate()
2929
{
30-
$this->createRequest(
31-
self::GET_METHOD,
32-
true,
33-
$this->getGetValidationParameters()
34-
);
35-
36-
$this->createRequest(self::DELETE_METHOD);
30+
if (in_array('R', $this->crudOptions)) {
31+
$this->createRequest(
32+
self::GET_METHOD,
33+
true,
34+
$this->getGetValidationParameters()
35+
);
36+
$this->createRequest(
37+
self::SEARCH_METHOD,
38+
false,
39+
$this->getSearchValidationParameters()
40+
);
41+
}
3742

38-
$this->createRequest(
39-
self::CREATE_METHOD,
40-
false,
41-
$this->getValidationParameters($this->fields, true)
42-
);
43+
if (in_array('D', $this->crudOptions)) {
44+
$this->createRequest(self::DELETE_METHOD);
45+
}
4346

44-
$this->createRequest(
45-
self::UPDATE_METHOD,
46-
true,
47-
$this->getValidationParameters($this->fields, false)
48-
);
47+
if (in_array('C', $this->crudOptions)) {
48+
$this->createRequest(
49+
self::CREATE_METHOD,
50+
false,
51+
$this->getValidationParameters($this->fields, true)
52+
);
53+
}
4954

50-
$this->createRequest(
51-
self::SEARCH_METHOD,
52-
false,
53-
$this->getSearchValidationParameters()
54-
);
55+
if (in_array('U', $this->crudOptions)) {
56+
$this->createRequest(
57+
self::UPDATE_METHOD,
58+
true,
59+
$this->getValidationParameters($this->fields, false)
60+
);
61+
}
5562
}
5663

5764
protected function createRequest($method, $needToValidate = true, $parameters = [])

src/Generators/TestsGenerator.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,25 @@ protected function generateExistedEntityFixture()
179179
$entity = Str::snake($this->model);
180180

181181
foreach (self::FIXTURE_TYPES as $type => $modifications) {
182-
foreach ($modifications as $modification) {
183-
$excepts = [];
184-
if ($modification === 'request') {
185-
$excepts = ['id'];
182+
if ($this->isFixtureNeeded($type)) {
183+
foreach ($modifications as $modification) {
184+
$excepts = [];
185+
if ($modification === 'request') {
186+
$excepts = ['id'];
187+
}
188+
$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
186189
}
187-
$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
188190
}
189191
}
190192
}
191193

194+
protected function isFixtureNeeded($type)
195+
{
196+
$firstLetter = strtoupper($type[0]);
197+
198+
return in_array($firstLetter, $this->crudOptions);
199+
}
200+
192201
protected function generateFixture($fixtureName, $data)
193202
{
194203
$fixturePath = $this->getFixturesPath($fixtureName);

stubs/controller.blade.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
namespace App\Http\Controllers;
22

3+
@if (in_array('C', $options))
34
use App\Http\Requests\{{$requestsFolder}}\Create{{$entity}}Request;
4-
use App\Http\Requests\{{$requestsFolder}}\Get{{$entity}}Request;
5+
@endif
6+
@if (in_array('U', $options))
57
use App\Http\Requests\{{$requestsFolder}}\Update{{$entity}}Request;
8+
@endif
9+
@if (in_array('D', $options))
610
use App\Http\Requests\{{$requestsFolder}}\Delete{{$entity}}Request;
11+
@endif
12+
@if (in_array('R', $options))
13+
use App\Http\Requests\{{$requestsFolder}}\Get{{$entity}}Request;
714
use App\Http\Requests\{{$requestsFolder}}\Search{{\Illuminate\Support\Str::plural($entity)}}Request;
15+
@endif
816
use App\Services\{{$entity}}Service;
17+
@if (in_array('D', $options) || in_array('U', $options))
918
use Symfony\Component\HttpFoundation\Response;
1019

20+
@endif
1121
class {{$entity}}Controller extends Controller
1222
{
23+
@if (in_array('C', $options))
1324
public function create(Create{{$entity}}Request $request, {{$entity}}Service $service)
1425
{
1526
$data = $request->onlyValidated();
@@ -19,6 +30,8 @@ public function create(Create{{$entity}}Request $request, {{$entity}}Service $se
1930
return response()->json($result);
2031
}
2132

33+
@endif
34+
@if (in_array('R', $options))
2235
public function get(Get{{$entity}}Request $request, {{$entity}}Service $service, $id)
2336
{
2437
$result = $service
@@ -28,24 +41,30 @@ public function get(Get{{$entity}}Request $request, {{$entity}}Service $service,
2841
return response()->json($result);
2942
}
3043

44+
public function search(Search{{\Illuminate\Support\Str::plural($entity)}}Request $request, {{$entity}}Service $service)
45+
{
46+
$result = $service->search($request->onlyValidated());
47+
48+
return response()->json($result);
49+
}
50+
51+
@endif
52+
@if (in_array('U', $options))
3153
public function update(Update{{$entity}}Request $request, {{$entity}}Service $service, $id)
3254
{
3355
$service->update($id, $request->onlyValidated());
3456

3557
return response('', Response::HTTP_NO_CONTENT);
3658
}
3759

60+
@endif
61+
@if (in_array('D', $options))
3862
public function delete(Delete{{$entity}}Request $request, {{$entity}}Service $service, $id)
3963
{
4064
$service->delete($id);
4165

4266
return response('', Response::HTTP_NO_CONTENT);
4367
}
4468

45-
public function search(Search{{\Illuminate\Support\Str::plural($entity)}}Request $request, {{$entity}}Service $service)
46-
{
47-
$result = $service->search($request->onlyValidated());
48-
49-
return response()->json($result);
50-
}
69+
@endif
5170
}

stubs/routes.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
1+
@if (in_array('C', $options))
22
Route::post('/{{$entities}}', ['uses' => {{$entity}}Controller::class . '@create']);
3+
@endif
4+
@if (in_array('U', $options))
35
Route::put('/{{$entities}}/{id}', ['uses' => {{$entity}}Controller::class . '@update']);
6+
@endif
7+
@if (in_array('D', $options))
48
Route::delete('/{{$entities}}/{id}', ['uses' => {{$entity}}Controller::class . '@delete']);
9+
@endif
10+
@if (in_array('R', $options))
511
Route::get('/{{$entities}}/{id}', ['uses' => {{$entity}}Controller::class . '@get']);
612
Route::get('/{{$entities}}', ['uses' => {{$entity}}Controller::class . '@search']);
13+
@endif

stubs/test.blade.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace App\Tests;
22

3-
use Illuminate\Support\Arr;
43
use Symfony\Component\HttpFoundation\Response;
54
@if ($withAuth)
65
use App\Models\User;
@@ -21,6 +20,7 @@ public function setUp() : void
2120
@endif
2221
}
2322

23+
@if (in_array('C', $options))
2424
public function testCreate()
2525
{
2626
$data = $this->getJsonFixture('create_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
@@ -49,6 +49,8 @@ public function testCreateNoAuth()
4949
}
5050

5151
@endif
52+
@endif
53+
@if (in_array('U', $options))
5254
public function testUpdate()
5355
{
5456
$data = $this->getJsonFixture('update_{{\Illuminate\Support\Str::snake($entity)}}_request.json');
@@ -88,6 +90,8 @@ public function testUpdateNoAuth()
8890
}
8991

9092
@endif
93+
@endif
94+
@if (in_array('D', $options))
9195
public function testDelete()
9296
{
9397
@if (!$withAuth)
@@ -127,6 +131,8 @@ public function testDeleteNoAuth()
127131
}
128132

129133
@endif
134+
@endif
135+
@if (in_array('R', $options))
130136
public function testGet()
131137
{
132138
@if (!$withAuth)
@@ -196,4 +202,6 @@ public function testSearch($filter, $fixture)
196202

197203
$this->assertEqualsFixture($fixture, $response->json());
198204
}
205+
206+
@endif
199207
}

0 commit comments

Comments
 (0)