Skip to content

Commit

Permalink
Custom Layers + Refactored Internals, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Nov 12, 2024
1 parent 6237174 commit 9366325
Show file tree
Hide file tree
Showing 33 changed files with 1,022 additions and 398 deletions.
4 changes: 2 additions & 2 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
|
*/
'application' => [
'path' => 'app/Modules',
'namespace' => 'App\Modules',
'path' => 'app/Modules',
'objects' => [
'controller',
'request',
Expand All @@ -55,7 +55,7 @@
|
*/
'layers' => [
// 'Infrastructure' => 'src/Infrastructure',
'Infrastructure' => 'src/Infrastructure',
// 'Integrations' => 'src/Integrations',
// 'Support' => 'src/Support',
],
Expand Down
14 changes: 7 additions & 7 deletions src/Commands/Concerns/ForwardsToDomainCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@ public function call($command, array $arguments = [])
'make:request' => $this->runCommand('ddd:request', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

'make:model' => $this->runCommand('ddd:model', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

'make:factory' => $this->runCommand('ddd:factory', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

'make:policy' => $this->runCommand('ddd:policy', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

'make:migration' => $this->runCommand('ddd:migration', [
...$arguments,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

'make:seeder' => $this->runCommand('ddd:seeder', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

'make:controller' => $this->runCommand('ddd:controller', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
'--domain' => $this->blueprint->domain->dotName,
], $this->output),

default => $this->runCommand($command, $arguments, $this->output),
Expand Down
10 changes: 10 additions & 0 deletions src/Commands/Concerns/HasGeneratorBlueprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

use Lunarstorm\LaravelDDD\Support\GeneratorBlueprint;

trait HasGeneratorBlueprint
{
protected ?GeneratorBlueprint $blueprint = null;
}
2 changes: 1 addition & 1 deletion src/Commands/Concerns/QualifiesDomainModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait QualifiesDomainModels
{
protected function qualifyModel(string $model)
{
if ($domain = $this->domain) {
if ($domain = $this->blueprint->domain) {
$domainModel = $domain->model($model);

return $domainModel->fullyQualifiedName;
Expand Down
77 changes: 20 additions & 57 deletions src/Commands/Concerns/ResolvesDomainFromInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

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

trait ResolvesDomainFromInput
{
use CanPromptForDomain,
HandleHooks,
HasGeneratorBlueprint,
QualifiesDomainModels;

protected $nameIsAbsolute = false;

protected ?Domain $domain = null;

protected function getOptions()
{
return [
Expand All @@ -28,47 +26,21 @@ protected function getOptions()

protected function rootNamespace()
{
$type = $this->guessObjectType();

return Str::finish(DomainResolver::resolveRootNamespace($type), '\\');
}

protected function guessObjectType(): string
{
return match ($this->name) {
'ddd:base-view-model' => 'view_model',
'ddd:base-model' => 'model',
'ddd:value' => 'value_object',
'ddd:dto' => 'data_transfer_object',
'ddd:migration' => 'migration',
default => str($this->name)->after(':')->snake()->toString(),
};
return $this->blueprint->rootNamespace();
}

protected function getDefaultNamespace($rootNamespace)
{
if ($this->domain) {
return $this->nameIsAbsolute
? $this->domain->namespace->root
: $this->domain->namespaceFor($this->guessObjectType());
}

return parent::getDefaultNamespace($rootNamespace);
return $this->blueprint
? $this->blueprint->getDefaultNamespace($rootNamespace)
: parent::getDefaultNamespace($rootNamespace);
}

protected function getPath($name)
{
if ($this->domain) {
return Path::normalize($this->laravel->basePath(
$this->domain->object(
type: $this->guessObjectType(),
name: $name,
absolute: $this->nameIsAbsolute
)->path
));
}

return parent::getPath($name);
return $this->blueprint
? $this->blueprint->getPath($name)
: parent::getPath($name);
}

protected function beforeHandle()
Expand All @@ -84,33 +56,24 @@ protected function beforeHandle()
$nameInput = Str::after($nameInput, ':');
}

$this->domain = match (true) {
$domainName = match (true) {
// Domain was specified explicitly via option (priority)
filled($this->option('domain')) => new Domain($this->option('domain')),
filled($this->option('domain')) => $this->option('domain'),

// Domain was specified as a prefix in the name
filled($domainExtractedFromName) => new Domain($domainExtractedFromName),
filled($domainExtractedFromName) => $domainExtractedFromName,

default => null,
default => $this->promptForDomainName(),
};

// If the domain is not set, prompt for it
if (! $this->domain) {
$this->domain = new Domain($this->promptForDomainName());
}

// Now that the domain part is handled,
// we will deal with the name portion.

// Normalize slash and dot separators
$nameInput = Str::replace(['.', '\\', '/'], '/', $nameInput);

if ($this->nameIsAbsolute = Str::startsWith($nameInput, ['/'])) {
// $nameInput = Str::after($nameInput, '/');
}
$this->blueprint = new GeneratorBlueprint(
nameInput: $nameInput,
domainName: $domainName,
command: $this,
);

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

app('ddd')->captureCommandContext($this, $this->domain, $this->guessObjectType());
$this->input->setOption('domain', $this->blueprint->domainName);
}
}
54 changes: 54 additions & 0 deletions src/Commands/Concerns/UpdatesComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

use Symfony\Component\Process\Process;

trait UpdatesComposer
{
public function fillComposerValue($path, $value)
{
$composerFile = base_path('composer.json');
$data = json_decode(file_get_contents($composerFile), true);
data_fill($data, $path, $value);

file_put_contents($composerFile, json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));

return $this;
}

public function registerPsr4Autoload($namespace, $path)
{
$namespace = str($namespace)
->rtrim('/\\')
->finish('\\')
->toString();

$this->comment("Registering `{$namespace}`:`{$path}` in composer.json...");

$this->fillComposerValue(['autoload', 'psr-4', $namespace], $path);

$this->composerReload();

return $this;
}

protected function composerReload()
{
$composer = $this->hasOption('composer') ? $this->option('composer') : 'global';

if ($composer !== 'global') {
$command = ['php', $composer, 'dump-autoload'];
} else {
$command = ['composer', 'dump-autoload'];
}

(new Process($command, base_path(), ['COMPOSER_MEMORY_LIMIT' => '-1']))
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
});

return $this;
}
}
Loading

0 comments on commit 9366325

Please sign in to comment.