diff --git a/src/Commands/Concerns/HasDomainStubs.php b/src/Commands/Concerns/HasDomainStubs.php index 9d935f1..50e129e 100644 --- a/src/Commands/Concerns/HasDomainStubs.php +++ b/src/Commands/Concerns/HasDomainStubs.php @@ -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) { @@ -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; @@ -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; + // } } diff --git a/src/Commands/DomainActionMakeCommand.php b/src/Commands/DomainActionMakeCommand.php index 96002db..3218457 100644 --- a/src/Commands/DomainActionMakeCommand.php +++ b/src/Commands/DomainActionMakeCommand.php @@ -2,8 +2,12 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs; + class DomainActionMakeCommand extends DomainGeneratorCommand { + use HasDomainStubs; + protected $name = 'ddd:action'; /** diff --git a/src/Commands/DomainBaseModelMakeCommand.php b/src/Commands/DomainBaseModelMakeCommand.php index 114f57d..e2c9c2a 100644 --- a/src/Commands/DomainBaseModelMakeCommand.php +++ b/src/Commands/DomainBaseModelMakeCommand.php @@ -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'; /** diff --git a/src/Commands/DomainBaseViewModelMakeCommand.php b/src/Commands/DomainBaseViewModelMakeCommand.php index ac54d0d..af6c247 100644 --- a/src/Commands/DomainBaseViewModelMakeCommand.php +++ b/src/Commands/DomainBaseViewModelMakeCommand.php @@ -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'; /** diff --git a/src/Commands/DomainControllerMakeCommand.php b/src/Commands/DomainControllerMakeCommand.php index 93febe0..6cc06ec 100644 --- a/src/Commands/DomainControllerMakeCommand.php +++ b/src/Commands/DomainControllerMakeCommand.php @@ -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; @@ -11,8 +12,8 @@ class DomainControllerMakeCommand extends ControllerMakeCommand { - use ForwardsToDomainCommands, - HasDomainStubs, + use HasDomainStubs, + ForwardsToDomainCommands, ResolvesDomainFromInput; protected $name = 'ddd:controller'; @@ -62,10 +63,10 @@ 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, [ @@ -73,12 +74,37 @@ protected function buildFormRequestReplacements(array $replace, $modelClass) '{{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); + } } diff --git a/src/Commands/DomainDtoMakeCommand.php b/src/Commands/DomainDtoMakeCommand.php index 793aa33..8dd532b 100644 --- a/src/Commands/DomainDtoMakeCommand.php +++ b/src/Commands/DomainDtoMakeCommand.php @@ -2,8 +2,12 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs; + class DomainDtoMakeCommand extends DomainGeneratorCommand { + use HasDomainStubs; + protected $name = 'ddd:dto'; /** diff --git a/src/Commands/DomainGeneratorCommand.php b/src/Commands/DomainGeneratorCommand.php index 8241339..283d0d4 100644 --- a/src/Commands/DomainGeneratorCommand.php +++ b/src/Commands/DomainGeneratorCommand.php @@ -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 diff --git a/src/Commands/DomainModelMakeCommand.php b/src/Commands/DomainModelMakeCommand.php index f57674e..ed44a90 100644 --- a/src/Commands/DomainModelMakeCommand.php +++ b/src/Commands/DomainModelMakeCommand.php @@ -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 = << */ + 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); @@ -56,9 +73,7 @@ protected function buildClass($name) $stub ); - $stub = $this->sortImports($stub); - - return $stub; + return $this->sortImports($stub); } protected function createBaseModelIfNeeded() diff --git a/src/Commands/DomainValueObjectMakeCommand.php b/src/Commands/DomainValueObjectMakeCommand.php index 52b80f8..4a3b68e 100644 --- a/src/Commands/DomainValueObjectMakeCommand.php +++ b/src/Commands/DomainValueObjectMakeCommand.php @@ -2,8 +2,12 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\HasDomainStubs; + class DomainValueObjectMakeCommand extends DomainGeneratorCommand { + use HasDomainStubs; + protected $name = 'ddd:value'; /** diff --git a/src/Commands/DomainViewModelMakeCommand.php b/src/Commands/DomainViewModelMakeCommand.php index b071955..935fa05 100644 --- a/src/Commands/DomainViewModelMakeCommand.php +++ b/src/Commands/DomainViewModelMakeCommand.php @@ -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'; /**