diff --git a/src/Commands/MakeModel.php b/src/Commands/MakeModel.php index 51716a4..224ada9 100644 --- a/src/Commands/MakeModel.php +++ b/src/Commands/MakeModel.php @@ -48,15 +48,40 @@ protected function getRelativeDomainNamespace(): string return config('ddd.namespaces.models', 'Models'); } + protected function preparePlaceholders(): array + { + $baseClass = config('ddd.base_model'); + $baseClassName = class_basename($baseClass); + + return [ + 'extends' => filled($baseClass) ? " extends {$baseClassName}" : '', + ]; + } + public function handle() { - $baseModel = config('ddd.base_model'); + $this->createBaseModelIfNeeded(config('ddd.base_model')); + + parent::handle(); + + if ($this->option('factory')) { + $this->createFactory(); + } + } + + protected function createBaseModelIfNeeded($baseModel) + { + if (class_exists($baseModel)) { + return; + } $parts = str($baseModel)->explode('\\'); $baseModelName = $parts->last(); $baseModelPath = $this->getPath($baseModel); - if (! file_exists($baseModelPath)) { + // base_path(config('ddd.paths.domains') . '/Shared/Models/BaseModel.php') + + if (!file_exists($baseModelPath)) { $this->warn("Base model {$baseModel} doesn't exist, generating..."); $this->call(MakeBaseModel::class, [ @@ -64,19 +89,13 @@ public function handle() 'name' => $baseModelName, ]); } - - parent::handle(); - - if ($this->option('factory')) { - $this->createFactory(); - } } protected function createFactory() { $this->call(MakeFactory::class, [ 'domain' => $this->getDomain(), - 'name' => $this->getNameInput().'Factory', + 'name' => $this->getNameInput() . 'Factory', '--model' => $this->qualifyClass($this->getNameInput()), ]); } diff --git a/stubs/model.php.stub b/stubs/model.php.stub index db53f28..f8ff6e8 100644 --- a/stubs/model.php.stub +++ b/stubs/model.php.stub @@ -5,7 +5,7 @@ namespace {{ namespace }}; use {{ rootNamespace }}\Shared\Models\BaseModel; use Illuminate\Database\Eloquent\SoftDeletes; -class {{ class }} extends BaseModel +class {{ class }}{{ extends }} { use SoftDeletes; diff --git a/tests/Generator/MakeModelTest.php b/tests/Generator/MakeModelTest.php index 55628ae..e13b8d5 100644 --- a/tests/Generator/MakeModelTest.php +++ b/tests/Generator/MakeModelTest.php @@ -106,7 +106,7 @@ expect(file_exists($expectedModelPath))->toBeTrue(); })->with('makeModelInputs'); -it('generates the base model if needed', function () { +it('generates the base model when possible', function () { $modelName = Str::studly(fake()->word()); $domain = Str::studly(fake()->word()); @@ -141,6 +141,19 @@ expect(file_exists($expectedBaseModelPath))->toBeTrue(); }); +it('skips base model creation if configured base model already exists', function ($baseModel) { + Config::set('ddd.base_model', $baseModel); + + expect(class_exists($baseModel))->toBeTrue(); + + Artisan::call('ddd:model Fruits Lemon'); + + expect(Artisan::output())->not->toContain("Base model {$baseModel} doesn't exist, generating..."); +})->with([ + ['Illuminate\Database\Eloquent\Model'], + ['Lunarstorm\LaravelDDD\Models\DomainModel'], +]); + it('shows meaningful hints when prompting for missing input', function () { $this->artisan('ddd:model') ->expectsQuestion('What is the domain?', 'Utility')