From 497b8b09c2eae60054579f2d11a7ab761317d1d8 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Thu, 16 May 2024 08:20:31 -0400 Subject: [PATCH] WIP experimentation --- src/Commands/Concerns/OverridesHandle.php | 25 ++++ .../Concerns/ResolvesDomainFromInput.php | 9 +- src/Commands/DomainMailMakeCommand.php | 87 ++++++++++++++ src/Commands/DomainModelMakeCommand.php | 93 +-------------- src/Commands/DomainModelMakeLegacyCommand.php | 108 ++++++++++++++++++ 5 files changed, 229 insertions(+), 93 deletions(-) create mode 100644 src/Commands/Concerns/OverridesHandle.php create mode 100644 src/Commands/DomainModelMakeLegacyCommand.php diff --git a/src/Commands/Concerns/OverridesHandle.php b/src/Commands/Concerns/OverridesHandle.php new file mode 100644 index 0000000..0240142 --- /dev/null +++ b/src/Commands/Concerns/OverridesHandle.php @@ -0,0 +1,25 @@ +beforeHandle(); + + parent::handle(); + + $this->afterHandle(); + } +} diff --git a/src/Commands/Concerns/ResolvesDomainFromInput.php b/src/Commands/Concerns/ResolvesDomainFromInput.php index 9f323b6..1f2f723 100644 --- a/src/Commands/Concerns/ResolvesDomainFromInput.php +++ b/src/Commands/Concerns/ResolvesDomainFromInput.php @@ -10,7 +10,8 @@ trait ResolvesDomainFromInput { - use CanPromptForDomain; + use OverridesHandle, + CanPromptForDomain; protected $nameIsAbsolute = false; @@ -66,7 +67,7 @@ protected function getPath($name) return parent::getPath($name); } - public function handle() + protected function beforeHandle() { $nameInput = $this->getNameInput(); @@ -90,7 +91,7 @@ public function handle() }; // If the domain is not set, prompt for it - if (! $this->domain) { + if (!$this->domain) { $this->domain = new Domain($this->promptForDomainName()); } @@ -105,7 +106,5 @@ public function handle() } $this->input->setArgument('name', $nameInput); - - parent::handle(); } } diff --git a/src/Commands/DomainMailMakeCommand.php b/src/Commands/DomainMailMakeCommand.php index 15f9508..6021c70 100644 --- a/src/Commands/DomainMailMakeCommand.php +++ b/src/Commands/DomainMailMakeCommand.php @@ -3,6 +3,7 @@ namespace Lunarstorm\LaravelDDD\Commands; use Illuminate\Foundation\Console\MailMakeCommand; +use Illuminate\Support\Str; use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; class DomainMailMakeCommand extends MailMakeCommand @@ -10,4 +11,90 @@ class DomainMailMakeCommand extends MailMakeCommand use ResolvesDomainFromInput; protected $name = 'ddd:mail'; + + /** + * Create a model factory for the model. + * + * @return void + */ + protected function createFactory() + { + $factory = Str::studly($this->argument('name')); + + $this->call('ddd:factory', [ + 'name' => "{$factory}Factory", + '--domain' => $this->domain->dotName, + '--model' => $this->qualifyClass($this->getNameInput()), + ]); + } + + /** + * Create a migration file for the model. + * + * @return void + */ + protected function createMigration() + { + $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name')))); + + if ($this->option('pivot')) { + $table = Str::singular($table); + } + + $this->call('make:migration', [ + 'name' => "create_{$table}_table", + '--create' => $table, + ]); + } + + /** + * Create a seeder file for the model. + * + * @return void + */ + protected function createSeeder() + { + $seeder = Str::studly(class_basename($this->argument('name'))); + + $this->call('make:seeder', [ + 'name' => "{$seeder}Seeder", + ]); + } + + /** + * Create a controller for the model. + * + * @return void + */ + protected function createController() + { + $controller = Str::studly(class_basename($this->argument('name'))); + + $modelName = $this->qualifyClass($this->getNameInput()); + + $this->call('make:controller', array_filter([ + 'name' => "{$controller}Controller", + '--model' => $this->option('resource') || $this->option('api') ? $modelName : null, + '--api' => $this->option('api'), + '--requests' => $this->option('requests') || $this->option('all'), + '--test' => $this->option('test'), + '--pest' => $this->option('pest'), + ])); + } + + /** + * Create a policy file for the model. + * + * @return void + */ + protected function createPolicy() + { + $policy = Str::studly(class_basename($this->argument('name'))); + + $this->call('ddd:policy', [ + 'name' => "{$policy}Policy", + '--domain' => $this->domain->dotName, + '--model' => $this->qualifyClass($this->getNameInput()), + ]); + } } diff --git a/src/Commands/DomainModelMakeCommand.php b/src/Commands/DomainModelMakeCommand.php index 18430c2..1f7a4e4 100644 --- a/src/Commands/DomainModelMakeCommand.php +++ b/src/Commands/DomainModelMakeCommand.php @@ -2,100 +2,17 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Illuminate\Foundation\Console\ModelMakeCommand; use Illuminate\Support\Str; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Lunarstorm\LaravelDDD\Support\DomainResolver; use Symfony\Component\Console\Input\InputOption; -class DomainModelMakeCommand extends DomainGeneratorCommand +class DomainModelMakeCommand extends ModelMakeCommand { - protected $name = 'ddd:model'; - - /** - * The console command description. - * - * @var string - */ - protected $description = 'Generate a domain model'; - - protected $type = 'Model'; - - protected function getOptions() - { - return [ - ...parent::getOptions(), - ['factory', 'f', InputOption::VALUE_NONE, 'Create a new factory for the domain model'], - ]; - } - - protected function getStub() - { - return $this->resolveStubPath('model.php.stub'); - } - - protected function preparePlaceholders(): array - { - $baseClass = config('ddd.base_model'); - $baseClassName = class_basename($baseClass); - - return [ - 'extends' => filled($baseClass) ? " extends {$baseClassName}" : '', - 'baseClassImport' => filled($baseClass) ? "use {$baseClass};" : '', - ]; - } - - public function handle() - { - $this->createBaseModelIfNeeded(); - - parent::handle(); - - if ($this->option('factory')) { - $this->createFactory(); - } - } - - protected function createBaseModelIfNeeded() - { - if (! $this->shouldCreateModel()) { - return; - } - - $baseModel = config('ddd.base_model'); - - $this->warn("Base model {$baseModel} doesn't exist, generating..."); + use ResolvesDomainFromInput; - $domain = DomainResolver::guessDomainFromClass($baseModel); - - $name = Str::after($baseModel, $domain); - - $this->call(DomainBaseModelMakeCommand::class, [ - '--domain' => $domain, - 'name' => $name, - ]); - } - - protected function shouldCreateModel(): bool - { - $baseModel = config('ddd.base_model'); - - // If the class exists, we don't need to create it. - if (class_exists($baseModel)) { - return false; - } - - // If the class is outside of the domain layer, we won't attempt to create it. - if (! DomainResolver::isDomainClass($baseModel)) { - return false; - } - - // At this point the class is probably a domain object, but we should - // check if the expected path exists. - if (file_exists(app()->basePath(DomainResolver::guessPathFromClass($baseModel)))) { - return false; - } - - return true; - } + protected $name = 'ddd:model'; protected function createFactory() { diff --git a/src/Commands/DomainModelMakeLegacyCommand.php b/src/Commands/DomainModelMakeLegacyCommand.php new file mode 100644 index 0000000..d649796 --- /dev/null +++ b/src/Commands/DomainModelMakeLegacyCommand.php @@ -0,0 +1,108 @@ +resolveStubPath('model.php.stub'); + } + + protected function preparePlaceholders(): array + { + $baseClass = config('ddd.base_model'); + $baseClassName = class_basename($baseClass); + + return [ + 'extends' => filled($baseClass) ? " extends {$baseClassName}" : '', + 'baseClassImport' => filled($baseClass) ? "use {$baseClass};" : '', + ]; + } + + public function handle() + { + $this->createBaseModelIfNeeded(); + + parent::handle(); + + if ($this->option('factory')) { + $this->createFactory(); + } + } + + protected function createBaseModelIfNeeded() + { + if (! $this->shouldCreateModel()) { + return; + } + + $baseModel = config('ddd.base_model'); + + $this->warn("Base model {$baseModel} doesn't exist, generating..."); + + $domain = DomainResolver::guessDomainFromClass($baseModel); + + $name = Str::after($baseModel, $domain); + + $this->call(DomainBaseModelMakeCommand::class, [ + '--domain' => $domain, + 'name' => $name, + ]); + } + + protected function shouldCreateModel(): bool + { + $baseModel = config('ddd.base_model'); + + // If the class exists, we don't need to create it. + if (class_exists($baseModel)) { + return false; + } + + // If the class is outside of the domain layer, we won't attempt to create it. + if (! DomainResolver::isDomainClass($baseModel)) { + return false; + } + + // At this point the class is probably a domain object, but we should + // check if the expected path exists. + if (file_exists(app()->basePath(DomainResolver::guessPathFromClass($baseModel)))) { + return false; + } + + return true; + } + + protected function createFactory() + { + $this->call(DomainFactoryMakeCommand::class, [ + 'name' => $this->getNameInput().'Factory', + '--domain' => $this->domain->dotName, + '--model' => $this->qualifyClass($this->getNameInput()), + ]); + } +}