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..757de13 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}'; /** @@ -55,6 +56,10 @@ class CrudMake extends GeneratorCommand $this->generateResources(); $this->generateTestFiles(); + if ($this->option('form-requests')) { + $this->generateRequestClasses(); + } + $this->info('CRUD files generated successfully!'); } @@ -115,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/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}'; /** 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/Generators/FormRequestGenerator.php b/src/Generators/FormRequestGenerator.php new file mode 100644 index 0000000..0be6a89 --- /dev/null +++ b/src/Generators/FormRequestGenerator.php @@ -0,0 +1,55 @@ +modelNames['model_name']; + $pluralModelName = $this->modelNames['plural_model_name']; + + $requestPath = $this->makeDirectory(app_path('Http/Requests/'.$pluralModelName)); + + $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.'); + } + + /** + * Get class file content. + * + * @param string $stubName Name of stub file + * @return string + */ + 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/controllers/full-formrequests.stub b/src/stubs/controllers/full-formrequests.stub new file mode 100644 index 0000000..b5a4ba4 --- /dev/null +++ b/src/stubs/controllers/full-formrequests.stub @@ -0,0 +1,111 @@ +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/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 new file mode 100644 index 0000000..6f31eae --- /dev/null +++ b/tests/CommandOptions/FullCrudFormRequestOptionsTest.php @@ -0,0 +1,260 @@ +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]); + + $this->assertFileExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); + $ctrlClassContent = "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 +{ + /** + * 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"))); + } + + /** @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)); + } +}