From 912a406f6970186bdc120eb5ba102a4dee0b86aa Mon Sep 17 00:00:00 2001 From: davood Date: Thu, 14 Dec 2023 18:18:44 +0330 Subject: [PATCH 1/5] publish stubs command added --- src/Console/Commands/PublishStubsCommand.php | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Console/Commands/PublishStubsCommand.php diff --git a/src/Console/Commands/PublishStubsCommand.php b/src/Console/Commands/PublishStubsCommand.php new file mode 100644 index 0000000..74862f3 --- /dev/null +++ b/src/Console/Commands/PublishStubsCommand.php @@ -0,0 +1,37 @@ +call('vendor:publish', [ + '--tag' => 'validatedDTO-stubs', + '--force' => $this->option('force'), + ]); + } +} From b07375a9fdb0371fc03c3527b80c72e247503656 Mon Sep 17 00:00:00 2001 From: davood Date: Thu, 14 Dec 2023 18:19:33 +0330 Subject: [PATCH 2/5] test for publish stubs command --- tests/Feature/PublishStubsCommandTest.php | 114 ++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/Feature/PublishStubsCommandTest.php diff --git a/tests/Feature/PublishStubsCommandTest.php b/tests/Feature/PublishStubsCommandTest.php new file mode 100644 index 0000000..56c3436 --- /dev/null +++ b/tests/Feature/PublishStubsCommandTest.php @@ -0,0 +1,114 @@ +artisan('dto:stubs') + ->assertExitCode(0); + + expect(base_path('stubs/resource_dto.stub'))->toBeFile(); + expect(base_path('stubs/simple_dto.stub'))->toBeFile(); + expect(base_path('stubs/dto.stub'))->toBeFile(); +}); + +it('publishes the package stubs with force flag', function () { + $this->artisan('dto:stubs', ['--force' => true]) + ->assertExitCode(0); + + expect(base_path('stubs/resource_dto.stub'))->toBeFile(); + expect(base_path('stubs/simple_dto.stub'))->toBeFile(); + expect(base_path('stubs/dto.stub'))->toBeFile(); + + expect(base_path('stubs/dto.stub'))->toBeFileWithContent(UserStubDTO()); + expect(base_path('stubs/simple_dto.stub'))->toBeFileWithContent(SimpleUserStubDTO()); + expect(base_path('stubs/resource_dto.stub'))->toBeFileWithContent(UserResourceStubDTO()); +}); + +/** + * Content of the expected UserDTO class + */ +function UserStubDTO(): string +{ + return << Date: Thu, 14 Dec 2023 18:20:06 +0330 Subject: [PATCH 3/5] registered to service provider --- src/Providers/ValidatedDTOServiceProvider.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Providers/ValidatedDTOServiceProvider.php b/src/Providers/ValidatedDTOServiceProvider.php index e2b925b..47437db 100644 --- a/src/Providers/ValidatedDTOServiceProvider.php +++ b/src/Providers/ValidatedDTOServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider; use WendellAdriel\ValidatedDTO\Console\Commands\MakeDTOCommand; +use WendellAdriel\ValidatedDTO\Console\Commands\PublishStubsCommand; use WendellAdriel\ValidatedDTO\Contracts\BaseDTO; final class ValidatedDTOServiceProvider extends ServiceProvider @@ -16,7 +17,10 @@ final class ValidatedDTOServiceProvider extends ServiceProvider public function boot() { if ($this->app->runningInConsole()) { - $this->commands(MakeDTOCommand::class); + $this->commands([ + MakeDTOCommand::class, + PublishStubsCommand::class, + ]); } $this->publishes( @@ -25,6 +29,12 @@ public function boot() ], 'config' ); + + $this->publishes([ + __DIR__ . '/../../src/Console/stubs/resource_dto.stub' => base_path('stubs/resource_dto.stub'), + __DIR__ . '/../../src/Console/stubs/simple_dto.stub' => base_path('stubs/simple_dto.stub'), + __DIR__ . '/../../src/Console/stubs/dto.stub' => base_path('stubs/dto.stub'), + ], 'validatedDTO-stubs'); } /** From c4d9188384a1769ef12c669c4503999ee687ed5c Mon Sep 17 00:00:00 2001 From: davood Date: Thu, 14 Dec 2023 18:21:05 +0330 Subject: [PATCH 4/5] now makeDTO uses custom stubs if they exists --- src/Console/Commands/MakeDTOCommand.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/MakeDTOCommand.php b/src/Console/Commands/MakeDTOCommand.php index ae13239..1e56710 100644 --- a/src/Console/Commands/MakeDTOCommand.php +++ b/src/Console/Commands/MakeDTOCommand.php @@ -56,11 +56,21 @@ protected function getPath($name) protected function getStub(): string { - return match (true) { - $this->option('resource') => __DIR__ . '/../stubs/resource_dto.stub', - $this->option('simple') => __DIR__ . '/../stubs/simple_dto.stub', - default => __DIR__ . '/../stubs/dto.stub', - }; + return $this->resolveStubPath(match (true) { + $this->option('resource') => 'resource_dto.stub', + $this->option('simple') => 'simple_dto.stub', + default => 'dto.stub', + }); + } + + /** + * Resolve the fully-qualified path to the stub. + */ + protected function resolveStubPath(string $stub): string + { + return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) + ? $customPath + : __DIR__ . '/../stubs/' . $stub; } protected function getOptions(): array From 409f8dd628564f489d8cb715ce945729bc4a6e63 Mon Sep 17 00:00:00 2001 From: davood Date: Fri, 15 Dec 2023 19:29:59 +0330 Subject: [PATCH 5/5] missing properties resolved --- src/Console/Commands/PublishStubsCommand.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Console/Commands/PublishStubsCommand.php b/src/Console/Commands/PublishStubsCommand.php index 74862f3..a1b8645 100644 --- a/src/Console/Commands/PublishStubsCommand.php +++ b/src/Console/Commands/PublishStubsCommand.php @@ -10,6 +10,18 @@ #[AsCommand(name: 'dto:stubs')] final class PublishStubsCommand extends Command { + /** + * @var string + */ + protected $name = 'dto:stubs'; + + /** + * @var string|null + * + * @deprecated + */ + protected static $defaultName = 'dto:stubs'; + /** * The name and signature of the console command. *