Skip to content

Commit

Permalink
Updates:
Browse files Browse the repository at this point in the history
- normalize domain stub trait across all domain generators
- ensure ddd:controller is able to check for base controller existence
- refine ddd:model stub replacements
  • Loading branch information
JasperTey committed Nov 9, 2024
1 parent 69b82a3 commit abcbee5
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 33 deletions.
53 changes: 35 additions & 18 deletions src/Commands/Concerns/HasDomainStubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@

trait HasDomainStubs
{
use InteractsWithStubs;
protected function getStub()
{
$defaultStub = parent::getStub();

$stubFilename = basename($defaultStub);

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

// Also check for legacy stub extensions
if (file_exists($legacyPublishedPath = Str::replaceLast('.stub', '.php.stub', $publishedPath))) {
return $legacyPublishedPath;
}

return $defaultStub;
}

protected function resolveDddStubPath($path)
{
Expand All @@ -16,7 +33,7 @@ protected function resolveDddStubPath($path)
->ltrim('/\\')
->toString();

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

if (file_exists($publishedPath)) {
return $publishedPath;
Expand All @@ -28,28 +45,28 @@ protected function resolveDddStubPath($path)
return $legacyPublishedPath;
}

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

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

$stubFilename = basename($stub);
// $stubFilename = basename($stub);

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

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

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

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

return $defaultStub;
}
// return $defaultStub;
// }
}
4 changes: 4 additions & 0 deletions src/Commands/DomainActionMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;

class DomainActionMakeCommand extends DomainGeneratorCommand
{
use HasDomainStubs;

protected $name = 'ddd:action';

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/DomainBaseModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Symfony\Component\Console\Input\InputArgument;

class DomainBaseModelMakeCommand extends DomainGeneratorCommand
{
use HasDomainStubs;

protected $name = 'ddd:base-model';

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/DomainBaseViewModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Symfony\Component\Console\Input\InputArgument;

class DomainBaseViewModelMakeCommand extends DomainGeneratorCommand
{
use HasDomainStubs;

protected $name = 'ddd:base-view-model';

/**
Expand Down
42 changes: 34 additions & 8 deletions src/Commands/DomainControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Routing\Console\ControllerMakeCommand;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\ForwardsToDomainCommands;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;
Expand All @@ -11,8 +12,8 @@

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

protected $name = 'ddd:controller';
Expand Down Expand Up @@ -62,23 +63,48 @@ protected function buildFormRequestReplacements(array $replace, $modelClass)
);
}

$namespacedRequests = $namespace.'\\'.$storeRequestClass.';';
$namespacedRequests = $namespace . '\\' . $storeRequestClass . ';';

if ($storeRequestClass !== $updateRequestClass) {
$namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';';
$namespacedRequests .= PHP_EOL . 'use ' . $namespace . '\\' . $updateRequestClass . ';';
}

return array_merge($replace, [
'{{ storeRequest }}' => $storeRequestClass,
'{{storeRequest}}' => $storeRequestClass,
'{{ updateRequest }}' => $updateRequestClass,
'{{updateRequest}}' => $updateRequestClass,
'{{ namespacedStoreRequest }}' => $namespace.'\\'.$storeRequestClass,
'{{namespacedStoreRequest}}' => $namespace.'\\'.$storeRequestClass,
'{{ namespacedUpdateRequest }}' => $namespace.'\\'.$updateRequestClass,
'{{namespacedUpdateRequest}}' => $namespace.'\\'.$updateRequestClass,
'{{ namespacedStoreRequest }}' => $namespace . '\\' . $storeRequestClass,
'{{namespacedStoreRequest}}' => $namespace . '\\' . $storeRequestClass,
'{{ namespacedUpdateRequest }}' => $namespace . '\\' . $updateRequestClass,
'{{namespacedUpdateRequest}}' => $namespace . '\\' . $updateRequestClass,
'{{ namespacedRequests }}' => $namespacedRequests,
'{{namespacedRequests}}' => $namespacedRequests,
]);
}

protected function buildClass($name)
{
$stub = parent::buildClass($name);

$replace = [];

$appRootNamespace = $this->laravel->getNamespace();
$pathToAppBaseController = parent::getPath("Http\Controllers\Controller");

$baseControllerExists = file_exists($pathToAppBaseController);

if ($baseControllerExists) {
$controllerClass = class_basename($name);
$replace["\nclass {$controllerClass}\n"] = "use {$appRootNamespace}Http\Controllers\Controller;\n\nclass {$controllerClass} extends Controller\n";
}

$stub = str_replace(
array_keys($replace),
array_values($replace),
$stub
);

return $this->sortImports($stub);
}
}
4 changes: 4 additions & 0 deletions src/Commands/DomainDtoMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;

class DomainDtoMakeCommand extends DomainGeneratorCommand
{
use HasDomainStubs;

protected $name = 'ddd:dto';

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/DomainGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\InteractsWithStubs;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;
use Lunarstorm\LaravelDDD\Support\DomainResolver;

abstract class DomainGeneratorCommand extends GeneratorCommand
{
use HasDomainStubs,
use InteractsWithStubs,
ResolvesDomainFromInput;

protected function getRelativeDomainNamespace(): string
Expand Down
27 changes: 21 additions & 6 deletions src/Commands/DomainModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,30 @@ public function handle()
$this->afterHandle();
}

protected function buildFactoryReplacements()
{
$replacements = parent::buildFactoryReplacements();

if ($this->option('factory')) {
$factoryNamespace = Str::start($this->domain->factory($this->getNameInput())->fullyQualifiedName, '\\');

$factoryCode = <<<EOT
/** @use HasFactory<$factoryNamespace> */
use HasFactory;
EOT;

$replacements['{{ factory }}'] = $factoryCode;
$replacements['{{ factoryImport }}'] = 'use Lunarstorm\LaravelDDD\Factories\HasDomainFactory as HasFactory;';
}

return $replacements;
}

protected function buildClass($name)
{
$stub = parent::buildClass($name);

$replacements = [
'use Illuminate\Database\Eloquent\Factories\HasFactory;' => "use Lunarstorm\LaravelDDD\Factories\HasDomainFactory as HasFactory;",
];
$replacements = [];

if ($baseModel = $this->getBaseModel()) {
$baseModelClass = class_basename($baseModel);
Expand All @@ -56,9 +73,7 @@ protected function buildClass($name)
$stub
);

$stub = $this->sortImports($stub);

return $stub;
return $this->sortImports($stub);
}

protected function createBaseModelIfNeeded()
Expand Down
4 changes: 4 additions & 0 deletions src/Commands/DomainValueObjectMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;

class DomainValueObjectMakeCommand extends DomainGeneratorCommand
{
use HasDomainStubs;

protected $name = 'ddd:value';

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Commands/DomainViewModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs;
use Lunarstorm\LaravelDDD\Support\DomainResolver;

class DomainViewModelMakeCommand extends DomainGeneratorCommand
{
use HasDomainStubs;

protected $name = 'ddd:view-model';

/**
Expand Down

0 comments on commit abcbee5

Please sign in to comment.