Skip to content

Commit

Permalink
Update internals and implement custom namespace resolving.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Oct 15, 2024
1 parent cfc4515 commit 79ea5ad
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 150 deletions.
31 changes: 0 additions & 31 deletions src/Commands/Concerns/CallsDomainCommands.php

This file was deleted.

62 changes: 62 additions & 0 deletions src/Commands/Concerns/ForwardsToDomainCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands\Concerns;

use Illuminate\Support\Str;

trait ForwardsToDomainCommands
{
public function call($command, array $arguments = [])
{
$subfolder = Str::contains($this->getNameInput(), '/')
? Str::beforeLast($this->getNameInput(), '/')
: null;

$nameWithSubfolder = $subfolder ? "{$subfolder}/{$arguments['name']}" : $arguments['name'];

return match ($command) {
'make:request' => $this->runCommand('ddd:request', [
...$arguments,
'name' => $nameWithSubfolder,
'--domain' => $this->domain->dotName,
], $this->output),

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

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

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

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

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

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

default => $this->runCommand($command, $arguments, $this->output),
};
}
}
2 changes: 2 additions & 0 deletions src/Commands/Concerns/ResolvesDomainFromInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,7 @@ protected function beforeHandle()
}

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

app('ddd')->captureCommandContext($this, $this->domain, $this->guessObjectType());
}
}
6 changes: 3 additions & 3 deletions src/Commands/DomainControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Routing\Console\ControllerMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\CallsDomainCommands;
use Lunarstorm\LaravelDDD\Commands\Concerns\ForwardsToDomainCommands;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

use function Laravel\Prompts\confirm;

class DomainControllerMakeCommand extends ControllerMakeCommand
{
use CallsDomainCommands,
use ForwardsToDomainCommands,
ResolvesDomainFromInput;

protected $name = 'ddd:controller';
Expand Down Expand Up @@ -51,7 +51,7 @@ protected function buildFormRequestReplacements(array $replace, $modelClass)
];

if ($this->option('requests')) {
$namespace = $this->domain->namespaceFor('request');
$namespace = $this->domain->namespaceFor('request', $this->getNameInput());

[$storeRequestClass, $updateRequestClass] = $this->generateFormRequests(
$modelClass,
Expand Down
86 changes: 3 additions & 83 deletions src/Commands/DomainModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Illuminate\Foundation\Console\ModelMakeCommand;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\ForwardsToDomainCommands;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;
use Lunarstorm\LaravelDDD\Commands\Migration\DomainMigrateMakeCommand;
use Lunarstorm\LaravelDDD\Support\DomainResolver;

class DomainModelMakeCommand extends ModelMakeCommand
{
use ResolvesDomainFromInput;
use ForwardsToDomainCommands,
ResolvesDomainFromInput;

protected $name = 'ddd:model';

Expand Down Expand Up @@ -58,87 +59,6 @@ protected function buildClass($name)
return $stub;
}

protected function createFactory()
{
$factory = Str::studly($this->argument('name'));

$this->call(DomainFactoryMakeCommand::class, [
'name' => $factory.'Factory',
'--domain' => $this->domain->dotName,
'--model' => $this->qualifyClass($this->getNameInput()),
]);
}

protected function createMigration()
{
$table = Str::snake(Str::pluralStudly(class_basename($this->argument('name'))));

if ($this->option('pivot')) {
$table = Str::singular($table);
}

$this->call(DomainMigrateMakeCommand::class, [
'name' => "create_{$table}_table",
'--domain' => $this->domain->dotName,
'--create' => $table,
]);
}

/**
* Create a seeder file for the model.
*
* @return void
*/
protected function createSeeder()
{
$seeder = Str::studly(class_basename($this->argument('name')));

$this->call(DomainSeederMakeCommand::class, [
'name' => "{$seeder}Seeder",
'--domain' => $this->domain->dotName,
]);
}

/**
* Create a controller for the model.
*
* @return void
*/
protected function createController()
{
$controller = Str::studly(class_basename($this->argument('name')));

$modelName = $this->qualifyClass($this->getNameInput());

$controllerName = "{$controller}Controller";

$this->call(DomainControllerMakeCommand::class, array_filter([
'name' => $controllerName,
'--domain' => $this->domain->dotName,
'--model' => $this->option('resource') || $this->option('api') ? $modelName : null,
'--api' => $this->option('api'),
'--requests' => $this->option('requests') || $this->option('all'),
'--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(DomainPolicyMakeCommand::class, [
'name' => "{$policy}Policy",
'--domain' => $this->domain->dotName,
'--model' => $this->qualifyClass($this->getNameInput()),
]);
}

protected function createBaseModelIfNeeded()
{
if (! $this->shouldCreateBaseModel()) {
Expand Down
29 changes: 23 additions & 6 deletions src/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Lunarstorm\LaravelDDD;

use Illuminate\Console\Command;
use Lunarstorm\LaravelDDD\Support\Domain;
use Lunarstorm\LaravelDDD\ValueObjects\DomainCommandContext;

class DomainManager
{
/**
Expand All @@ -23,13 +27,16 @@ class DomainManager
*
* @var callable|null
*/
protected $applicationLayerNamespaceResolver;
protected $namespaceResolver;

protected ?DomainCommandContext $commandContext;

public function __construct()
{
$this->autoloadFilter = null;
$this->applicationLayerFilter = null;
$this->applicationLayerNamespaceResolver = null;
$this->namespaceResolver = null;
$this->commandContext = null;
}

public function filterAutoloadPathsUsing(callable $filter): void
Expand All @@ -52,13 +59,23 @@ public function getApplicationLayerFilter(): ?callable
return $this->applicationLayerFilter;
}

public function resolveApplicationLayerNamespaceUsing(callable $resolver): void
public function resolveNamespaceUsing(callable $resolver): void
{
$this->namespaceResolver = $resolver;
}

public function getNamespaceResolver(): ?callable
{
return $this->namespaceResolver;
}

public function captureCommandContext(Command $command, ?Domain $domain, ?string $type): void
{
$this->applicationLayerNamespaceResolver = $resolver;
$this->commandContext = DomainCommandContext::fromCommand($command, $domain, $type);
}

public function getApplicationLayerNamespaceResolver(): ?callable
public function getCommandContext(): ?DomainCommandContext
{
return $this->applicationLayerNamespaceResolver;
return $this->commandContext;
}
}
2 changes: 1 addition & 1 deletion src/Facades/DDD.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @see \Lunarstorm\LaravelDDD\DomainManager
*
* @method static void filterAutoloadPathsUsing(callable $filter)
* @method static void resolveApplicationLayerNamespaceUsing(callable $resolver)
* @method static void resolveNamespaceUsing(callable $resolver)
*/
class DDD extends Facade
{
Expand Down
16 changes: 10 additions & 6 deletions src/Support/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public function relativePath(string $path = ''): string
return collect([$this->domain, $path])->filter()->implode(DIRECTORY_SEPARATOR);
}

public function namespaceFor(string $type): string
public function namespaceFor(string $type, ?string $name = null): string
{
return DomainResolver::getDomainObjectNamespace($this->domainWithSubdomain, $type);
return DomainResolver::getDomainObjectNamespace($this->domainWithSubdomain, $type, $name);
}

public function guessNamespaceFromName(string $name): string
Expand All @@ -121,11 +121,15 @@ public function guessNamespaceFromName(string $name): string

public function object(string $type, string $name, bool $absolute = false): DomainObject
{
$namespaceResolver = app('ddd')->getApplicationLayerNamespaceResolver();
$resolvedNamespace = null;

$resolvedNamespace = is_callable($namespaceResolver)
? $namespaceResolver($this->domainWithSubdomain, $type, $name)
: null;
if (DomainResolver::isApplicationLayer($type)) {
$resolver = app('ddd')->getNamespaceResolver();

$resolvedNamespace = is_callable($resolver)
? $resolver($this->domainWithSubdomain, $type, app('ddd')->getCommandContext())
: null;
}

$namespace = $resolvedNamespace ?? match (true) {
$absolute => $this->namespace->root,
Expand Down
24 changes: 18 additions & 6 deletions src/Support/DomainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,37 @@ public static function resolveRootNamespace(string $type): ?string
*
* @param string $domain The domain name.
* @param string $type The domain object type.
* @param string|null $object The domain object name.
* @param string|null $name The domain object name.
*/
public static function getDomainObjectNamespace(string $domain, string $type, ?string $object = null): string
public static function getDomainObjectNamespace(string $domain, string $type, ?string $name = null): string
{
$resolver = app('ddd')->getApplicationLayerNamespaceResolver() ?? function (string $domain, string $type, ?string $object) {
if (static::isApplicationLayer($type)) {
$customResolver = app('ddd')->getNamespaceResolver();

$resolved = is_callable($customResolver)
? $customResolver($domain, $type, app('ddd')->getCommandContext())
: null;

if (! is_null($resolved)) {
return $resolved;
}
}

$resolver = function (string $domain, string $type, ?string $name) {
$namespace = collect([
static::resolveRootNamespace($type),
$domain,
static::getRelativeObjectNamespace($type),
])->filter()->implode('\\');

if ($object) {
$namespace .= "\\{$object}";
if ($name) {
$namespace .= "\\{$name}";
}

return $namespace;
};

return $resolver($domain, $type, $object);
return $resolver($domain, $type, $name);
}

/**
Expand Down
Loading

0 comments on commit 79ea5ad

Please sign in to comment.