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 diff --git a/src/Console/Commands/PublishStubsCommand.php b/src/Console/Commands/PublishStubsCommand.php new file mode 100644 index 0000000..a1b8645 --- /dev/null +++ b/src/Console/Commands/PublishStubsCommand.php @@ -0,0 +1,49 @@ +call('vendor:publish', [ + '--tag' => 'validatedDTO-stubs', + '--force' => $this->option('force'), + ]); + } +} 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'); } /** 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 <<