Skip to content

Commit

Permalink
WIP:
Browse files Browse the repository at this point in the history
- skip base model creation if ddd.base_model is a class that exists
- otherwise, attempt to generate one, as long as it's within a domain-level namespace
  • Loading branch information
JasperTey committed Nov 12, 2023
1 parent 0a3d435 commit 976889e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
37 changes: 28 additions & 9 deletions src/Commands/MakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,54 @@ 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, [
'domain' => 'Shared',
'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()),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion stubs/model.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 14 additions & 1 deletion tests/Generator/MakeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 976889e

Please sign in to comment.