5 changed files with 266 additions and 0 deletions
-
12src/CrudMake.php
-
47src/Generators/FormRequestGenerator.php
-
45src/stubs/requests/create-request.stub
-
31src/stubs/requests/update-request.stub
-
131tests/CommandOptions/FullCrudFormRequestOptionsTest.php
@ -0,0 +1,47 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Luthfi\CrudGenerator\Generators; |
||||
|
|
||||
|
/** |
||||
|
* Form Request Generator Class |
||||
|
*/ |
||||
|
class FormRequestGenerator extends BaseGenerator |
||||
|
{ |
||||
|
/** |
||||
|
* {@inheritDoc} |
||||
|
*/ |
||||
|
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)); |
||||
|
$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; |
||||
|
} |
||||
|
} |
||||
@ -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', |
||||
|
]; |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue