Skip to content

Commit

Permalink
Further refinements and WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Oct 14, 2024
1 parent 3668e05 commit 86baa31
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 55 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ In production, you should cache the autoload manifests using the `ddd:cache` com
This is the content of the published config file (`ddd.php`):

```php
<?php

return [

/*
Expand All @@ -264,7 +262,7 @@ return [
*/
'domain_namespace' => 'Domain',

/*
/*
|--------------------------------------------------------------------------
| Application Layer
|--------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"pestphp/pest-plugin-laravel": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0"
"phpstan/phpstan-phpunit": "^1.0",
"spatie/laravel-data": "^4.10"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion config/ddd.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
'domain_namespace' => 'Domain',

/*
/*
|--------------------------------------------------------------------------
| Application Layer
|--------------------------------------------------------------------------
Expand Down
13 changes: 7 additions & 6 deletions src/Commands/CacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

use Illuminate\Console\Command;
use Lunarstorm\LaravelDDD\Support\DomainAutoloader;
use Lunarstorm\LaravelDDD\Support\DomainMigration;

class CacheCommand extends Command
{
protected $name = 'ddd:cache';

protected $description = 'Cache auto-discovered domain objects used for autoloading.';
protected $description = 'Cache auto-discovered domain objects and migration paths.';

public function handle()
{
DomainAutoloader::cacheProviders();
$this->components->info('Caching DDD providers, commands, migration paths.');

$this->components->info('Domain providers cached successfully.');
$this->components->task('domain providers', fn () => DomainAutoloader::cacheProviders());
$this->components->task('domain commands', fn () => DomainAutoloader::cacheCommands());
$this->components->task('domain migration paths', fn () => DomainMigration::cachePaths());

DomainAutoloader::cacheCommands();

$this->components->info('Domain commands cached successfully.');
$this->newLine();
}
}
13 changes: 13 additions & 0 deletions src/Commands/DomainMiddlewareMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Lunarstorm\LaravelDDD\Commands;

use Illuminate\Routing\Console\MiddlewareMakeCommand;
use Lunarstorm\LaravelDDD\Commands\Concerns\ResolvesDomainFromInput;

class DomainMiddlewareMakeCommand extends MiddlewareMakeCommand
{
use ResolvesDomainFromInput;

protected $name = 'ddd:middleware';
}
4 changes: 4 additions & 0 deletions src/Commands/DomainModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ protected function shouldCreateBaseModel(): bool
{
$baseModel = config('ddd.base_model');

if (is_null($baseModel)) {
return false;
}

// If the class exists, we don't need to create it.
if (class_exists($baseModel)) {
return false;
Expand Down
36 changes: 36 additions & 0 deletions src/DomainManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ class DomainManager
*/
protected $autoloadFilter;

/**
* The application layer filter callback.
*
* @var callable|null
*/
protected $applicationLayerFilter;

/**
* The application layer object resolver callback.
*
* @var callable|null
*/
protected $applicationLayerNamespaceResolver;

public function __construct()
{
$this->autoloadFilter = null;
$this->applicationLayerFilter = null;
$this->applicationLayerNamespaceResolver = null;
}

public function filterAutoloadPathsUsing(callable $filter): void
Expand All @@ -25,4 +41,24 @@ public function getAutoloadFilter(): ?callable
{
return $this->autoloadFilter;
}

public function filterApplicationLayerUsing(callable $filter): void
{
$this->applicationLayerFilter = $filter;
}

public function getApplicationLayerFilter(): ?callable
{
return $this->applicationLayerFilter;
}

public function resolveApplicationLayerNamespaceUsing(callable $resolver): void
{
$this->applicationLayerNamespaceResolver = $resolver;
}

public function getApplicationLayerNamespaceResolver(): ?callable
{
return $this->applicationLayerNamespaceResolver;
}
}
1 change: 1 addition & 0 deletions src/Facades/DDD.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @see \Lunarstorm\LaravelDDD\DomainManager
*
* @method static void filterAutoloadPathsUsing(callable $filter)
* @method static void resolveApplicationLayerNamespaceUsing(callable $resolver)
*/
class DDD extends Facade
{
Expand Down
11 changes: 6 additions & 5 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function configurePackage(Package $package): void
Commands\DomainJobMakeCommand::class,
Commands\DomainListenerMakeCommand::class,
Commands\DomainMailMakeCommand::class,
Commands\DomainMiddlewareMakeCommand::class,
Commands\DomainNotificationMakeCommand::class,
Commands\DomainObserverMakeCommand::class,
Commands\DomainPolicyMakeCommand::class,
Expand All @@ -66,11 +67,9 @@ public function configurePackage(Package $package): void
$package->hasCommand(Commands\DomainInterfaceMakeCommand::class);
$package->hasCommand(Commands\DomainTraitMakeCommand::class);
}

$this->registerDomainMigrateMakeCommand();
}

protected function registerDomainMigrateMakeCommand()
protected function registerMigrations()
{
$this->app->singleton(Commands\Migration\DomainMigrateMakeCommand::class, function ($app) {
// Once we have the migration creator registered, we will create the command
Expand All @@ -81,19 +80,21 @@ protected function registerDomainMigrateMakeCommand()

return new Commands\Migration\DomainMigrateMakeCommand($creator, $composer);
});

$this->loadMigrationsFrom(DomainMigration::paths());
}

public function packageBooted()
{
$this->publishes([
$this->package->basePath('/../stubs') => resource_path("stubs/{$this->package->shortName()}"),
], "{$this->package->shortName()}-stubs");

$this->loadMigrationsFrom(DomainMigration::paths());
}

public function packageRegistered()
{
(new DomainAutoloader)->autoload();

$this->registerMigrations();
}
}
15 changes: 12 additions & 3 deletions src/Support/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ public function guessNamespaceFromName(string $name): string

public function object(string $type, string $name, bool $absolute = false): DomainObject
{
$namespaceResolver = app('ddd')->getApplicationLayerNamespaceResolver();

$namespace = match (true) {
is_callable($namespaceResolver) => $namespaceResolver(
domain: $this->domainWithSubdomain,

Check failure on line 128 in src/Support/Domain.php

View workflow job for this annotation

GitHub Actions / phpstan

Unknown parameter $domain in call to callable callable(): mixed.
type: $type,

Check failure on line 129 in src/Support/Domain.php

View workflow job for this annotation

GitHub Actions / phpstan

Unknown parameter $type in call to callable callable(): mixed.
object: $name

Check failure on line 130 in src/Support/Domain.php

View workflow job for this annotation

GitHub Actions / phpstan

Unknown parameter $object in call to callable callable(): mixed.
),
$absolute => $this->namespace->root,
str($name)->startsWith('\\') => $this->guessNamespaceFromName($name),
default => $this->namespaceFor($type),
Expand All @@ -132,14 +139,16 @@ public function object(string $type, string $name, bool $absolute = false): Doma
->trim('\\')
->toString();

$fullyQualifiedName = $namespace.'\\'.$baseName;

return new DomainObject(
name: $baseName,
domain: $this->domain,
namespace: $namespace,
fullyQualifiedName: $namespace.'\\'.$baseName,
fullyQualifiedName: $fullyQualifiedName,
path: DomainResolver::isApplicationLayer($type)
? $this->pathInApplicationLayer($namespace.'\\'.$baseName)
: $this->path($namespace.'\\'.$baseName),
? $this->pathInApplicationLayer($fullyQualifiedName)
: $this->path($fullyQualifiedName),
type: $type
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Support/DomainMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ public static function domainMigrationFolder(): string

public static function cachePaths(): void
{
DomainCache::set('domain-migration-folders', static::discoverPaths());
DomainCache::set('domain-migration-paths', static::discoverPaths());
}

public static function clearCache(): void
{
DomainCache::forget('domain-migration-folders');
DomainCache::forget('domain-migration-paths');
}

public static function paths(): array
{
return DomainCache::has('domain-migration-folders')
? DomainCache::get('domain-migration-folders')
return DomainCache::has('domain-migration-paths')
? DomainCache::get('domain-migration-paths')
: static::discoverPaths();
}

Expand Down
68 changes: 39 additions & 29 deletions src/Support/DomainResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public static function domainRootNamespace(): ?string
return config('ddd.domain_namespace');
}

/**
* Get the current configured root application layer namespace.
*/
public static function applicationLayerRootNamespace(): ?string
{
return config('ddd.application_layer.namespace', 'App\Modules');
return config('ddd.application_layer.namespace');
}

/**
Expand All @@ -50,49 +53,56 @@ public static function getRelativeObjectNamespace(string $type): string
return config("ddd.namespaces.{$type}", str($type)->plural()->studly()->toString());
}

/**
* Determine whether a given object type is part of the application layer.
*/
public static function isApplicationLayer(string $type): bool
{
$applicationObjects = config('ddd.application_layer.objects', ['controller', 'request']);

return in_array($type, $applicationObjects);
}

// public static function getDomainControllerNamespace(string $domain, ?string $controller = null): string
// {
// $controllerRootNamespace = app()->getNamespace() . 'Http\Controllers';

// $namespace = collect([
// $controllerRootNamespace,
// $domain,
// ])->filter()->implode('\\');
$filter = app('ddd')->getApplicationLayerFilter() ?? function (string $type) {
$applicationObjects = config('ddd.application_layer.objects', ['controller', 'request']);

// if ($controller) {
// $namespace .= "\\{$controller}";
// }
return in_array($type, $applicationObjects);
};

// return $namespace;
// }
return $filter($type);
}

/**
* Resolve the root namespace for a given domain object type.
*
* @param string $type The domain object type.
*/
public static function resolveRootNamespace(string $type): ?string
{
return static::isApplicationLayer($type)
? static::applicationLayerRootNamespace()
: static::domainRootNamespace();
}

/**
* Get the fully qualified namespace for a domain object.
*
* @param string $domain The domain name.
* @param string $type The domain object type.
* @param string|null $object The domain object name.
*/
public static function getDomainObjectNamespace(string $domain, string $type, ?string $object = null): string
{
$namespace = collect([
static::resolveRootNamespace($type),
$domain,
static::getRelativeObjectNamespace($type),
])->filter()->implode('\\');

if ($object) {
$namespace .= "\\{$object}";
}
$resolver = app('ddd')->getApplicationLayerNamespaceResolver() ?? function (string $domain, string $type, ?string $object) {
$namespace = collect([
static::resolveRootNamespace($type),
$domain,
static::getRelativeObjectNamespace($type),
])->filter()->implode('\\');

if ($object) {
$namespace .= "\\{$object}";
}

return $namespace;
};

return $namespace;
return $resolver($domain, $type, $object);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
//
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Loading

0 comments on commit 86baa31

Please sign in to comment.