diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bad98b..fb8a88f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/config/ddd.php b/config/ddd.php index 5122ce5..a1bd431 100644 --- a/config/ddd.php +++ b/config/ddd.php @@ -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', ], /* diff --git a/src/Commands/Concerns/ResolvesDomainFromInput.php b/src/Commands/Concerns/ResolvesDomainFromInput.php index 2fbe3f5..defcc99 100644 --- a/src/Commands/Concerns/ResolvesDomainFromInput.php +++ b/src/Commands/Concerns/ResolvesDomainFromInput.php @@ -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 @@ -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); @@ -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); diff --git a/src/Commands/DomainChannelMakeCommand.php b/src/Commands/DomainChannelMakeCommand.php new file mode 100644 index 0000000..f3e5ba5 --- /dev/null +++ b/src/Commands/DomainChannelMakeCommand.php @@ -0,0 +1,13 @@ +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) diff --git a/src/Commands/DomainListCommand.php b/src/Commands/DomainListCommand.php new file mode 100644 index 0000000..c201d77 --- /dev/null +++ b/src/Commands/DomainListCommand.php @@ -0,0 +1,12 @@ +resolveStubPath('action.php.stub'); } - protected function getRelativeDomainNamespace(): string - { - return config('ddd.namespaces.actions', 'Actions'); - } - protected function preparePlaceholders(): array { $baseClass = config('ddd.base_action'); diff --git a/src/Commands/MakeBaseModel.php b/src/Commands/MakeBaseModel.php index 7b4062e..22b544a 100644 --- a/src/Commands/MakeBaseModel.php +++ b/src/Commands/MakeBaseModel.php @@ -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'; /** @@ -20,8 +23,6 @@ class MakeBaseModel extends DomainGeneratorCommand protected function getArguments() { return [ - ...parent::getArguments(), - new InputArgument( 'name', InputArgument::OPTIONAL, @@ -38,6 +39,6 @@ protected function getStub() protected function getRelativeDomainNamespace(): string { - return config('ddd.namespaces.models', 'Models'); + return config('ddd.namespaces.model', 'Models'); } } diff --git a/src/Commands/MakeBaseViewModel.php b/src/Commands/MakeBaseViewModel.php index 5966277..ad9d929 100644 --- a/src/Commands/MakeBaseViewModel.php +++ b/src/Commands/MakeBaseViewModel.php @@ -2,10 +2,13 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Symfony\Component\Console\Input\InputArgument; class MakeBaseViewModel extends DomainGeneratorCommand { + use ResolvesDomainFromInput; + protected $name = 'ddd:base-view-model'; /** @@ -20,8 +23,6 @@ class MakeBaseViewModel extends DomainGeneratorCommand protected function getArguments() { return [ - ...parent::getArguments(), - new InputArgument( 'name', InputArgument::OPTIONAL, @@ -38,6 +39,6 @@ protected function getStub() protected function getRelativeDomainNamespace(): string { - return config('ddd.namespaces.view_models', 'ViewModels'); + return config('ddd.namespaces.view_model', 'ViewModels'); } } diff --git a/src/Commands/MakeDTO.php b/src/Commands/MakeDTO.php index 8e781c6..6c97e55 100644 --- a/src/Commands/MakeDTO.php +++ b/src/Commands/MakeDTO.php @@ -2,10 +2,13 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Symfony\Component\Console\Input\InputArgument; class MakeDTO extends DomainGeneratorCommand { + use ResolvesDomainFromInput; + protected $name = 'ddd:dto'; /** @@ -17,19 +20,6 @@ class MakeDTO extends DomainGeneratorCommand protected $type = 'Data Transfer Object'; - protected function getArguments() - { - return [ - ...parent::getArguments(), - - new InputArgument( - 'name', - InputArgument::REQUIRED, - 'The name of the DTO', - ), - ]; - } - protected function getStub() { return $this->resolveStubPath('dto.php.stub'); @@ -37,7 +27,7 @@ protected function getStub() protected function getRelativeDomainNamespace(): string { - return config('ddd.namespaces.data_transfer_objects', 'Data'); + return config('ddd.namespaces.data_transfer_object', 'Data'); } protected function preparePlaceholders(): array diff --git a/src/Commands/MakeFactory.php b/src/Commands/MakeFactory.php index b18b702..702ccdb 100644 --- a/src/Commands/MakeFactory.php +++ b/src/Commands/MakeFactory.php @@ -2,6 +2,7 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Lunarstorm\LaravelDDD\Support\Domain; use Lunarstorm\LaravelDDD\Support\Path; use Symfony\Component\Console\Input\InputArgument; @@ -9,6 +10,8 @@ class MakeFactory extends DomainGeneratorCommand { + use ResolvesDomainFromInput; + protected $name = 'ddd:factory'; /** @@ -20,22 +23,10 @@ class MakeFactory extends DomainGeneratorCommand protected $type = 'Factory'; - protected function getArguments() - { - return [ - ...parent::getArguments(), - - new InputArgument( - 'name', - InputArgument::REQUIRED, - 'The name of the factory', - ), - ]; - } - protected function getOptions() { return [ + ...parent::getOptions(), ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'], ]; } @@ -52,9 +43,9 @@ protected function rootNamespace() protected function getDefaultNamespace($rootNamespace) { - $domain = $this->getDomain(); + $domain = $this->domain?->domain; - return $rootNamespace.'\\'.$domain; + return $rootNamespace . '\\' . $domain; } protected function getRelativeDomainNamespace(): string @@ -64,7 +55,7 @@ protected function getRelativeDomainNamespace(): string protected function getPath($name) { - if (! str_ends_with($name, 'Factory')) { + if (!str_ends_with($name, 'Factory')) { $name .= 'Factory'; } @@ -75,7 +66,7 @@ protected function getPath($name) ->append('.php') ->toString(); - return Path::normalize(base_path('database/factories/'.$name)); + return Path::normalize(base_path('database/factories/' . $name)); } protected function getFactoryName() diff --git a/src/Commands/MakeModel.php b/src/Commands/MakeModel.php index 3239bfe..b27a043 100644 --- a/src/Commands/MakeModel.php +++ b/src/Commands/MakeModel.php @@ -2,12 +2,15 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Lunarstorm\LaravelDDD\Support\DomainResolver; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; class MakeModel extends DomainGeneratorCommand { + use ResolvesDomainFromInput; + protected $name = 'ddd:model'; /** @@ -19,19 +22,6 @@ class MakeModel extends DomainGeneratorCommand protected $type = 'Model'; - protected function getArguments() - { - return [ - ...parent::getArguments(), - - new InputArgument( - 'name', - InputArgument::REQUIRED, - 'The name of the model', - ), - ]; - } - protected function getOptions() { return [ @@ -44,11 +34,6 @@ protected function getStub() return $this->resolveStubPath('model.php.stub'); } - protected function getRelativeDomainNamespace(): string - { - return config('ddd.namespaces.models', 'Models'); - } - protected function preparePlaceholders(): array { $baseClass = config('ddd.base_model'); @@ -87,24 +72,24 @@ protected function createBaseModelIfNeeded() $this->rootNamespace(), ]; - if (! str($baseModel)->startsWith($allowedNamespacePrefixes)) { + if (!str($baseModel)->startsWith($allowedNamespacePrefixes)) { return; } $domain = DomainResolver::guessDomainFromClass($baseModel); - if (! $domain) { + if (!$domain) { return; } $baseModelName = class_basename($baseModel); $baseModelPath = $this->getPath($baseModel); - if (! file_exists($baseModelPath)) { + if (!file_exists($baseModelPath)) { $this->info("Generating {$baseModel}..."); $this->call(MakeBaseModel::class, [ - 'domain' => $domain, + '--domain' => $domain, 'name' => $baseModelName, ]); } @@ -113,8 +98,8 @@ protected function createBaseModelIfNeeded() protected function createFactory() { $this->call(MakeFactory::class, [ - 'domain' => $this->getDomain(), - 'name' => $this->getNameInput().'Factory', + 'name' => $this->getNameInput() . 'Factory', + '--domain' => $this->domain->domain, '--model' => $this->qualifyClass($this->getNameInput()), ]); } diff --git a/src/Commands/MakeValueObject.php b/src/Commands/MakeValueObject.php index ad54ea5..d8dd06e 100644 --- a/src/Commands/MakeValueObject.php +++ b/src/Commands/MakeValueObject.php @@ -2,10 +2,13 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Symfony\Component\Console\Input\InputArgument; class MakeValueObject extends DomainGeneratorCommand { + use ResolvesDomainFromInput; + protected $name = 'ddd:value'; /** @@ -17,24 +20,6 @@ class MakeValueObject extends DomainGeneratorCommand protected $type = 'Value Object'; - protected function getArguments() - { - return [ - ...parent::getArguments(), - - new InputArgument( - 'name', - InputArgument::REQUIRED, - 'The name of the value object', - ), - ]; - } - - protected function getRelativeDomainNamespace(): string - { - return config('ddd.namespaces.value_objects', 'ValueObjects'); - } - protected function getStub() { return $this->resolveStubPath('value-object.php.stub'); diff --git a/src/Commands/MakeViewModel.php b/src/Commands/MakeViewModel.php index 16ebd16..e28435c 100644 --- a/src/Commands/MakeViewModel.php +++ b/src/Commands/MakeViewModel.php @@ -2,10 +2,13 @@ namespace Lunarstorm\LaravelDDD\Commands; +use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput; use Symfony\Component\Console\Input\InputArgument; class MakeViewModel extends DomainGeneratorCommand { + use ResolvesDomainFromInput; + protected $name = 'ddd:view-model'; /** @@ -17,24 +20,6 @@ class MakeViewModel extends DomainGeneratorCommand protected $type = 'View Model'; - protected function getArguments() - { - return [ - ...parent::getArguments(), - - new InputArgument( - 'name', - InputArgument::REQUIRED, - 'The name of the view model', - ), - ]; - } - - protected function getRelativeDomainNamespace(): string - { - return config('ddd.namespaces.view_models', 'ViewModels'); - } - protected function getStub() { return $this->resolveStubPath('view-model.php.stub'); @@ -48,7 +33,7 @@ public function handle() $baseName = $parts->last(); $basePath = $this->getPath($baseViewModel); - if (! file_exists($basePath)) { + if (!file_exists($basePath)) { $this->warn("Base view model {$baseViewModel} doesn't exist, generating..."); $this->call(MakeBaseViewModel::class, [ diff --git a/src/Commands/ProxyMakeCommand.php b/src/Commands/ProxyMakeCommand.php new file mode 100644 index 0000000..edc0bdd --- /dev/null +++ b/src/Commands/ProxyMakeCommand.php @@ -0,0 +1,11 @@ +version() >= 11) { - $package->hasCommand(DomainEnumMakeCommand::class); + $package->hasCommand(\Lunarstorm\LaravelDDD\Commands\DomainEnumMakeCommand::class); } } diff --git a/src/Support/Domain.php b/src/Support/Domain.php index 7fe53ad..9d7f385 100644 --- a/src/Support/Domain.php +++ b/src/Support/Domain.php @@ -19,6 +19,8 @@ class Domain public readonly DomainNamespaces $namespace; + public static array $objects = []; + public function __construct(string $domain, ?string $subdomain = null) { if (is_null($subdomain)) { @@ -57,6 +59,14 @@ public function __construct(string $domain, ?string $subdomain = null) $this->path = Path::join(DomainResolver::getConfiguredDomainPath(), $this->domainWithSubdomain); } + protected function registerDomainObjects() + { + } + + protected function registerDomainObject() + { + } + protected function getDomainBasePath() { return app()->basePath(DomainResolver::getConfiguredDomainPath()); @@ -82,24 +92,33 @@ public function relativePath(string $path = ''): string return collect([$this->domain, $path])->filter()->implode(DIRECTORY_SEPARATOR); } - public function model(string $name): DomainObject + public function namespaceFor(string $type): string { - $name = str_replace($this->namespace->models.'\\', '', $name); + return DomainResolver::getDomainObjectNamespace($this->domainWithSubdomain, $type); + } + + public function object(string $type, string $name): DomainObject + { + $namespace = $this->namespaceFor($type); return new DomainObject( - name: $name, - namespace: $this->namespace->models, - fqn: $this->namespace->models.'\\'.$name, - path: $this->path($this->namespace->models.'\\'.$name), + name: str($name)->replace($this->namespace->root, '')->toString(), + namespace: $namespace, + fqn: $namespace.'\\'.$name, + path: $this->path($namespace.'\\'.$name), ); } - public function factory(string $name): DomainObject + public function model(string $name): DomainObject { - $name = str_replace($this->namespace->factories.'\\', '', $name); + return $this->object('model', $name); + } + public function factory(string $name): DomainObject + { + // Domain factories are a special breed... return new DomainObject( - name: $name, + name: str($name)->replace($this->namespace->root, '')->toString(), namespace: $this->namespace->factories, fqn: $this->namespace->factories.'\\'.$name, path: str("database/factories/{$this->domainWithSubdomain}/{$name}.php") @@ -110,14 +129,7 @@ public function factory(string $name): DomainObject public function dataTransferObject(string $name): DomainObject { - $name = str_replace($this->namespace->dataTransferObjects.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->dataTransferObjects, - fqn: $this->namespace->dataTransferObjects.'\\'.$name, - path: $this->path($this->namespace->dataTransferObjects.'\\'.$name), - ); + return $this->object('data_transfer_object', $name); } public function dto(string $name): DomainObject @@ -127,157 +139,66 @@ public function dto(string $name): DomainObject public function viewModel(string $name): DomainObject { - $name = str_replace($this->namespace->viewModels.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->viewModels, - fqn: $this->namespace->viewModels.'\\'.$name, - path: $this->path($this->namespace->viewModels.'\\'.$name), - ); + return $this->object('view_model', $name); } public function valueObject(string $name): DomainObject { - $name = str_replace($this->namespace->valueObjects.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->valueObjects, - fqn: $this->namespace->valueObjects.'\\'.$name, - path: $this->path($this->namespace->valueObjects.'\\'.$name), - ); + return $this->object('value_object', $name); } public function action(string $name): DomainObject { - $name = str_replace($this->namespace->actions.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->actions, - fqn: $this->namespace->actions.'\\'.$name, - path: $this->path($this->namespace->actions.'\\'.$name), - ); + return $this->object('action', $name); } public function cast(string $name): DomainObject { - $name = str_replace($this->namespace->casts.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->casts, - fqn: $this->namespace->casts.'\\'.$name, - path: $this->path($this->namespace->casts.'\\'.$name), - ); + return $this->object('cast', $name); } public function command(string $name): DomainObject { - $name = str_replace($this->namespace->commands.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->commands, - fqn: $this->namespace->commands.'\\'.$name, - path: $this->path($this->namespace->commands.'\\'.$name), - ); + return $this->object('command', $name); } public function enum(string $name): DomainObject { - $name = str_replace($this->namespace->enums.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->enums, - fqn: $this->namespace->enums.'\\'.$name, - path: $this->path($this->namespace->enums.'\\'.$name), - ); + return $this->object('enum', $name); } public function job(string $name): DomainObject { - $name = str_replace($this->namespace->jobs.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->jobs, - fqn: $this->namespace->jobs.'\\'.$name, - path: $this->path($this->namespace->jobs.'\\'.$name), - ); + return $this->object('job', $name); } public function mail(string $name): DomainObject { - $name = str_replace($this->namespace->mail.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->mail, - fqn: $this->namespace->mail.'\\'.$name, - path: $this->path($this->namespace->mail.'\\'.$name), - ); + return $this->object('mail', $name); } public function notification(string $name): DomainObject { - $name = str_replace($this->namespace->notifications.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->notifications, - fqn: $this->namespace->notifications.'\\'.$name, - path: $this->path($this->namespace->notifications.'\\'.$name), - ); + return $this->object('notification', $name); } public function resource(string $name): DomainObject { - $name = str_replace($this->namespace->resources.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->resources, - fqn: $this->namespace->resources.'\\'.$name, - path: $this->path($this->namespace->resources.'\\'.$name), - ); + return $this->object('resource', $name); } public function rule(string $name): DomainObject { - $name = str_replace($this->namespace->rules.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->rules, - fqn: $this->namespace->rules.'\\'.$name, - path: $this->path($this->namespace->rules.'\\'.$name), - ); + return $this->object('rule', $name); } public function event(string $name): DomainObject { - $name = str_replace($this->namespace->events.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->events, - fqn: $this->namespace->events.'\\'.$name, - path: $this->path($this->namespace->events.'\\'.$name), - ); + return $this->object('event', $name); } public function exception(string $name): DomainObject { - $name = str_replace($this->namespace->exceptions.'\\', '', $name); - - return new DomainObject( - name: $name, - namespace: $this->namespace->exceptions, - fqn: $this->namespace->exceptions.'\\'.$name, - path: $this->path($this->namespace->exceptions.'\\'.$name), - ); + return $this->object('exception', $name); } } diff --git a/src/Support/DomainResolver.php b/src/Support/DomainResolver.php index 508e750..38ab361 100644 --- a/src/Support/DomainResolver.php +++ b/src/Support/DomainResolver.php @@ -16,12 +16,22 @@ public static function getConfiguredDomainNamespace(): ?string return config('ddd.domain_namespace'); } + public static function getRelativeObjectNamespace(string $type): string + { + return config("ddd.namespaces.{$type}", str($type)->plural()->studly()->toString()); + } + + public static function getDomainObjectNamespace(string $domain, string $type): string + { + return implode('\\', [static::getConfiguredDomainNamespace(), $domain, static::getRelativeObjectNamespace($type)]); + } + public static function guessDomainFromClass(string $class): ?string { $domainNamespace = Str::finish(DomainResolver::getConfiguredDomainNamespace(), '\\'); - if (! str($class)->startsWith($domainNamespace)) { - // Not a domain model + if (!str($class)->startsWith($domainNamespace)) { + // Not a domain object return null; } diff --git a/src/ValueObjects/DomainNamespaces.php b/src/ValueObjects/DomainNamespaces.php index 035dac0..1750a13 100644 --- a/src/ValueObjects/DomainNamespaces.php +++ b/src/ValueObjects/DomainNamespaces.php @@ -39,22 +39,22 @@ public static function from(string $domain, ?string $subdomain = null): self return new self( root: $domainNamespace, - models: "{$domainNamespace}\\".config('ddd.namespaces.models', 'Models'), + models: "{$domainNamespace}\\".config('ddd.namespaces.model', 'Models'), factories: "Database\\Factories\\{$domainWithSubdomain}", - dataTransferObjects: "{$domainNamespace}\\".config('ddd.namespaces.data_transfer_objects', 'Data'), - viewModels: "{$domainNamespace}\\".config('ddd.namespaces.view_models', 'ViewModels'), - valueObjects: "{$domainNamespace}\\".config('ddd.namespaces.value_objects', 'ValueObjects'), - actions: "{$domainNamespace}\\".config('ddd.namespaces.actions', 'Actions'), + dataTransferObjects: "{$domainNamespace}\\".config('ddd.namespaces.data_transfer_object', 'Data'), + viewModels: "{$domainNamespace}\\".config('ddd.namespaces.view_model', 'ViewModels'), + valueObjects: "{$domainNamespace}\\".config('ddd.namespaces.value_object', 'ValueObjects'), + actions: "{$domainNamespace}\\".config('ddd.namespaces.action', 'Actions'), enums: "{$domainNamespace}\\".config('ddd.namespaces.enums', 'Enums'), - events: "{$domainNamespace}\\".config('ddd.namespaces.events', 'Events'), - casts: "{$domainNamespace}\\".config('ddd.namespaces.casts', 'Casts'), - commands: "{$domainNamespace}\\".config('ddd.namespaces.commands', 'Commands'), - exceptions: "{$domainNamespace}\\".config('ddd.namespaces.exceptions', 'Exceptions'), - jobs: "{$domainNamespace}\\".config('ddd.namespaces.jobs', 'Jobs'), + events: "{$domainNamespace}\\".config('ddd.namespaces.event', 'Events'), + casts: "{$domainNamespace}\\".config('ddd.namespaces.cast', 'Casts'), + commands: "{$domainNamespace}\\".config('ddd.namespaces.command', 'Commands'), + exceptions: "{$domainNamespace}\\".config('ddd.namespaces.exception', 'Exceptions'), + jobs: "{$domainNamespace}\\".config('ddd.namespaces.job', 'Jobs'), mail: "{$domainNamespace}\\".config('ddd.namespaces.mail', 'Mail'), - notifications: "{$domainNamespace}\\".config('ddd.namespaces.notifications', 'Notifications'), - resources: "{$domainNamespace}\\".config('ddd.namespaces.resources', 'Resources'), - rules: "{$domainNamespace}\\".config('ddd.namespaces.rules', 'Rules'), + notifications: "{$domainNamespace}\\".config('ddd.namespaces.notification', 'Notifications'), + resources: "{$domainNamespace}\\".config('ddd.namespaces.resource', 'Resources'), + rules: "{$domainNamespace}\\".config('ddd.namespaces.rule', 'Rules'), ); } } diff --git a/src/ValueObjects/DomainObjectNamespace.php b/src/ValueObjects/DomainObjectNamespace.php new file mode 100644 index 0000000..1278844 --- /dev/null +++ b/src/ValueObjects/DomainObjectNamespace.php @@ -0,0 +1,30 @@ +when($subdomain, fn ($domain) => $domain->append("\\{$subdomain}")) + ->toString(); + + $root = DomainResolver::getConfiguredDomainNamespace(); + + $domainNamespace = implode('\\', [$root, $domainWithSubdomain]); + + $namespace = "{$domainNamespace}\\".config("ddd.namespaces.{$key}", Str::studly($key)); + + return new self(key: $key, namespace: $namespace); + } +} diff --git a/tests/Generator/ExtendedCommandsTest.php b/tests/Generator/ExtendedCommandsTest.php new file mode 100644 index 0000000..06317ee --- /dev/null +++ b/tests/Generator/ExtendedCommandsTest.php @@ -0,0 +1,50 @@ +object($type, $objectName); + + $relativePath = $domainObject->path; + $expectedNamespace = $domainObject->namespace; + $expectedPath = base_path($relativePath); + + if (file_exists($expectedPath)) { + unlink($expectedPath); + } + + expect(file_exists($expectedPath))->toBeFalse(); + + $command = "ddd:{$type} {$domain->domain}:{$objectName}"; + + Artisan::call($command); + + expect(Artisan::output())->toContainFilepath($relativePath); + + expect(file_exists($expectedPath))->toBeTrue(); + + expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); +})->with([ + 'cast' => ['cast', 'SomeCast'], + 'channel' => ['channel', 'SomeChannel'], + 'command' => ['command', 'SomeCommand'], + 'enum' => ['enum', 'SomeEnum'], + 'event' => ['event', 'SomeEvent'], + 'exception' => ['exception', 'SomeException'], + 'job' => ['job', 'SomeJob'], + 'listener' => ['listener', 'SomeListener'], + 'mail' => ['mail', 'SomeMail'], + 'notification' => ['notification', 'SomeNotification'], + 'observer' => ['observer', 'SomeObserver'], + 'policy' => ['policy', 'SomePolicy'], + 'provider' => ['provider', 'SomeProvider'], + 'resource' => ['resource', 'SomeResource'], + 'rule' => ['rule', 'SomeRule'], + 'scope' => ['scope', 'SomeScope'], +])->with('domainPaths'); diff --git a/tests/Generator/MakeActionTest.php b/tests/Generator/MakeActionTest.php index e101006..c3395a5 100644 --- a/tests/Generator/MakeActionTest.php +++ b/tests/Generator/MakeActionTest.php @@ -15,7 +15,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.actions'), + config('ddd.namespaces.action'), "{$name}.php", ]); @@ -27,7 +27,7 @@ expect(file_exists($expectedPath))->toBeFalse(); - Artisan::call("ddd:action {$domain} {$name}"); + Artisan::call("ddd:action {$domain}:{$name}"); expect(Artisan::output())->when( Feature::IncludeFilepathInGeneratorCommandOutput->exists(), @@ -39,7 +39,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.actions'), + config('ddd.namespaces.action'), ]); expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); @@ -51,11 +51,11 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.actions'), + config('ddd.namespaces.action'), "{$normalized}.php", ])); - Artisan::call("ddd:action {$domain} {$given}"); + Artisan::call("ddd:action {$domain}:{$given}"); expect(file_exists($expectedPath))->toBeTrue(); })->with('makeActionInputs'); @@ -76,7 +76,7 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.actions'), + config('ddd.namespaces.action'), "{$name}.php", ])); @@ -84,11 +84,11 @@ unlink($expectedPath); } - Artisan::call("ddd:action {$domain} {$name}"); + Artisan::call("ddd:action {$domain}:{$name}"); expect(file_exists($expectedPath))->toBeTrue(); - expect(file_get_contents($expectedPath))->toContain("class {$name} extends {$baseAction}".PHP_EOL.'{'); + expect(file_get_contents($expectedPath))->toContain("class {$name} extends {$baseAction}" . PHP_EOL . '{'); })->with([ 'BaseAction' => 'BaseAction', 'Base\Action' => 'Base\Action', @@ -103,7 +103,7 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.actions'), + config('ddd.namespaces.action'), "{$name}.php", ])); @@ -111,8 +111,8 @@ unlink($expectedPath); } - Artisan::call("ddd:action {$domain} {$name}"); + Artisan::call("ddd:action {$domain}:{$name}"); expect(file_exists($expectedPath))->toBeTrue(); - expect(file_get_contents($expectedPath))->toContain("class {$name}".PHP_EOL.'{'); + expect(file_get_contents($expectedPath))->toContain("class {$name}" . PHP_EOL . '{'); }); diff --git a/tests/Generator/MakeBaseModelTest.php b/tests/Generator/MakeBaseModelTest.php index 3a7fe08..c77edf8 100644 --- a/tests/Generator/MakeBaseModelTest.php +++ b/tests/Generator/MakeBaseModelTest.php @@ -14,7 +14,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), "{$modelName}.php", ]); @@ -38,7 +38,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), ]); expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); diff --git a/tests/Generator/MakeBaseViewModelTest.php b/tests/Generator/MakeBaseViewModelTest.php index 2e7f5a2..038ef5c 100644 --- a/tests/Generator/MakeBaseViewModelTest.php +++ b/tests/Generator/MakeBaseViewModelTest.php @@ -14,7 +14,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.view_models'), + config('ddd.namespaces.view_model'), "{$className}.php", ]); @@ -38,7 +38,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.view_models'), + config('ddd.namespaces.view_model'), ]); expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); diff --git a/tests/Generator/MakeCastTest.php b/tests/Generator/MakeCastTest.php deleted file mode 100644 index 78a5035..0000000 --- a/tests/Generator/MakeCastTest.php +++ /dev/null @@ -1,30 +0,0 @@ -cast($name)->path; - $expectedNamespace = $domain->namespace->casts; - $expectedPath = base_path($relativePath); - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:cast {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeCommandTest.php b/tests/Generator/MakeCommandTest.php deleted file mode 100644 index ea798b4..0000000 --- a/tests/Generator/MakeCommandTest.php +++ /dev/null @@ -1,30 +0,0 @@ -command($name)->path; - $expectedNamespace = $domain->namespace->commands; - $expectedPath = base_path($relativePath); - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:command {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeDataTransferObjectTest.php b/tests/Generator/MakeDataTransferObjectTest.php index e4e9b4e..0c03cef 100644 --- a/tests/Generator/MakeDataTransferObjectTest.php +++ b/tests/Generator/MakeDataTransferObjectTest.php @@ -15,7 +15,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.data_transfer_objects'), + config('ddd.namespaces.data_transfer_object'), "{$dtoName}.php", ]); @@ -39,7 +39,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.data_transfer_objects'), + config('ddd.namespaces.data_transfer_object'), ]); expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); @@ -51,11 +51,11 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.data_transfer_objects'), + config('ddd.namespaces.data_transfer_object'), "{$normalized}.php", ])); - Artisan::call("ddd:dto {$domain} {$given}"); + Artisan::call("ddd:dto {$domain}:{$given}"); expect(file_exists($expectedPath))->toBeTrue(); })->with('makeDtoInputs'); diff --git a/tests/Generator/MakeEnumTest.php b/tests/Generator/MakeEnumTest.php deleted file mode 100644 index d27dc7c..0000000 --- a/tests/Generator/MakeEnumTest.php +++ /dev/null @@ -1,30 +0,0 @@ -enum($name)->path; - $expectedNamespace = $domain->namespace->enums; - $expectedPath = base_path($relativePath); - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:enum {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeEventTest.php b/tests/Generator/MakeEventTest.php deleted file mode 100644 index 8ba7ef9..0000000 --- a/tests/Generator/MakeEventTest.php +++ /dev/null @@ -1,30 +0,0 @@ -event($name)->path; - $expectedNamespace = $domain->namespace->events; - $expectedPath = base_path($relativePath); - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:event {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeExceptionTest.php b/tests/Generator/MakeExceptionTest.php deleted file mode 100644 index 8e7a00e..0000000 --- a/tests/Generator/MakeExceptionTest.php +++ /dev/null @@ -1,30 +0,0 @@ -exception($name)->path; - $expectedNamespace = $domain->namespace->exceptions; - $expectedPath = base_path($relativePath); - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:exception {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeJobTest.php b/tests/Generator/MakeJobTest.php deleted file mode 100644 index eae924f..0000000 --- a/tests/Generator/MakeJobTest.php +++ /dev/null @@ -1,30 +0,0 @@ -job($name)->path; - $expectedNamespace = $domain->namespace->jobs; - $expectedPath = base_path($relativePath); - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:job {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeMailTest.php b/tests/Generator/MakeMailTest.php deleted file mode 100644 index eecdfec..0000000 --- a/tests/Generator/MakeMailTest.php +++ /dev/null @@ -1,30 +0,0 @@ -mail($name)->path; - $expectedPath = base_path($relativePath); - $expectedNamespace = $domain->namespace->mail; - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:mail {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeModelTest.php b/tests/Generator/MakeModelTest.php index 3c471ae..4fc803e 100644 --- a/tests/Generator/MakeModelTest.php +++ b/tests/Generator/MakeModelTest.php @@ -16,7 +16,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), "{$modelName}.php", ]); @@ -40,7 +40,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), ]); expect(file_get_contents($expectedModelPath))->toContain("namespace {$expectedNamespace};"); @@ -96,11 +96,11 @@ $expectedModelPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), "{$normalized}.php", ])); - Artisan::call("ddd:model {$domain} {$given}"); + Artisan::call("ddd:model {$domain}:{$given}"); expect(file_exists($expectedModelPath))->toBeTrue(); })->with('makeModelInputs'); @@ -114,14 +114,14 @@ $expectedModelPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), "{$modelName}.php", ])); $expectedModelClass = implode('\\', [ basename(config('ddd.domain_path')), $domain, - config('ddd.namespaces.models'), + config('ddd.namespaces.model'), $modelName, ]); diff --git a/tests/Generator/MakeNotificationTest.php b/tests/Generator/MakeNotificationTest.php deleted file mode 100644 index a7a274c..0000000 --- a/tests/Generator/MakeNotificationTest.php +++ /dev/null @@ -1,30 +0,0 @@ -notification($name)->path; - $expectedPath = base_path($relativePath); - $expectedNamespace = $domain->namespace->notifications; - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:notification {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeResourceTest.php b/tests/Generator/MakeResourceTest.php deleted file mode 100644 index 03603a7..0000000 --- a/tests/Generator/MakeResourceTest.php +++ /dev/null @@ -1,30 +0,0 @@ -resource($name)->path; - $expectedPath = base_path($relativePath); - $expectedNamespace = $domain->namespace->resources; - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:resource {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeRuleTest.php b/tests/Generator/MakeRuleTest.php deleted file mode 100644 index 68e2793..0000000 --- a/tests/Generator/MakeRuleTest.php +++ /dev/null @@ -1,30 +0,0 @@ -rule($name)->path; - $expectedPath = base_path($relativePath); - $expectedNamespace = $domain->namespace->rules; - - if (file_exists($expectedPath)) { - unlink($expectedPath); - } - - expect(file_exists($expectedPath))->toBeFalse(); - - Artisan::call("make:rule {$domain->domain}:{$name}"); - - expect(Artisan::output())->toContainFilepath($relativePath); - - expect(file_exists($expectedPath))->toBeTrue(); - - expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); -})->with('domainPaths'); diff --git a/tests/Generator/MakeValueObjectTest.php b/tests/Generator/MakeValueObjectTest.php index be16a54..86a527a 100644 --- a/tests/Generator/MakeValueObjectTest.php +++ b/tests/Generator/MakeValueObjectTest.php @@ -15,7 +15,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.value_objects'), + config('ddd.namespaces.value_object'), "{$valueObjectName}.php", ]); @@ -39,7 +39,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.value_objects'), + config('ddd.namespaces.value_object'), ]); expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); @@ -51,11 +51,11 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.value_objects'), + config('ddd.namespaces.value_object'), "{$normalized}.php", ])); - Artisan::call("ddd:value {$domain} {$given}"); + Artisan::call("ddd:value {$domain}:{$given}"); expect(file_exists($expectedPath))->toBeTrue(); })->with([ diff --git a/tests/Generator/MakeViewModelTest.php b/tests/Generator/MakeViewModelTest.php index 9d2c3e9..75363b5 100644 --- a/tests/Generator/MakeViewModelTest.php +++ b/tests/Generator/MakeViewModelTest.php @@ -15,7 +15,7 @@ $relativePath = implode('/', [ $domainPath, $domain, - config('ddd.namespaces.view_models'), + config('ddd.namespaces.view_model'), "{$viewModelName}.php", ]); @@ -39,7 +39,7 @@ $expectedNamespace = implode('\\', [ $domainRoot, $domain, - config('ddd.namespaces.view_models'), + config('ddd.namespaces.view_model'), ]); expect(file_get_contents($expectedPath))->toContain("namespace {$expectedNamespace};"); @@ -51,11 +51,11 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.view_models'), + config('ddd.namespaces.view_model'), "{$normalized}.php", ])); - Artisan::call("ddd:view-model {$domain} {$given}"); + Artisan::call("ddd:view-model {$domain}:{$given}"); expect(file_exists($expectedPath))->toBeTrue(); })->with('makeViewModelInputs'); @@ -67,7 +67,7 @@ $expectedPath = base_path(implode('/', [ config('ddd.domain_path'), $domain, - config('ddd.namespaces.view_models'), + config('ddd.namespaces.view_model'), "{$className}.php", ])); @@ -78,7 +78,7 @@ expect(file_exists($expectedPath))->toBeFalse(); // This currently only tests for the default base model - $expectedBaseViewModelPath = base_path(config('ddd.domain_path').'/Shared/ViewModels/ViewModel.php'); + $expectedBaseViewModelPath = base_path(config('ddd.domain_path') . '/Shared/ViewModels/ViewModel.php'); if (file_exists($expectedBaseViewModelPath)) { unlink($expectedBaseViewModelPath); diff --git a/tests/Support/DomainTest.php b/tests/Support/DomainTest.php index 5b20189..5e3d760 100644 --- a/tests/Support/DomainTest.php +++ b/tests/Support/DomainTest.php @@ -79,3 +79,13 @@ ['Reporting', 'SendInvoiceReport', 'Domain\\Reporting\\Actions\\SendInvoiceReport', 'src/Domain/Reporting/Actions/SendInvoiceReport.php'], ['Reporting.Internal', 'SendInvoiceReport', 'Domain\\Reporting\\Internal\\Actions\\SendInvoiceReport', 'src/Domain/Reporting/Internal/Actions/SendInvoiceReport.php'], ]); + +it('can describe an anonymous domain object', function ($domainName, $objectType, $objectName, $expectedFQN, $expectedPath) { + expect((new Domain($domainName))->object($objectType, $objectName)) + ->name->toBe($objectName) + ->fqn->toBe($expectedFQN) + ->path->toBe(Path::normalize($expectedPath)); +})->with([ + ['Invoicing', 'rule', 'SomeRule', 'Domain\\Invoicing\\Rules\\SomeRule', 'src/Domain/Invoicing/Rules/SomeRule.php'], + ['Other', 'thing', 'Something', 'Domain\\Other\\Things\\Something', 'src/Domain/Other/Things/Something.php'], +]);