diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index c1a2fa8..d9306d6 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -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 diff --git a/src/Commands/GenerateCommand.php b/src/Commands/GenerateCommand.php index 3bc8a30..6d3ef51 100644 --- a/src/Commands/GenerateCommand.php +++ b/src/Commands/GenerateCommand.php @@ -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; } } diff --git a/src/Commands/GenerateRequestCommand.php b/src/Commands/GenerateRequestCommand.php new file mode 100644 index 0000000..ee26612 --- /dev/null +++ b/src/Commands/GenerateRequestCommand.php @@ -0,0 +1,32 @@ +getNamespace($name).'\\', '', $name); + + // Do string replacement + return str_replace('{{ name }}', $class, $stub); + } +} diff --git a/src/LaravelRestifyServiceProvider.php b/src/LaravelRestifyServiceProvider.php index bba2f21..dbf6773 100644 --- a/src/LaravelRestifyServiceProvider.php +++ b/src/LaravelRestifyServiceProvider.php @@ -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; @@ -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); } } diff --git a/stubs/controller.stub b/stubs/controller.stub index 6922494..7dffb9c 100644 --- a/stubs/controller.stub +++ b/stubs/controller.stub @@ -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); // } } diff --git a/stubs/request.stub b/stubs/request.stub new file mode 100644 index 0000000..5fdc68d --- /dev/null +++ b/stubs/request.stub @@ -0,0 +1,26 @@ +|string> + */ + public function rules(): array + { + return [ + // + ]; + } + + public function toDto() + { + return DTO::from(...$this->validated()); + } +} diff --git a/tests/PackageTest.php b/tests/PackageTest.php index 21c5ad7..8b6b6e9 100644 --- a/tests/PackageTest.php +++ b/tests/PackageTest.php @@ -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); });