-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- implement abstract DomainFactory (extends Factory). - DomainFactory::resolveFactoryName() implements custom domain factory resolution. - Ensure factory resolution respects customizations made in ddd config. - Simplify base-model stub's newFactory method to use DomainFactory::factoryForModel(). - Refactor the API of the internal Domain support class. - Add more test coverage.
- Loading branch information
Showing
17 changed files
with
446 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
|
||
abstract class DomainFactory extends Factory | ||
{ | ||
/** | ||
* Get the domain namespace. | ||
* | ||
* @return string | ||
*/ | ||
protected static function domainNamespace() | ||
{ | ||
return basename(config('ddd.paths.domains')).'\\'; | ||
} | ||
|
||
/** | ||
* Get the factory name for the given domain model name. | ||
* | ||
* @param class-string<\Illuminate\Database\Eloquent\Model> $modelName | ||
* @return null|class-string<\Illuminate\Database\Eloquent\Factories\Factory> | ||
*/ | ||
public static function resolveFactoryName(string $modelName) | ||
{ | ||
$resolver = function (string $modelName) { | ||
$domainNamespace = static::domainNamespace(); | ||
$modelNamespace = config('ddd.namespaces.models'); | ||
|
||
// Expected domain model FQN: | ||
// {DomainNamespace}\{Domain}\{ModelNamespace}\{Model} | ||
|
||
if (! Str::startsWith($modelName, $domainNamespace)) { | ||
// Not a domain model | ||
return null; | ||
} | ||
|
||
$domain = str($modelName) | ||
->after($domainNamespace) | ||
->beforeLast($modelNamespace) | ||
->trim('\\') | ||
->toString(); | ||
|
||
$modelBaseName = class_basename($modelName); | ||
|
||
return static::$namespace."{$domain}\\{$modelBaseName}Factory"; | ||
}; | ||
|
||
return $resolver($modelName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Lunarstorm\LaravelDDD\Factories\DomainFactory; | ||
|
||
abstract class DomainModel | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
|
||
protected static function newFactory() | ||
{ | ||
return DomainFactory::factoryForModel(get_called_class()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\Support; | ||
|
||
class DomainResolver | ||
{ | ||
public static function fromModelClass(string $modelClass) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\ValueObjects; | ||
|
||
class DomainNamespaces | ||
{ | ||
public function __construct( | ||
public readonly string $root, | ||
public readonly string $models, | ||
public readonly string $factories, | ||
public readonly string $dataTransferObjects, | ||
public readonly string $viewModels, | ||
public readonly string $valueObjects, | ||
public readonly string $actions, | ||
) { | ||
} | ||
|
||
public static function from(string $domain, string $subdomain = null): self | ||
{ | ||
$domainWithSubdomain = str($domain) | ||
->when($subdomain, fn ($domain) => $domain->append("\\{$subdomain}")) | ||
->toString(); | ||
|
||
$root = basename(config('ddd.paths.domains')); | ||
|
||
$domainNamespace = implode('\\', [$root, $domainWithSubdomain]); | ||
|
||
return new self( | ||
root: $domainNamespace, | ||
models: "{$domainNamespace}\\".config('ddd.namespaces.models'), | ||
factories: "Database\\Factories\\{$domainWithSubdomain}", | ||
dataTransferObjects: "{$domainNamespace}\\".config('ddd.namespaces.data_transfer_objects'), | ||
viewModels: "{$domainNamespace}\\".config('ddd.namespaces.view_models'), | ||
valueObjects: "{$domainNamespace}\\".config('ddd.namespaces.value_objects'), | ||
actions: "{$domainNamespace}\\".config('ddd.namespaces.actions'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\ValueObjects; | ||
|
||
class DomainObject | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
public readonly string $namespace, | ||
public readonly string $fqn, | ||
public readonly string $path, | ||
) { | ||
} | ||
} |
Oops, something went wrong.