-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
229 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
]); | ||
} | ||
} |