Skip to content

Commit

Permalink
Refactor domain path and namespace configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Mar 23, 2024
1 parent 1cfaa38 commit 259297e
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 90 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

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

## [Unversioned]
### Added
- Config keys `ddd.domain_path` and `ddd.domain_namespace` added to specify path to the domain folder and root domain namespace. This allows a custom domain namespace if it differs from the basename of the domain path. e.g., `src/Domains` domain folder, but with `Domain` namespace.

### Deprecated
- Config `ddd.paths.domains` deprecated in favour of `ddd.domain_path` and `ddd.domain_namespace`.

## [0.9.0] - 2024-03-11
### Changed
- Internals: normalize generator file paths using `DIRECTORY_SEPARATOR` for consistency across different operating systems when it comes to console output and test expectations.
Expand Down
40 changes: 12 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,23 @@ return [

/*
|--------------------------------------------------------------------------
| Paths
| Domain Path
|--------------------------------------------------------------------------
|
| This value contains paths to the layers of the application in the context
| of domain driven design, relative to the base folder of the application.
| The path to the domain folder relative to the application root.
|
*/
'domain_path' => 'src/Domain',

'paths' => [
//
// Path to the Domain layer.
//
'domains' => 'src/Domain',
],
/*
|--------------------------------------------------------------------------
| Domain Namespace
|--------------------------------------------------------------------------
|
| The root domain namespace.
|
*/
'domain_namespace' => 'Domain',

/*
|--------------------------------------------------------------------------
Expand All @@ -114,29 +117,10 @@ return [
|
*/
'namespaces' => [
//
// Models
//
'models' => 'Models',

//
// Data Transfer Objects (DTO)
//
'data_transfer_objects' => 'Data',

//
// View Models
//
'view_models' => 'ViewModels',

//
// Value Objects
//
'value_objects' => 'ValueObjects',

//
// Actions
//
'actions' => 'Actions',
],

Expand Down
40 changes: 12 additions & 28 deletions config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

/*
|--------------------------------------------------------------------------
| Paths
| Domain Path
|--------------------------------------------------------------------------
|
| This value contains paths to the layers of the application in the context
| of domain driven design, relative to the base folder of the application.
| The path to the domain folder relative to the application root.
|
*/
'domain_path' => 'src/Domain',

'paths' => [
//
// Path to the Domain layer.
//
'domains' => 'src/Domain',
],
/*
|--------------------------------------------------------------------------
| Domain Namespace
|--------------------------------------------------------------------------
|
| The root domain namespace.
|
*/
'domain_namespace' => 'Domain',

/*
|--------------------------------------------------------------------------
Expand All @@ -36,29 +39,10 @@
|
*/
'namespaces' => [
//
// Models
//
'models' => 'Models',

//
// Data Transfer Objects (DTO)
//
'data_transfer_objects' => 'Data',

//
// View Models
//
'view_models' => 'ViewModels',

//
// Value Objects
//
'value_objects' => 'ValueObjects',

//
// Actions
//
'actions' => 'Actions',
],

Expand Down
8 changes: 5 additions & 3 deletions src/Commands/DomainGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand All @@ -22,9 +23,8 @@ protected function getArguments()

protected function rootNamespace()
{
return str($this->getDomainBasePath())
return str(DomainResolver::getConfiguredDomainNamespace())
->rtrim('/\\')
->basename()
->toString();
}

Expand Down Expand Up @@ -58,7 +58,9 @@ protected function getDomain()

protected function getDomainBasePath()
{
return Path::normalize($this->laravel->basePath(config('ddd.paths.domains', 'src/Domains')));
return Path::normalize($this->laravel->basePath(
DomainResolver::getConfiguredDomainPath() ?? 'src/Domain'

Check failure on line 62 in src/Commands/DomainGeneratorCommand.php

View workflow job for this annotation

GitHub Actions / phpstan

Expression on left side of ?? is not nullable.
));
}

protected function getPath($name)
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Console\Command;
use Lunarstorm\LaravelDDD\Support\DomainResolver;
use Symfony\Component\Process\Process;

class InstallCommand extends Command
Expand Down Expand Up @@ -34,11 +35,10 @@ public function handle(): int

public function registerDomainAutoload()
{
$domainPath = config('ddd.paths.domains');
$domainPath = DomainResolver::getConfiguredDomainPath();

$domainRootNamespace = str($domainPath)
$domainRootNamespace = str(DomainResolver::getConfiguredDomainNamespace())
->rtrim('/\\')
->basename()
->toString();

$this->comment("Registering domain path `{$domainPath}` in composer.json...");
Expand Down
3 changes: 2 additions & 1 deletion src/Factories/DomainFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Support\DomainResolver;

abstract class DomainFactory extends Factory
{
Expand All @@ -14,7 +15,7 @@ abstract class DomainFactory extends Factory
*/
protected static function domainNamespace()
{
return basename(config('ddd.paths.domains')).'\\';
return Str::finish(DomainResolver::getConfiguredDomainNamespace(), '\\');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public function __construct(string $domain, ?string $subdomain = null)

$this->namespace = DomainNamespaces::from($this->domain, $this->subdomain);

$this->path = Path::join(config('ddd.paths.domains'), $this->domainWithSubdomain);
$this->path = Path::join(DomainResolver::getConfiguredDomainPath(), $this->domainWithSubdomain);
}

protected function getDomainBasePath()
{
return app()->basePath(config('ddd.paths.domains'));
return app()->basePath(DomainResolver::getConfiguredDomainPath());
}

public function path(?string $path = null): string
Expand Down
27 changes: 25 additions & 2 deletions src/Support/DomainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,36 @@

namespace Lunarstorm\LaravelDDD\Support;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;

class DomainResolver
{
public static function getConfiguredDomainPath(): string
{
if (Config::has('ddd.paths.domains')) {
// Deprecated
return config('ddd.paths.domains');
}

return config('ddd.domain_path');
}

public static function getConfiguredDomainNamespace(): string
{
if (Config::has('ddd.paths.domains')) {
// Deprecated
return basename(config('ddd.paths.domains'));
}

return config('ddd.domain_namespace');
}

public static function guessDomainFromClass(string $class): ?string
{
$domainNamespace = basename(config('ddd.paths.domains')).'\\';
$domainNamespace = Str::finish(DomainResolver::getConfiguredDomainNamespace(), '\\');

if (! str($class)->startsWith($domainNamespace)) {
if (!str($class)->startsWith($domainNamespace)) {
// Not a domain model
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ValueObjects/DomainNamespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Lunarstorm\LaravelDDD\ValueObjects;

use Lunarstorm\LaravelDDD\Support\DomainResolver;

class DomainNamespaces
{
public function __construct(
Expand All @@ -21,7 +23,7 @@ public static function from(string $domain, ?string $subdomain = null): self
->when($subdomain, fn ($domain) => $domain->append("\\{$subdomain}"))
->toString();

$root = basename(config('ddd.paths.domains'));
$root = DomainResolver::getConfiguredDomainNamespace();

$domainNamespace = implode('\\', [$root, $domainWithSubdomain]);

Expand Down
18 changes: 18 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Support\Facades\Config;
use Lunarstorm\LaravelDDD\Support\DomainResolver;

it('can customize the domain path via ddd.domain_path', function () {
$path = fake()->word();

Config::set('ddd.domain_path', $path);

expect(DomainResolver::getConfiguredDomainPath())->toEqual($path);
});

it('can customize the domain root namespace via ddd.domain_namespace', function () {
Config::set('ddd.domain_namespace', 'Doughmain');

expect(DomainResolver::getConfiguredDomainNamespace())->toEqual('Doughmain');
});
9 changes: 5 additions & 4 deletions tests/Generator/MakeActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;

it('can generate action objects', function ($domainPath, $domainRoot) {
Config::set('ddd.paths.domains', $domainPath);
Config::set('ddd.domain_path', $domainPath);
Config::set('ddd.domain_namespace', $domainRoot);

$name = Str::studly(fake()->word());
$domain = Str::studly(fake()->word());
Expand Down Expand Up @@ -48,7 +49,7 @@
$domain = Str::studly(fake()->word());

$expectedPath = base_path(implode('/', [
config('ddd.paths.domains'),
config('ddd.domain_path'),
$domain,
config('ddd.namespaces.actions'),
"{$normalized}.php",
Expand All @@ -73,7 +74,7 @@
$domain = Str::studly(fake()->word());

$expectedPath = base_path(implode('/', [
config('ddd.paths.domains'),
config('ddd.domain_path'),
$domain,
config('ddd.namespaces.actions'),
"{$name}.php",
Expand All @@ -100,7 +101,7 @@
$domain = Str::studly(fake()->word());

$expectedPath = base_path(implode('/', [
config('ddd.paths.domains'),
config('ddd.domain_path'),
$domain,
config('ddd.namespaces.actions'),
"{$name}.php",
Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/MakeBaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;

it('can generate domain base model', function ($domainPath, $domainRoot) {
Config::set('ddd.paths.domains', $domainPath);
Config::set('ddd.domain_path', $domainPath);
Config::set('ddd.domain_namespace', $domainRoot);

$modelName = 'BaseModel';
$domain = 'Shared';
Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/MakeBaseViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;

it('can generate base view model', function ($domainPath, $domainRoot) {
Config::set('ddd.paths.domains', $domainPath);
Config::set('ddd.domain_path', $domainPath);
Config::set('ddd.domain_namespace', $domainRoot);

$className = 'ViewModel';
$domain = 'Shared';
Expand Down
5 changes: 3 additions & 2 deletions tests/Generator/MakeDataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;

it('can generate data transfer objects', function ($domainPath, $domainRoot) {
Config::set('ddd.paths.domains', $domainPath);
Config::set('ddd.domain_path', $domainPath);
Config::set('ddd.domain_namespace', $domainRoot);

$dtoName = Str::studly(fake()->word());
$domain = Str::studly(fake()->word());
Expand Down Expand Up @@ -48,7 +49,7 @@
$domain = Str::studly(fake()->word());

$expectedPath = base_path(implode('/', [
config('ddd.paths.domains'),
config('ddd.domain_path'),
$domain,
config('ddd.namespaces.data_transfer_objects'),
"{$normalized}.php",
Expand Down
3 changes: 2 additions & 1 deletion tests/Generator/MakeFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
use Lunarstorm\LaravelDDD\Tests\Fixtures\Enums\Feature;

it('can generate domain factories', function ($domainPath, $domainRoot, $domain, $subdomain) {
Config::set('ddd.paths.domains', $domainPath);
Config::set('ddd.domain_path', $domainPath);
Config::set('ddd.domain_namespace', $domainRoot);

$modelName = Str::studly(fake()->word());

Expand Down
Loading

0 comments on commit 259297e

Please sign in to comment.