Skip to content

Commit

Permalink
WIP experimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed May 16, 2024
1 parent f6cb25a commit 497b8b0
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 93 deletions.
25 changes: 25 additions & 0 deletions src/Commands/Concerns/OverridesHandle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

trait OverridesHandle
{
protected function beforeHandle()
{
//
}

protected function afterHandle()
{
//
}

public function handle()
{
$this->beforeHandle();

parent::handle();

$this->afterHandle();
}
}
9 changes: 4 additions & 5 deletions src/Commands/Concerns/ResolvesDomainFromInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

trait ResolvesDomainFromInput
{
use CanPromptForDomain;
use OverridesHandle,
CanPromptForDomain;

protected $nameIsAbsolute = false;

Expand Down Expand Up @@ -66,7 +67,7 @@ protected function getPath($name)
return parent::getPath($name);
}

public function handle()
protected function beforeHandle()
{
$nameInput = $this->getNameInput();

Expand All @@ -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());
}

Expand All @@ -105,7 +106,5 @@ public function handle()
}

$this->input->setArgument('name', $nameInput);

parent::handle();
}
}
87 changes: 87 additions & 0 deletions src/Commands/DomainMailMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,98 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\MailMakeCommand;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

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')) {

Check failure on line 40 in src/Commands/DomainMailMakeCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "ddd:mail" does not have 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,

Check failure on line 77 in src/Commands/DomainMailMakeCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "ddd:mail" does not have option "api".

Check failure on line 77 in src/Commands/DomainMailMakeCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "ddd:mail" does not have option "resource".
'--api' => $this->option('api'),

Check failure on line 78 in src/Commands/DomainMailMakeCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "ddd:mail" does not have option "api".
'--requests' => $this->option('requests') || $this->option('all'),

Check failure on line 79 in src/Commands/DomainMailMakeCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "ddd:mail" does not have option "all".

Check failure on line 79 in src/Commands/DomainMailMakeCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Command "ddd:mail" does not have option "requests".
'--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()),
]);
}
}
93 changes: 5 additions & 88 deletions src/Commands/DomainModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
108 changes: 108 additions & 0 deletions src/Commands/DomainModelMakeLegacyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Support\DomainResolver;
use Symfony\Component\Console\Input\InputOption;

class DomainModelMakeLegacyCommand extends DomainGeneratorCommand
{
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...");

$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()),
]);
}
}

0 comments on commit 497b8b0

Please sign in to comment.