-
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.
Merge pull request #31 from lunarstorm/feat-sub-domain-factories
feat: Add support for subdomains in newFactory method (#29)
- Loading branch information
Showing
19 changed files
with
483 additions
and
81 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
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,19 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Lunarstorm\LaravelDDD\Factories\DomainFactory; | ||
|
||
abstract class DomainModel extends Model | ||
{ | ||
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,20 @@ | ||
<?php | ||
|
||
namespace Lunarstorm\LaravelDDD\Support; | ||
|
||
class Path | ||
{ | ||
public static function normalize($path) | ||
{ | ||
return str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path); | ||
} | ||
|
||
public static function join(...$parts) | ||
{ | ||
$parts = array_map(function ($part) { | ||
return trim(static::normalize($part), DIRECTORY_SEPARATOR); | ||
}, $parts); | ||
|
||
return implode(DIRECTORY_SEPARATOR, $parts); | ||
} | ||
} |
Oops, something went wrong.