Skip to content

Commit

Permalink
Merge pull request #10 from RapidKit/feature/request-class-based-on-base
Browse files Browse the repository at this point in the history
feature: generate request class based on base
  • Loading branch information
ramaID authored Jun 16, 2024
2 parents 407c794 + 6508ee2 commit cccefec
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.3'
coverage: none

- name: Install composer dependencies
Expand Down
13 changes: 10 additions & 3 deletions src/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ public function handle(): int
$path = app_path();

$filePath = $path.'/Data/'.$name.'Data.php';
$this->info('Generating data object class: '.$filePath);
$this->info('Generated data object class: '.$filePath);
Artisan::call('restify:gen-data '.$name.'Data');

$filePath = $path.'/Http/Controllers/Api/'.$name.'Controller.php';

$this->info('Generating controller class: '.$filePath);
$this->info('Generated controller class: '.$filePath);
Artisan::call('restify:gen-controller '.$name.'Controller');

$filePath = $path.'/Http/Requests/'.$name.'StoreRequest.php';
$this->info('Generated store request class: '.$filePath);
Artisan::call('restify:gen-request '.$name.'StoreRequest');

$filePath = $path.'/Http/Requests/'.$name.'UpdateRequest.php';
$this->info('Generated update request class: '.$filePath);
Artisan::call('restify:gen-request '.$name.'UpdateRequest');

return self::SUCCESS;
}
}
32 changes: 32 additions & 0 deletions src/Commands/GenerateRequestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace RapidKit\LaravelRestify\Commands;

use Illuminate\Console\GeneratorCommand;

class GenerateRequestCommand extends GeneratorCommand
{
public $signature = 'restify:gen-request {name}';

public $description = 'Generate a new Restify request class for storing resource.';

protected function getStub()
{
return __DIR__.'/../../stubs/request.stub';
}

protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\Requests';
}

protected function replaceClass($stub, $name)
{
$class = str_replace($this->getNamespace($name).'\\', '', $name);

// Do string replacement
return str_replace('{{ name }}', $class, $stub);
}
}
2 changes: 2 additions & 0 deletions src/LaravelRestifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RapidKit\LaravelRestify\Commands\GenerateCommand;
use RapidKit\LaravelRestify\Commands\GenerateControllerCommand;
use RapidKit\LaravelRestify\Commands\GenerateDataCommand;
use RapidKit\LaravelRestify\Commands\GenerateRequestCommand;
use RapidKit\LaravelRestify\Commands\SetupCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand All @@ -25,6 +26,7 @@ public function configurePackage(Package $package): void
->hasCommand(SetupCommand::class)
->hasCommand(GenerateDataCommand::class)
->hasCommand(GenerateControllerCommand::class)
->hasCommand(GenerateRequestCommand::class)
->hasCommand(GenerateCommand::class);
}
}
6 changes: 3 additions & 3 deletions stubs/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ class {{ name }} extends \RapidKit\LaravelRestify\Bases\BaseController
return $this->model;
}

// public function store(BaseRequest $base): Author
// public function store(BaseRequest $base): Model
// {
// return parent::store(AuthorStoreRequest::createFrom($base));
// return parent::store(StoreRequest::createFrom($base));
// }

// public function update(BaseRequest $base, string|int $id): \Illuminate\Http\Response
// {
// return parent::update(AuthorUpdateRequest::createFrom($base), $id);
// return parent::update(UpdateRequest::createFrom($base), $id);
// }
}
26 changes: 26 additions & 0 deletions stubs/request.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Requests;

use App\Data\AuthorData as DTO; // TODO: sesuaikan class model
use RapidKit\LaravelRestify\Bases\BaseRequest;

class {{ name }} extends BaseRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
];
}

public function toDto()
{
return DTO::from(...$this->validated());
}
}
4 changes: 4 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
artisan('restify:gen-controller CategoryController')->assertExitCode(0);
});

it('can generate request stub', function () {
artisan('restify:gen-request AuthorStoreRequest')->assertExitCode(0);
});

it('can generate file from stubs', function () {
artisan('restify:gen Category')->assertExitCode(0);
});

0 comments on commit cccefec

Please sign in to comment.