Skip to content

Commit

Permalink
Refactoring stubs, WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Oct 22, 2024
1 parent c944c79 commit 36f2796
Show file tree
Hide file tree
Showing 48 changed files with 216 additions and 228 deletions.
55 changes: 55 additions & 0 deletions src/Commands/Concerns/HasDomainStubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Facades\DDD;

trait HasDomainStubs
{
use InteractsWithStubs;

protected function resolveDddStubPath($path)
{
$path = str($path)
->basename()
->ltrim('/\\')
->toString();

$publishedPath = resource_path('stubs/ddd/'.$path);

if (file_exists($publishedPath)) {
return $publishedPath;
}

$legacyPublishedPath = Str::replaceLast('.stub', '.php.stub', $publishedPath);

if (file_exists($legacyPublishedPath)) {
return $legacyPublishedPath;
}

return DDD::packagePath('stubs/'.$path);
}

protected function resolveStubPath($stub)
{
$defaultStub = parent::resolveStubPath($stub);

$stubFilename = basename($stub);

// Check if there is a user-published stub
$publishedPath = app()->basePath('stubs/ddd/'.$stubFilename);

if (file_exists($publishedPath)) {
return $publishedPath;
}

$legacyPublishedPath = Str::replaceLast('.stub', '.php.stub', $publishedPath);

if (file_exists($legacyPublishedPath)) {
return $legacyPublishedPath;
}

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

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

trait InteractsWithStubs
{
protected function fillPlaceholder($stub, $placeholder, $value)
{
return str_replace(["{{$placeholder}}", "{{ $placeholder }}"], $value, $stub);
}

protected function preparePlaceholders(): array
{
return [];
}

protected function applyPlaceholders($stub)
{
$placeholders = $this->preparePlaceholders();

foreach ($placeholders as $placeholder => $value) {
$stub = $this->fillPlaceholder($stub, $placeholder, $value ?? '');
}

return $stub;
}

protected function buildClass($name)
{
return $this->applyPlaceholders(parent::buildClass($name));
}
}
2 changes: 1 addition & 1 deletion src/Commands/DomainActionMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DomainActionMakeCommand extends DomainGeneratorCommand

protected function getStub()
{
return $this->resolveStubPath('action.php.stub');
return $this->resolveDddStubPath('action.stub');
}

protected function preparePlaceholders(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DomainBaseModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function getArguments()

protected function getStub()
{
return $this->resolveStubPath('base-model.php.stub');
return $this->resolveDddStubPath('base-model.stub');
}

protected function getRelativeDomainNamespace(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DomainBaseViewModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function getArguments()

protected function getStub()
{
return $this->resolveStubPath('base-view-model.php.stub');
return $this->resolveDddStubPath('base-view-model.stub');
}

protected function getRelativeDomainNamespace(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/DomainCastMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\CastMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainCastMakeCommand extends CastMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:cast';
}
4 changes: 3 additions & 1 deletion src/Commands/DomainChannelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ChannelMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainChannelMakeCommand extends ChannelMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:channel';
}
4 changes: 3 additions & 1 deletion src/Commands/DomainClassMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ClassMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainClassMakeCommand extends ClassMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:class';
}
4 changes: 3 additions & 1 deletion src/Commands/DomainConsoleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ConsoleMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainConsoleMakeCommand extends ConsoleMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:command';
}
2 changes: 2 additions & 0 deletions src/Commands/DomainControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Illuminate\Routing\Console\ControllerMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ForwardsToDomainCommands;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

use function Laravel\Prompts\confirm;

class DomainControllerMakeCommand extends ControllerMakeCommand
{
use ForwardsToDomainCommands,
HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:controller';
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DomainDtoMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function configure()

protected function getStub()
{
return $this->resolveStubPath('dto.php.stub');
return $this->resolveDddStubPath('dto.stub');
}

protected function getRelativeDomainNamespace(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/DomainEnumMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\EnumMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainEnumMakeCommand extends EnumMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:enum';
}
4 changes: 3 additions & 1 deletion src/Commands/DomainEventMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\EventMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainEventMakeCommand extends EventMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:event';
}
4 changes: 3 additions & 1 deletion src/Commands/DomainExceptionMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ExceptionMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainExceptionMakeCommand extends ExceptionMakeCommand
{
use ResolvesDomainFromInput;
use HasDomainStubs,
ResolvesDomainFromInput;

protected $name = 'ddd:exception';
}
55 changes: 12 additions & 43 deletions src/Commands/DomainFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,25 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Symfony\Component\Console\Input\InputOption;
use Illuminate\Database\Console\Factories\FactoryMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainFactoryMakeCommand extends DomainGeneratorCommand
class DomainFactoryMakeCommand extends FactoryMakeCommand
{
protected $name = 'ddd:factory';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a domain model factory';
use HasDomainStubs,
ResolvesDomainFromInput;

protected $type = 'Factory';

protected function getOptions()
{
return [
...parent::getOptions(),
['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
];
}
protected $name = 'ddd:factory';

protected function getStub()
{
return $this->resolveStubPath('factory.php.stub');
return $this->resolveDddStubPath('factory.stub');
}

protected function getPath($name)
protected function getNamespace($name)
{
if (! str_ends_with($name, 'Factory')) {
$name .= 'Factory';
}

return parent::getPath($name);
}

protected function getFactoryName()
{
$name = $this->getNameInput();

return str_ends_with($name, 'Factory')
? substr($name, 0, -7)
: $name;
return $this->domain->namespaceFor('factory');
}

protected function preparePlaceholders(): array
Expand All @@ -60,16 +35,10 @@ protected function preparePlaceholders(): array

$domainFactory = $domain->factory($name);

// dump('preparing placeholders', [
// 'name' => $name,
// 'modelName' => $modelName,
// 'domainFactory' => $domainFactory,
// ]);

return [
'namespacedModel' => $domainModel->fullyQualifiedName,
'model' => class_basename($domainModel->fullyQualifiedName),
'factory' => $this->getFactoryName(),
'factory' => $domainFactory->name,
'namespace' => $domainFactory->namespace,
];
}
Expand All @@ -80,6 +49,6 @@ protected function guessModelName($name)
$name = substr($name, 0, -7);
}

return $this->domain->model($name)->name;
return $this->domain->model(class_basename($name))->name;
}
}
Loading

0 comments on commit 36f2796

Please sign in to comment.