Browse Source
Merge pull request #5 from nafiesl/form-requests-option
Merge pull request #5 from nafiesl/form-requests-option
Add form requests command optiontags/1.2.9
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 521 additions and 0 deletions
-
1src/CrudApiMake.php
-
13src/CrudMake.php
-
1src/CrudSimpleMake.php
-
4src/Generators/ControllerGenerator.php
-
55src/Generators/FormRequestGenerator.php
-
111src/stubs/controllers/full-formrequests.stub
-
45src/stubs/requests/create-request.stub
-
31src/stubs/requests/update-request.stub
-
260tests/CommandOptions/FullCrudFormRequestOptionsTest.php
@ -0,0 +1,55 @@ |
|||
<?php |
|||
|
|||
namespace Luthfi\CrudGenerator\Generators; |
|||
|
|||
/** |
|||
* Form Request Generator Class |
|||
*/ |
|||
class FormRequestGenerator extends BaseGenerator |
|||
{ |
|||
/** |
|||
* Generate class file content. |
|||
* |
|||
* @param string $type Type of crud |
|||
* @return void |
|||
*/ |
|||
public function generate(string $type = 'full') |
|||
{ |
|||
$modelName = $this->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; |
|||
} |
|||
} |
|||
@ -0,0 +1,111 @@ |
|||
<?php |
|||
|
|||
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 |
|||
{ |
|||
/** |
|||
* Display a listing of the singleMstr. |
|||
* |
|||
* @return \Illuminate\View\View |
|||
*/ |
|||
public function index() |
|||
{ |
|||
$singleMstrQuery = Master::query(); |
|||
$singleMstrQuery->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(); |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Requests\Masters; |
|||
|
|||
use fullMstr; |
|||
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 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); |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Requests\Masters; |
|||
|
|||
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('master')); |
|||
} |
|||
|
|||
/** |
|||
* Get the validation rules that apply to the request. |
|||
* |
|||
* @return array |
|||
*/ |
|||
public function rules() |
|||
{ |
|||
return [ |
|||
'name' => 'required|max:60', |
|||
'description' => 'nullable|max:255', |
|||
]; |
|||
} |
|||
} |
|||
@ -0,0 +1,260 @@ |
|||
<?php |
|||
|
|||
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]); |
|||
|
|||
$this->assertFileExists(app_path("Http/Controllers/{$this->model_name}Controller.php")); |
|||
$ctrlClassContent = "<?php
|
|||
|
|||
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 |
|||
{ |
|||
/** |
|||
* 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 = "<?php
|
|||
|
|||
namespace App\Http\Requests\\{$this->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 = "<?php
|
|||
|
|||
namespace App\Http\Requests\\{$this->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)); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue