From 54ae8416c3bbd751d000fc7f2e3a6c5a8d459198 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 11 Oct 2018 09:37:59 +0800 Subject: [PATCH 1/5] Add --form-requests command option --- src/CrudApiMake.php | 1 + src/CrudMake.php | 1 + src/CrudSimpleMake.php | 1 + 3 files changed, 3 insertions(+) diff --git a/src/CrudApiMake.php b/src/CrudApiMake.php index ddf0f23..56f6b14 100644 --- a/src/CrudApiMake.php +++ b/src/CrudApiMake.php @@ -12,6 +12,7 @@ class CrudApiMake extends GeneratorCommand protected $signature = 'make:crud-api {name : The model name} {--p|parent= : The generated API controller parent directory} {--t|tests-only : Generate API CRUD testcases only} + {--r|form-requests : Generate CRUD with Form Request on create and update actions} {--f|formfield : Generate CRUD with FormField facades}'; /** diff --git a/src/CrudMake.php b/src/CrudMake.php index a797242..5426eb4 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -13,6 +13,7 @@ class CrudMake extends GeneratorCommand {--p|parent= : The generated controller parent directory} {--t|tests-only : Generate CRUD testcases only} {--f|formfield : Generate CRUD with FormField facades} + {--r|form-requests : Generate CRUD with Form Request on create and update actions} {--bs3 : Generate CRUD with Bootstrap 3 views}'; /** diff --git a/src/CrudSimpleMake.php b/src/CrudSimpleMake.php index 1b0d1f8..dd0dcef 100644 --- a/src/CrudSimpleMake.php +++ b/src/CrudSimpleMake.php @@ -13,6 +13,7 @@ class CrudSimpleMake extends GeneratorCommand {--p|parent= : The generated controller parent directory} {--t|tests-only : Generate CRUD testcases only} {--f|formfield : Generate CRUD with FormField facades} + {--r|form-requests : Generate CRUD with Form Request on create and update actions} {--bs3 : Generate CRUD with Bootstrap 3 views}'; /** From 297539bad8d1e29f4535df008f0fe52e969139d7 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 11 Oct 2018 09:38:37 +0800 Subject: [PATCH 2/5] Add full-formrequests controller stub --- src/Generators/ControllerGenerator.php | 4 + src/stubs/controllers/full-formrequests.stub | 109 ++++++++++++++++++ .../FullCrudFormRequestOptionsTest.php | 127 +++++++++++++++++++++ 3 files changed, 240 insertions(+) create mode 100644 src/stubs/controllers/full-formrequests.stub create mode 100644 tests/CommandOptions/FullCrudFormRequestOptionsTest.php diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index c8de52c..df19527 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -39,6 +39,10 @@ class ControllerGenerator extends BaseGenerator */ public function getContent(string $stubName) { + if ($this->command->option('form-requests')) { + $stubName .= '-formrequests'; + } + $stub = $this->getStubFileContent($stubName); $controllerFileContent = $this->replaceStubString($stub); diff --git a/src/stubs/controllers/full-formrequests.stub b/src/stubs/controllers/full-formrequests.stub new file mode 100644 index 0000000..62ba3aa --- /dev/null +++ b/src/stubs/controllers/full-formrequests.stub @@ -0,0 +1,109 @@ +where('name', 'like', '%'.request('q').'%'); + $mstrCollections = $singleMstrQuery->paginate(25); + + return view('masters.index', compact('mstrCollections')); + } + + /** + * Show the form for creating a new singleMstr. + * + * @return \Illuminate\View\View + */ + public function create() + { + $this->authorize('create', new Master); + + return view('masters.create'); + } + + /** + * Store a newly created singleMstr in storage. + * + * @param \App\Http\Requests\Masters\CreateRequest $createMasterForm + * @return \Illuminate\Routing\Redirector + */ + public function store(CreateRequest $createMasterForm) + { + $singleMstr = $createMasterForm->save(); + + return redirect()->route('masters.show', $singleMstr); + } + + /** + * Display the specified singleMstr. + * + * @param \App\Master $singleMstr + * @return \Illuminate\View\View + */ + public function show(Master $singleMstr) + { + return view('masters.show', compact('singleMstr')); + } + + /** + * Show the form for editing the specified singleMstr. + * + * @param \App\Master $singleMstr + * @return \Illuminate\View\View + */ + public function edit(Master $singleMstr) + { + $this->authorize('update', $singleMstr); + + return view('masters.edit', compact('singleMstr')); + } + + /** + * Update the specified singleMstr in storage. + * + * @param \App\Http\Requests\Masters\UpdateRequest $singleMstrUpdateForm + * @param \fullMstr $singleMstr + * @return \Illuminate\Routing\Redirector + */ + public function update(UpdateRequest $singleMstrUpdateForm, Master $singleMstr) + { + $singleMstr->update($singleMstrUpdateForm->validated()); + + return redirect()->route('masters.show', $singleMstr); + } + + /** + * Remove the specified singleMstr from storage. + * + * @param \fullMstr $singleMstr + * @return \Illuminate\Routing\Redirector + */ + public function destroy(Master $singleMstr) + { + $this->authorize('delete', $singleMstr); + + request()->validate([ + 'master_id' => 'required', + ]); + + if (request('master_id') == $singleMstr->id && $singleMstr->delete()) { + $routeParam = request()->only('page', 'q'); + + return redirect()->route('masters.index', $routeParam); + } + + return back(); + } +} diff --git a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php new file mode 100644 index 0000000..9dda61f --- /dev/null +++ b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php @@ -0,0 +1,127 @@ +artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]); + + $this->assertFileExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); + $ctrlClassContent = "full_model_name}; +use Illuminate\Http\Request; + +class {$this->model_name}Controller extends Controller +{ + /** + * Display a listing of the {$this->single_model_var_name}. + * + * @return \Illuminate\View\View + */ + public function index() + { + \${$this->single_model_var_name}Query = {$this->model_name}::query(); + \${$this->single_model_var_name}Query->where('name', 'like', '%'.request('q').'%'); + \${$this->collection_model_var_name} = \${$this->single_model_var_name}Query->paginate(25); + + return view('{$this->table_name}.index', compact('{$this->collection_model_var_name}')); + } + + /** + * Show the form for creating a new {$this->single_model_var_name}. + * + * @return \Illuminate\View\View + */ + public function create() + { + \$this->authorize('create', new {$this->model_name}); + + return view('{$this->table_name}.create'); + } + + /** + * Store a newly created {$this->single_model_var_name} in storage. + * + * @param \App\Http\Requests\\{$this->plural_model_name}\CreateRequest \$create{$this->model_name}Form + * @return \Illuminate\Routing\Redirector + */ + public function store(CreateRequest \$create{$this->model_name}Form) + { + \${$this->single_model_var_name} = \$create{$this->model_name}Form->save(); + + return redirect()->route('{$this->table_name}.show', \${$this->single_model_var_name}); + } + + /** + * Display the specified {$this->single_model_var_name}. + * + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\View\View + */ + public function show({$this->model_name} \${$this->single_model_var_name}) + { + return view('{$this->table_name}.show', compact('{$this->single_model_var_name}')); + } + + /** + * Show the form for editing the specified {$this->single_model_var_name}. + * + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\View\View + */ + public function edit({$this->model_name} \${$this->single_model_var_name}) + { + \$this->authorize('update', \${$this->single_model_var_name}); + + return view('{$this->table_name}.edit', compact('{$this->single_model_var_name}')); + } + + /** + * Update the specified {$this->single_model_var_name} in storage. + * + * @param \App\Http\Requests\\{$this->plural_model_name}\UpdateRequest \${$this->single_model_var_name}UpdateForm + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\Routing\Redirector + */ + public function update(UpdateRequest \${$this->single_model_var_name}UpdateForm, {$this->model_name} \${$this->single_model_var_name}) + { + \${$this->single_model_var_name}->update(\${$this->single_model_var_name}UpdateForm->validated()); + + return redirect()->route('{$this->table_name}.show', \${$this->single_model_var_name}); + } + + /** + * Remove the specified {$this->single_model_var_name} from storage. + * + * @param \\{$this->full_model_name} \${$this->single_model_var_name} + * @return \Illuminate\Routing\Redirector + */ + public function destroy({$this->model_name} \${$this->single_model_var_name}) + { + \$this->authorize('delete', \${$this->single_model_var_name}); + + request()->validate([ + '{$this->lang_name}_id' => 'required', + ]); + + if (request('{$this->lang_name}_id') == \${$this->single_model_var_name}->id && \${$this->single_model_var_name}->delete()) { + \$routeParam = request()->only('page', 'q'); + + return redirect()->route('{$this->table_name}.index', \$routeParam); + } + + return back(); + } +} +"; + $this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->model_name}Controller.php"))); + } +} From 725e3715896907ff9072887e34427f46bed807e6 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 11 Oct 2018 10:15:30 +0800 Subject: [PATCH 3/5] Add create and update form request stubs --- src/CrudMake.php | 12 ++ src/Generators/FormRequestGenerator.php | 47 ++++++++ src/stubs/requests/create-request.stub | 45 +++++++ src/stubs/requests/update-request.stub | 31 +++++ .../FullCrudFormRequestOptionsTest.php | 131 +++++++++++++++++++++ 5 files changed, 266 insertions(+) create mode 100644 src/Generators/FormRequestGenerator.php create mode 100644 src/stubs/requests/create-request.stub create mode 100644 src/stubs/requests/update-request.stub diff --git a/src/CrudMake.php b/src/CrudMake.php index 5426eb4..757de13 100644 --- a/src/CrudMake.php +++ b/src/CrudMake.php @@ -56,6 +56,10 @@ class CrudMake extends GeneratorCommand $this->generateResources(); $this->generateTestFiles(); + if ($this->option('form-requests')) { + $this->generateRequestClasses(); + } + $this->info('CRUD files generated successfully!'); } @@ -116,4 +120,12 @@ class CrudMake extends GeneratorCommand app('Luthfi\CrudGenerator\Generators\IndexViewGenerator', ['command' => $this])->generate(); app('Luthfi\CrudGenerator\Generators\ShowViewGenerator', ['command' => $this])->generate(); } + + /** + * Generate Form Requests + */ + public function generateRequestClasses() + { + app('Luthfi\CrudGenerator\Generators\FormRequestGenerator', ['command' => $this])->generate(); + } } diff --git a/src/Generators/FormRequestGenerator.php b/src/Generators/FormRequestGenerator.php new file mode 100644 index 0000000..74948b6 --- /dev/null +++ b/src/Generators/FormRequestGenerator.php @@ -0,0 +1,47 @@ +modelNames['model_name']; + $pluralModelName = $this->modelNames['plural_model_name']; + + $requestPath = $this->makeDirectory(app_path('Http/Requests/'.$pluralModelName)); + $createRequestPath = $requestPath.'/CreateRequest.php'; + $this->generateFile($createRequestPath, $this->getContent('requests/create-request')); + + $updateRequestPath = $requestPath.'/UpdateRequest.php'; + $this->generateFile($updateRequestPath, $this->getContent('requests/update-request')); + + $this->command->info($modelName.' Form Requests generated.'); + } + + /** + * {@inheritDoc} + */ + public function getContent(string $stubName) + { + $stub = $this->getStubFileContent($stubName); + + $controllerFileContent = $this->replaceStubString($stub); + + $appNamespace = $this->getAppNamespace(); + + $controllerFileContent = str_replace( + "App\Http\Controllers", + "{$appNamespace}Http\Controllers", + $controllerFileContent + ); + + return $controllerFileContent; + } +} diff --git a/src/stubs/requests/create-request.stub b/src/stubs/requests/create-request.stub new file mode 100644 index 0000000..72557f6 --- /dev/null +++ b/src/stubs/requests/create-request.stub @@ -0,0 +1,45 @@ +user()->can('create', new Master); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'name' => 'required|max:60', + 'description' => 'nullable|max:255', + ]; + } + + /** + * Save proposal to database. + * + * @return \fullMstr + */ + public function save() + { + $newMaster = $this->validated(); + $newMaster['creator_id'] = auth()->id(); + + return Master::create($newMaster); + } +} diff --git a/src/stubs/requests/update-request.stub b/src/stubs/requests/update-request.stub new file mode 100644 index 0000000..2173556 --- /dev/null +++ b/src/stubs/requests/update-request.stub @@ -0,0 +1,31 @@ +user()->can('update', $this->route('master')); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'name' => 'required|max:60', + 'description' => 'nullable|max:255', + ]; + } +} diff --git a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php index 9dda61f..7c6fdb3 100644 --- a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php +++ b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php @@ -3,10 +3,41 @@ namespace Tests\CommandOptions; use Tests\TestCase; +use Illuminate\Contracts\Console\Kernel; class FullCrudFormRequestOptionsTest extends TestCase { /** @test */ + public function it_can_generate_form_request_classes() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]); + + $this->assertNotContains("{$this->model_name} model already exists.", app(Kernel::class)->output()); + + $this->assertFileExists(app_path($this->model_name.'.php')); + $this->assertFileExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); + $this->assertFileExists(app_path("Http/Requests/{$this->plural_model_name}/CreateRequest.php")); + $this->assertFileExists(app_path("Http/Requests/{$this->plural_model_name}/UpdateRequest.php")); + + $migrationFilePath = database_path('migrations/'.date('Y_m_d_His').'_create_'.$this->table_name.'_table.php'); + $this->assertFileExists($migrationFilePath); + + $this->assertFileExists(resource_path("views/{$this->table_name}/index.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/create.blade.php")); + $this->assertFileExists(resource_path("views/{$this->table_name}/edit.blade.php")); + $this->assertFileNotExists(resource_path("views/{$this->table_name}/forms.blade.php")); + + $localeConfig = config('app.locale'); + $this->assertFileExists(resource_path("lang/{$localeConfig}/{$this->lang_name}.php")); + + $this->assertFileExists(base_path("routes/web.php")); + $this->assertFileExists(app_path("Policies/{$this->model_name}Policy.php")); + $this->assertFileExists(database_path("factories/{$this->model_name}Factory.php")); + $this->assertFileExists(base_path("tests/Unit/Models/{$this->model_name}Test.php")); + $this->assertFileExists(base_path("tests/Feature/Manage{$this->model_name}Test.php")); + } + + /** @test */ public function it_can_generate_controller_file_with_form_requests_class() { $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]); @@ -124,4 +155,104 @@ class {$this->model_name}Controller extends Controller "; $this->assertEquals($ctrlClassContent, file_get_contents(app_path("Http/Controllers/{$this->model_name}Controller.php"))); } + + /** @test */ + public function it_generates_correct_create_form_request_file_content() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]); + + $classFilePath = app_path("Http/Requests/{$this->plural_model_name}/CreateRequest.php"); + + $this->assertFileExists($classFilePath); + $formRequestClassContent = "plural_model_name}; + +use {$this->full_model_name}; +use Illuminate\Foundation\Http\FormRequest; + +class CreateRequest extends FormRequest +{ + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return \$this->user()->can('create', new {$this->model_name}); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'name' => 'required|max:60', + 'description' => 'nullable|max:255', + ]; + } + + /** + * Save proposal to database. + * + * @return \\{$this->full_model_name} + */ + public function save() + { + \$new{$this->model_name} = \$this->validated(); + \$new{$this->model_name}['creator_id'] = auth()->id(); + + return {$this->model_name}::create(\$new{$this->model_name}); + } +} +"; + $this->assertEquals($formRequestClassContent, file_get_contents($classFilePath)); + } + + /** @test */ + public function it_generates_correct_update_form_request_file_content() + { + $this->artisan('make:crud', ['name' => $this->model_name, '--no-interaction' => true, '--form-requests' => true]); + + $classFilePath = app_path("Http/Requests/{$this->plural_model_name}/UpdateRequest.php"); + + $this->assertFileExists($classFilePath); + $formRequestClassContent = "plural_model_name}; + +use Illuminate\Foundation\Http\FormRequest; + +class UpdateRequest extends FormRequest +{ + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() + { + return \$this->user()->can('update', \$this->route('{$this->lang_name}')); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'name' => 'required|max:60', + 'description' => 'nullable|max:255', + ]; + } +} +"; + $this->assertEquals($formRequestClassContent, file_get_contents($classFilePath)); + } } From 66591d9e33a96aeee55adba4bd7cc65b594faf42 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 11 Oct 2018 10:19:52 +0800 Subject: [PATCH 4/5] Add use statement on "form request"-ed controller --- src/stubs/controllers/full-formrequests.stub | 2 ++ tests/CommandOptions/FullCrudFormRequestOptionsTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/stubs/controllers/full-formrequests.stub b/src/stubs/controllers/full-formrequests.stub index 62ba3aa..b5a4ba4 100644 --- a/src/stubs/controllers/full-formrequests.stub +++ b/src/stubs/controllers/full-formrequests.stub @@ -4,6 +4,8 @@ namespace App\Http\Controllers; use fullMstr; use Illuminate\Http\Request; +use App\Http\Requests\Masters\CreateRequest; +use App\Http\Requests\Masters\UpdateRequest; class MasterController extends Controller { diff --git a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php index 7c6fdb3..6f31eae 100644 --- a/tests/CommandOptions/FullCrudFormRequestOptionsTest.php +++ b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php @@ -49,6 +49,8 @@ namespace App\Http\Controllers; use {$this->full_model_name}; use Illuminate\Http\Request; +use App\Http\Requests\\{$this->plural_model_name}\CreateRequest; +use App\Http\Requests\\{$this->plural_model_name}\UpdateRequest; class {$this->model_name}Controller extends Controller { From d88a4d09bf821d3222a6091837396f1d6f397083 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Thu, 11 Oct 2018 10:27:45 +0800 Subject: [PATCH 5/5] Refactor Request generator - closes #4 Update docblocks --- src/Generators/FormRequestGenerator.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Generators/FormRequestGenerator.php b/src/Generators/FormRequestGenerator.php index 74948b6..0be6a89 100644 --- a/src/Generators/FormRequestGenerator.php +++ b/src/Generators/FormRequestGenerator.php @@ -8,7 +8,10 @@ namespace Luthfi\CrudGenerator\Generators; class FormRequestGenerator extends BaseGenerator { /** - * {@inheritDoc} + * Generate class file content. + * + * @param string $type Type of crud + * @return void */ public function generate(string $type = 'full') { @@ -16,17 +19,22 @@ class FormRequestGenerator extends BaseGenerator $pluralModelName = $this->modelNames['plural_model_name']; $requestPath = $this->makeDirectory(app_path('Http/Requests/'.$pluralModelName)); - $createRequestPath = $requestPath.'/CreateRequest.php'; - $this->generateFile($createRequestPath, $this->getContent('requests/create-request')); - $updateRequestPath = $requestPath.'/UpdateRequest.php'; - $this->generateFile($updateRequestPath, $this->getContent('requests/update-request')); + $this->generateFile( + $requestPath.'/CreateRequest.php', $this->getContent('requests/create-request') + ); + $this->generateFile( + $requestPath.'/UpdateRequest.php', $this->getContent('requests/update-request') + ); $this->command->info($modelName.' Form Requests generated.'); } /** - * {@inheritDoc} + * Get class file content. + * + * @param string $stubName Name of stub file + * @return string */ public function getContent(string $stubName) {