Skip to content

Commit

Permalink
Merge pull request #69 from DavoodGhanbarpour/customizable_stubs
Browse files Browse the repository at this point in the history
Customizable stubs
  • Loading branch information
WendellAdriel authored Feb 5, 2024
2 parents 13bef8e + 409f8dd commit 13496f3
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/Console/Commands/MakeDTOCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions src/Console/Commands/PublishStubsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace WendellAdriel\ValidatedDTO\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[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.
*
* @var string
*/
protected $signature = 'dto:stubs {--force : Overwrite any existing files}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Publish all stubs that are available for customization';

/**
* Execute the console command.
*/
public function handle(): void
{
$this->call('vendor:publish', [
'--tag' => 'validatedDTO-stubs',
'--force' => $this->option('force'),
]);
}
}
12 changes: 11 additions & 1 deletion src/Providers/ValidatedDTOServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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');
}

/**
Expand Down
114 changes: 114 additions & 0 deletions tests/Feature/PublishStubsCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

it('publishes the package stubs', function () {
$this->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 <<<CLASS
<?php
namespace {{ namespace }};
use WendellAdriel\ValidatedDTO\ValidatedDTO;
class {{ class }} extends ValidatedDTO
{
protected function rules(): array
{
return [];
}
protected function defaults(): array
{
return [];
}
protected function casts(): array
{
return [];
}
}
CLASS;
}

/**
* Content of the expected SimpleUserDTO class
*/
function SimpleUserStubDTO(): string
{
return <<<CLASS
<?php
namespace {{ namespace }};
use WendellAdriel\ValidatedDTO\SimpleDTO;
class {{ class }} extends SimpleDTO
{
protected function defaults(): array
{
return [];
}
protected function casts(): array
{
return [];
}
}
CLASS;
}

/**
* Content of the expected UserResourceDTO class
*/
function UserResourceStubDTO(): string
{
return <<<CLASS
<?php
namespace {{ namespace }};
use WendellAdriel\ValidatedDTO\ResourceDTO;
class {{ class }} extends ResourceDTO
{
protected function defaults(): array
{
return [];
}
protected function casts(): array
{
return [];
}
}
CLASS;
}

0 comments on commit 13496f3

Please sign in to comment.