Skip to content

Commit

Permalink
Refactoring internals, WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Mar 24, 2024
1 parent 9ae9560 commit b2f3fbf
Show file tree
Hide file tree
Showing 45 changed files with 406 additions and 713 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to `laravel-ddd` will be documented in this file.

## [Unversioned]
### Added
- Additional generator commands that extend Laravel's generators and funnel the generated objects into the domain layer:
- `ddd:cast {domain}:{name}`
- `ddd:channel {domain}:{name}`
- `ddd:command {domain}:{name}`
- `ddd:enum {domain}:{name}` (Laravel 11 only)
- `ddd:event {domain}:{name}`
- `ddd:exception {domain}:{name}`
- `ddd:job {domain}:{name}`
- `ddd:listener {domain}:{name}`
- `ddd:mail {domain}:{name}`
- `ddd:notification {domain}:{name}`
- `ddd:policy {domain}:{name}`
- `ddd:provider {domain}:{name}`
- `ddd:resource {domain}:{name}`
- `ddd:rule {domain}:{name}`
- `ddd:scope {domain}:{name}`

### Chore
- Dropped Laravel 9 support.

## [0.10.0] - 2024-03-23
### Added
- Add `ddd.domain_path` and `ddd.domain_namespace` to config, to specify the path to the domain layer and root domain namespace more explicitly (replaces the previous `ddd.paths.domains` config).
Expand Down
34 changes: 20 additions & 14 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,27 @@
|
*/
'namespaces' => [
'models' => 'Models',
'data_transfer_objects' => 'Data',
'view_models' => 'ViewModels',
'value_objects' => 'ValueObjects',
'actions' => 'Actions',
'casts' => 'Casts',
'commands' => 'Commands',
'enums' => 'Enums',
'events' => 'Events',
'exceptions' => 'Exceptions',
'jobs' => 'Jobs',
'model' => 'Models',
'data_transfer_object' => 'Data',
'view_model' => 'ViewModels',
'value_object' => 'ValueObjects',
'action' => 'Actions',
'cast' => 'Casts',
'channel' => 'Channels',
'command' => 'Commands',
'enum' => 'Enums',
'event' => 'Events',
'exception' => 'Exceptions',
'job' => 'Jobs',
'listener' => 'Listeners',
'mail' => 'Mail',
'notifications' => 'Notifications',
'resources' => 'Resources',
'rules' => 'Rules',
'notification' => 'Notifications',
'observer' => 'Observers',
'policy' => 'Policies',
'provider' => 'Providers',
'resource' => 'Resources',
'rule' => 'Rules',
'scope' => 'Scopes',
],

/*
Expand Down
40 changes: 12 additions & 28 deletions src/Commands/Concerns/ResolvesDomainFromInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Support\Domain;
use Lunarstorm\LaravelDDD\Support\DomainResolver;
use Lunarstorm\LaravelDDD\Support\Path;
use Symfony\Component\Console\Input\InputOption;

trait ResolvesDomainFromInput
Expand All @@ -24,22 +25,17 @@ protected function rootNamespace()
return Str::finish(DomainResolver::getConfiguredDomainNamespace(), '\\');
}

protected function guessObjectType(): string
{
$type = str($this->name)->after(':')->snake()->toString();

return $type;
}

protected function getDefaultNamespace($rootNamespace)
{
if ($this->domain) {
return match ($this->name) {
'ddd:cast' => $this->domain->namespace->casts,
'ddd:command' => $this->domain->namespace->commands,
'ddd:enum' => $this->domain->namespace->enums,
'ddd:event' => $this->domain->namespace->events,
'ddd:exception' => $this->domain->namespace->exceptions,
'ddd:job' => $this->domain->namespace->jobs,
'ddd:mail' => $this->domain->namespace->mail,
'ddd:notification' => $this->domain->namespace->notifications,
'ddd:resource' => $this->domain->namespace->resources,
'ddd:rule' => $this->domain->namespace->rules,
default => throw new \Exception("Unsupported domain generator: {$this->name}"),
};
return $this->domain->namespaceFor($this->guessObjectType());
}

return parent::getDefaultNamespace($rootNamespace);
Expand All @@ -48,21 +44,9 @@ protected function getDefaultNamespace($rootNamespace)
protected function getPath($name)
{
if ($this->domain) {
return $this->laravel->basePath(
match ($this->name) {
'ddd:cast' => $this->domain->cast($name)->path,
'ddd:command' => $this->domain->command($name)->path,
'ddd:enum' => $this->domain->enum($name)->path,
'ddd:event' => $this->domain->event($name)->path,
'ddd:exception' => $this->domain->exception($name)->path,
'ddd:job' => $this->domain->job($name)->path,
'ddd:mail' => $this->domain->mail($name)->path,
'ddd:notification' => $this->domain->notification($name)->path,
'ddd:resource' => $this->domain->resource($name)->path,
'ddd:rule' => $this->domain->rule($name)->path,
default => throw new \Exception("Unsupported domain generator: {$this->name}"),
}
);
return Path::normalize($this->laravel->basePath(
$this->domain->object($this->guessObjectType(), class_basename($name))->path
));
}

return parent::getPath($name);
Expand Down
13 changes: 13 additions & 0 deletions src/Commands/DomainChannelMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ChannelMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainChannelMakeCommand extends ChannelMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:channel';
}
67 changes: 6 additions & 61 deletions src/Commands/DomainGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,32 @@

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;
use Lunarstorm\LaravelDDD\Support\DomainResolver;
use Lunarstorm\LaravelDDD\Support\Path;
use Symfony\Component\Console\Input\InputArgument;

abstract class DomainGeneratorCommand extends GeneratorCommand
{
protected function getArguments()
{
return [
new InputArgument(
'domain',
InputArgument::REQUIRED,
'The domain'
),
];
}

protected function rootNamespace()
{
return str(DomainResolver::getConfiguredDomainNamespace())
->rtrim('/\\')
->toString();
}
use ResolvesDomainFromInput;

protected function getDefaultNamespace($rootNamespace)
protected function getRelativeDomainNamespace(): string
{
$domain = $this->getDomain();

return $rootNamespace.'\\'.$domain.'\\'.$this->getRelativeDomainNamespace();
return DomainResolver::getRelativeObjectNamespace($this->guessObjectType());
}

abstract protected function getRelativeDomainNamespace(): string;

protected function getNameInput()
{
return Str::studly($this->argument('name'));
}

protected function getDomainInput()
{
return $this->argument('domain');
}

protected function getDomain()
{
return str($this->getDomainInput())
->trim()
->replace(['.', '/'], '\\')
->studly()
->toString();
}

protected function getDomainBasePath()
{
return Path::normalize($this->laravel->basePath(
DomainResolver::getConfiguredDomainPath() ?? 'src/Domain'
));
}

protected function getPath($name)
{
$path = str($name)
->replaceFirst($this->rootNamespace(), '')
->replace('\\', '/')
->ltrim('/')
->append('.php')
->prepend($this->getDomainBasePath().DIRECTORY_SEPARATOR)
->toString();

return Path::normalize($path);
}

protected function resolveStubPath($path)
{
$path = ltrim($path, '/\\');

$publishedPath = resource_path('stubs/ddd/'.$path);
$publishedPath = resource_path('stubs/ddd/' . $path);

return file_exists($publishedPath)
? $publishedPath
: __DIR__.DIRECTORY_SEPARATOR.'../../stubs'.DIRECTORY_SEPARATOR.$path;
: __DIR__ . DIRECTORY_SEPARATOR . '../../stubs' . DIRECTORY_SEPARATOR . $path;
}

protected function fillPlaceholder($stub, $placeholder, $value)
Expand Down
12 changes: 12 additions & 0 deletions src/Commands/DomainListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Console\Command;

class DomainListCommand extends Command
{
protected $name = 'ddd:list';

protected $description = 'List all current domains';
}
13 changes: 13 additions & 0 deletions src/Commands/DomainListenerMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ListenerMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainListenerMakeCommand extends ListenerMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:listener';
}
13 changes: 13 additions & 0 deletions src/Commands/DomainObserverMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ObserverMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainObserverMakeCommand extends ObserverMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:observer';
}
13 changes: 13 additions & 0 deletions src/Commands/DomainPolicyMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\PolicyMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainPolicyMakeCommand extends PolicyMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:policy';
}
13 changes: 13 additions & 0 deletions src/Commands/DomainProviderMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ProviderMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainProviderMakeCommand extends ProviderMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:provider';
}
13 changes: 13 additions & 0 deletions src/Commands/DomainScopeMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Foundation\Console\ScopeMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainScopeMakeCommand extends ScopeMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:scope';
}
20 changes: 0 additions & 20 deletions src/Commands/MakeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Symfony\Component\Console\Input\InputArgument;

class MakeAction extends DomainGeneratorCommand
{
protected $name = 'ddd:action';
Expand All @@ -17,29 +15,11 @@ class MakeAction extends DomainGeneratorCommand

protected $type = 'Action';

protected function getArguments()
{
return [
...parent::getArguments(),

new InputArgument(
'name',
InputArgument::REQUIRED,
'The name of the Action',
),
];
}

protected function getStub()
{
return $this->resolveStubPath('action.php.stub');
}

protected function getRelativeDomainNamespace(): string
{
return config('ddd.namespaces.actions', 'Actions');
}

protected function preparePlaceholders(): array
{
$baseClass = config('ddd.base_action');
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/MakeBaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Lunarstorm\LaravelDDD\Commands;

use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;
use Symfony\Component\Console\Input\InputArgument;

class MakeBaseModel extends DomainGeneratorCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:base-model';

/**
Expand All @@ -20,8 +23,6 @@ class MakeBaseModel extends DomainGeneratorCommand
protected function getArguments()
{
return [
...parent::getArguments(),

new InputArgument(
'name',
InputArgument::OPTIONAL,
Expand All @@ -38,6 +39,6 @@ protected function getStub()

protected function getRelativeDomainNamespace(): string
{
return config('ddd.namespaces.models', 'Models');
return config('ddd.namespaces.model', 'Models');
}
}
Loading

0 comments on commit b2f3fbf

Please sign in to comment.