From 024c5148771885db7b2b88041104ba64df858b3a Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 12 Apr 2024 20:40:57 -0400 Subject: [PATCH 01/14] Remove cache:clearing hook. --- CHANGELOG.md | 4 ++++ src/LaravelDDDServiceProvider.php | 2 -- tests/Command/CacheTest.php | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aaa0d6..186bc85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-ddd` will be documented in this file. +## [Unversioned] +### Changed +- Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results regardless of being executed before or after `cache:clear` or `optimize:clear` commands. + ## [1.1.0] - 2024-04-07 ### Added - Add `ddd:class` generator extending Laravel's `make:class` (Laravel 11 only). diff --git a/src/LaravelDDDServiceProvider.php b/src/LaravelDDDServiceProvider.php index 19f6de2..f804c55 100644 --- a/src/LaravelDDDServiceProvider.php +++ b/src/LaravelDDDServiceProvider.php @@ -69,7 +69,5 @@ public function packageBooted() public function packageRegistered() { (new DomainAutoloader())->autoload(); - - Event::subscribe(CacheClearSubscriber::class); } } diff --git a/tests/Command/CacheTest.php b/tests/Command/CacheTest.php index 029e4c2..33835d0 100644 --- a/tests/Command/CacheTest.php +++ b/tests/Command/CacheTest.php @@ -40,3 +40,23 @@ expect(DomainCache::get('domain-providers'))->toBeNull(); expect(DomainCache::get('domain-commands'))->toBeNull(); }); + +it('will not be cleared by laravel cache clearing', function () { + expect(DomainCache::get('domain-providers'))->toBeNull(); + expect(DomainCache::get('domain-commands'))->toBeNull(); + + $this->artisan('ddd:cache')->execute(); + + expect(DomainCache::get('domain-providers'))->not->toBeNull(); + expect(DomainCache::get('domain-commands'))->not->toBeNull(); + + $this->artisan('cache:clear')->execute(); + + expect(DomainCache::get('domain-providers'))->not->toBeNull(); + expect(DomainCache::get('domain-commands'))->not->toBeNull(); + + $this->artisan('optimize:clear')->execute(); + + expect(DomainCache::get('domain-providers'))->not->toBeNull(); + expect(DomainCache::get('domain-commands'))->not->toBeNull(); +}); From 1b80095571e7f0ea18b9763c1aa41d678c0dc0d4 Mon Sep 17 00:00:00 2001 From: JasperTey Date: Sat, 13 Apr 2024 00:41:21 +0000 Subject: [PATCH 02/14] Fix styling --- src/LaravelDDDServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/LaravelDDDServiceProvider.php b/src/LaravelDDDServiceProvider.php index f804c55..4c953cc 100644 --- a/src/LaravelDDDServiceProvider.php +++ b/src/LaravelDDDServiceProvider.php @@ -2,8 +2,6 @@ namespace Lunarstorm\LaravelDDD; -use Illuminate\Support\Facades\Event; -use Lunarstorm\LaravelDDD\Listeners\CacheClearSubscriber; use Lunarstorm\LaravelDDD\Support\DomainAutoloader; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; From 932f656c46082aa9363c3f581d21bc5732b525fb Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 19:00:46 -0400 Subject: [PATCH 03/14] Autoload ignore & custom filter callback. --- CHANGELOG.md | 2 +- src/DomainManager.php | 28 ++++++++ src/Facades/DDD.php | 18 ++++++ src/Facades/LaravelDDD.php | 16 ----- src/LaravelDDD.php | 7 -- src/LaravelDDDServiceProvider.php | 6 ++ src/Support/DomainAutoloader.php | 24 ++++++- tests/Autoload/IgnoreTest.php | 102 ++++++++++++++++++++++++++++++ tests/Command/CacheTest.php | 2 + 9 files changed, 179 insertions(+), 26 deletions(-) create mode 100755 src/DomainManager.php create mode 100644 src/Facades/DDD.php delete mode 100644 src/Facades/LaravelDDD.php delete mode 100755 src/LaravelDDD.php create mode 100644 tests/Autoload/IgnoreTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 186bc85..deac72f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to `laravel-ddd` will be documented in this file. ## [Unversioned] ### Changed -- Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results regardless of being executed before or after `cache:clear` or `optimize:clear` commands. +- Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results no matter what order runs in production (before or after `cache:clear` or `optimize:clear` commands). ## [1.1.0] - 2024-04-07 ### Added diff --git a/src/DomainManager.php b/src/DomainManager.php new file mode 100755 index 0000000..2de6b0f --- /dev/null +++ b/src/DomainManager.php @@ -0,0 +1,28 @@ +autoloadFilter = null; + } + + public function filterAutoloadPathsUsing(callable $filter): void + { + $this->autoloadFilter = $filter; + } + + public function getAutoloadFilter(): ?callable + { + return $this->autoloadFilter; + } +} diff --git a/src/Facades/DDD.php b/src/Facades/DDD.php new file mode 100644 index 0000000..5164cda --- /dev/null +++ b/src/Facades/DDD.php @@ -0,0 +1,18 @@ +app->scoped(DomainManager::class, function () { + return new DomainManager(); + }); + + $this->app->bind('ddd', DomainManager::class); + /* * This class is a Package Service Provider * diff --git a/src/Support/DomainAutoloader.php b/src/Support/DomainAutoloader.php index ca75ec8..989c466 100644 --- a/src/Support/DomainAutoloader.php +++ b/src/Support/DomainAutoloader.php @@ -16,6 +16,7 @@ use Lunarstorm\LaravelDDD\Factories\DomainFactory; use Lunarstorm\LaravelDDD\ValueObjects\DomainObject; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use Throwable; class DomainAutoloader @@ -122,6 +123,25 @@ protected function handleFactories(): void }); } + protected static function finder($paths) + { + $filter = app('ddd')->getAutoloadFilter() ?? function (SplFileInfo $file) { + $pathAfterDomain = str($file->getRelativePath())->after('/')->finish('/'); + + $ignoredFolders = collect(config('ddd.autoload.ignore', [])) + ->map(fn ($path) => Str::finish($path, '/')); + + if ($pathAfterDomain->startsWith($ignoredFolders)) { + return false; + } + }; + + return Finder::create() + ->files() + ->in($paths) + ->filter($filter); + } + protected static function discoverProviders(): array { $configValue = config('ddd.autoload.providers'); @@ -138,7 +158,7 @@ protected static function discoverProviders(): array return []; } - return Lody::classesFromFinder(Finder::create()->files()->in($paths)) + return Lody::classesFromFinder(static::finder($paths)) ->isNotAbstract() ->isInstanceOf(ServiceProvider::class) ->toArray(); @@ -162,7 +182,7 @@ protected static function discoverCommands(): array return []; } - return Lody::classesFromFinder(Finder::create()->files()->in($paths)) + return Lody::classesFromFinder(static::finder($paths)) ->isNotAbstract() ->isInstanceOf(Command::class) ->toArray(); diff --git a/tests/Autoload/IgnoreTest.php b/tests/Autoload/IgnoreTest.php new file mode 100644 index 0000000..364d75b --- /dev/null +++ b/tests/Autoload/IgnoreTest.php @@ -0,0 +1,102 @@ +setupTestApplication(); +}); + +it('can ignore folders when autoloading', function () { + Artisan::call('ddd:cache'); + + $expected = [ + 'Domain\Invoicing\Providers\InvoiceServiceProvider', + 'Domain\Invoicing\Commands\InvoiceDeliver', + ]; + + $cached = [ + ...DomainCache::get('domain-providers'), + ...DomainCache::get('domain-commands'), + ]; + + expect($cached)->toEqual($expected); + + Config::set('ddd.autoload.ignore', ['Commands']); + + Artisan::call('ddd:cache'); + + $expected = [ + 'Domain\Invoicing\Providers\InvoiceServiceProvider', + ]; + + $cached = [ + ...DomainCache::get('domain-providers'), + ...DomainCache::get('domain-commands'), + ]; + + expect($cached)->toEqual($expected); + + Config::set('ddd.autoload.ignore', ['Providers']); + + Artisan::call('ddd:cache'); + + $expected = [ + 'Domain\Invoicing\Commands\InvoiceDeliver', + ]; + + $cached = [ + ...DomainCache::get('domain-providers'), + ...DomainCache::get('domain-commands'), + ]; + + expect($cached)->toEqual($expected); +}); + +it('can register a custom autoload filter', function () { + Artisan::call('ddd:cache'); + + $expected = [ + 'Domain\Invoicing\Providers\InvoiceServiceProvider', + 'Domain\Invoicing\Commands\InvoiceDeliver', + ]; + + $cached = [ + ...DomainCache::get('domain-providers'), + ...DomainCache::get('domain-commands'), + ]; + + expect($cached)->toEqual($expected); + + $secret = null; + + DDD::filterAutoloadPathsUsing(function (SplFileInfo $file) use (&$secret) { + $ignoredFiles = [ + 'InvoiceServiceProvider.php', + 'InvoiceDeliver.php', + ]; + + $secret = 'i-was-invoked'; + + if (Str::endsWith($file->getRelativePathname(), $ignoredFiles)) { + return false; + } + }); + + Artisan::call('ddd:cache'); + + $cached = [ + ...DomainCache::get('domain-providers'), + ...DomainCache::get('domain-commands'), + ]; + + expect($cached)->toEqual([]); + + expect($secret)->toEqual('i-was-invoked'); +}); diff --git a/tests/Command/CacheTest.php b/tests/Command/CacheTest.php index 33835d0..81a3587 100644 --- a/tests/Command/CacheTest.php +++ b/tests/Command/CacheTest.php @@ -42,6 +42,8 @@ }); it('will not be cleared by laravel cache clearing', function () { + config(['cache.default' => 'file']); + expect(DomainCache::get('domain-providers'))->toBeNull(); expect(DomainCache::get('domain-commands'))->toBeNull(); From 10fbd47735cfd88a66419c9bf4c4ed3b3d0de4fb Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 19:05:28 -0400 Subject: [PATCH 04/14] Normalize filter paths to forward slashes. --- src/Support/DomainAutoloader.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Support/DomainAutoloader.php b/src/Support/DomainAutoloader.php index 989c466..116a4c1 100644 --- a/src/Support/DomainAutoloader.php +++ b/src/Support/DomainAutoloader.php @@ -28,7 +28,7 @@ public function __construct() public function autoload(): void { - if (! config()->has('ddd.autoload')) { + if (!config()->has('ddd.autoload')) { return; } @@ -99,10 +99,10 @@ protected function handlePolicies(): void return Arr::wrap(Collection::times(count($classDirnameSegments), function ($index) use ($class, $classDirnameSegments) { $classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index)); - return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; + return $classDirname . '\\Policies\\' . class_basename($class) . 'Policy'; })->reverse()->values()->first(function ($class) { return class_exists($class); - }) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']); + }) ?: [$classDirname . '\\Policies\\' . class_basename($class) . 'Policy']); }); } @@ -115,18 +115,21 @@ protected function handleFactories(): void $appNamespace = static::appNamespace(); - $modelName = Str::startsWith($modelName, $appNamespace.'Models\\') - ? Str::after($modelName, $appNamespace.'Models\\') + $modelName = Str::startsWith($modelName, $appNamespace . 'Models\\') + ? Str::after($modelName, $appNamespace . 'Models\\') : Str::after($modelName, $appNamespace); - return 'Database\\Factories\\'.$modelName.'Factory'; + return 'Database\\Factories\\' . $modelName . 'Factory'; }); } protected static function finder($paths) { $filter = app('ddd')->getAutoloadFilter() ?? function (SplFileInfo $file) { - $pathAfterDomain = str($file->getRelativePath())->after('/')->finish('/'); + $pathAfterDomain = str($file->getRelativePath()) + ->replace('\\', '/') + ->after('/') + ->finish('/'); $ignoredFolders = collect(config('ddd.autoload.ignore', [])) ->map(fn ($path) => Str::finish($path, '/')); From 5a73f0abeb87ec178af2f1f4201a6893dfd7c851 Mon Sep 17 00:00:00 2001 From: JasperTey Date: Mon, 15 Apr 2024 23:05:52 +0000 Subject: [PATCH 05/14] Fix styling --- src/Support/DomainAutoloader.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Support/DomainAutoloader.php b/src/Support/DomainAutoloader.php index 116a4c1..025f328 100644 --- a/src/Support/DomainAutoloader.php +++ b/src/Support/DomainAutoloader.php @@ -28,7 +28,7 @@ public function __construct() public function autoload(): void { - if (!config()->has('ddd.autoload')) { + if (! config()->has('ddd.autoload')) { return; } @@ -99,10 +99,10 @@ protected function handlePolicies(): void return Arr::wrap(Collection::times(count($classDirnameSegments), function ($index) use ($class, $classDirnameSegments) { $classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index)); - return $classDirname . '\\Policies\\' . class_basename($class) . 'Policy'; + return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; })->reverse()->values()->first(function ($class) { return class_exists($class); - }) ?: [$classDirname . '\\Policies\\' . class_basename($class) . 'Policy']); + }) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']); }); } @@ -115,11 +115,11 @@ protected function handleFactories(): void $appNamespace = static::appNamespace(); - $modelName = Str::startsWith($modelName, $appNamespace . 'Models\\') - ? Str::after($modelName, $appNamespace . 'Models\\') + $modelName = Str::startsWith($modelName, $appNamespace.'Models\\') + ? Str::after($modelName, $appNamespace.'Models\\') : Str::after($modelName, $appNamespace); - return 'Database\\Factories\\' . $modelName . 'Factory'; + return 'Database\\Factories\\'.$modelName.'Factory'; }); } From 01501c7a1ea2af7a2999702eba2b39b41c891cc6 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 19:28:05 -0400 Subject: [PATCH 06/14] Update config and readme. --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++---------- config/ddd.php | 20 ++++++++++++++++ 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c7e19d2..c170495 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,27 @@ When `ddd.autoload.factories` is enabled, the package will register a custom fac If your application implements its own factory discovery using `Factory::guessFactoryNamesUsing()`, you should set `ddd.autoload.factories` to `false` to ensure it is not overridden. +### Ignoring Folders During Autoloading +To specify folders that should be skipped during autoloading discovery, add them to the `ddd.autoload_ignore` configuration option. By default, the `Tests` and `Migrations` folders are ignored. +```php +'autoload_ignore' => [ + 'Tests', + 'Migrations', +], +``` + +Folders specified here are relative to the root of each domain. e.g., src/Domain/Invoicing/. If more advanced filtering is needed, a callback can be registered using the `DDD::filterAutoloadPathsUsing(callback $filter)` in your AppServiceProvider's boot method: +```php +use Lunarstorm\LaravelDDD\Facades\DDD; +use Symfony\Component\Finder\SplFileInfo; + +DDD::filterAutoloadPathsUsing(function (SplFileInfo $file) { + if (basename($file->getRelativePathname()) === 'functions.php') { + return false; + } +}); +``` + ### Disabling Autoloading You may disable autoloading by setting the respective autoload options to `false` in the configuration file as needed, or by commenting out the autoload configuration entirely. ```php @@ -221,11 +242,14 @@ You may disable autoloading by setting the respective autoload options to `false // 'factories' => true, // ], ``` + + ## Autoloading in Production In production, you should cache the autoload manifests using the `ddd:cache` command as part of your application's deployment process. This will speed up the auto-discovery and registration of domain providers and commands. The `ddd:clear` command may be used to clear the cache if needed. + ## Configuration File This is the content of the published config file (`ddd.php`): @@ -356,34 +380,50 @@ return [ */ 'autoload' => [ /** - * When enabled, any class in the domain layer extending - * `Illuminate\Support\ServiceProvider` will be - * auto-registered as a service provider + * When enabled, any class within the domain layer extending `Illuminate\Support\ServiceProvider` + * will be auto-registered as a service provider */ 'providers' => true, /** - * When enabled, any class in the domain layer extending - * `Illuminate\Console\Command` will be auto-registered - * as a command when running in console. + * When enabled, any class within the domain layer extending `Illuminate\Console\Command` + * will be auto-registered as a command when running in console. */ 'commands' => true, /** - * When enabled, a custom policy discovery callback will be - * registered to resolve policy names for domain models, - * or fallback to Laravel's default otherwise. + * When enabled, the package will register a custom policy discovery callback to resolve policy names + * for domain models, and fallback to Laravel's default for all other cases. */ 'policies' => true, /** - * When enabled, a custom policy discovery callback will be - * registered to resolve factory names for domain models, - * or fallback to Laravel's default otherwise. + * When enabled, the package will register a custom factory discovery callback to resolve factory names + * for domain models, and fallback to Laravel's default for all other cases. */ 'factories' => true, ], + /* + |-------------------------------------------------------------------------- + | Autoload Ignore Folders + |-------------------------------------------------------------------------- + | + | Folders that should be skipped during autoloading discovery, + | relative to the root of each domain. + | + | e.g., src/Domain/Invoicing/ + | + | If more advanced filtering is needed, a callback can be registered + | using the `DDD::filterAutoloadPathsUsing(callback $filter)` in + | the AppServiceProvider's boot method. + | + */ + 'autoload_ignore' => [ + 'Tests', + 'Migrations', + ], + /* |-------------------------------------------------------------------------- | Caching diff --git a/config/ddd.php b/config/ddd.php index ae7acb4..dc295fd 100644 --- a/config/ddd.php +++ b/config/ddd.php @@ -150,6 +150,26 @@ 'factories' => true, ], + /* + |-------------------------------------------------------------------------- + | Autoload Ignore Folders + |-------------------------------------------------------------------------- + | + | Folders that should be skipped during autoloading discovery, + | relative to the root of each domain. + | + | e.g., src/Domain/Invoicing/ + | + | If more advanced filtering is needed, a callback can be registered + | using the `DDD::filterAutoloadPathsUsing(callback $filter)` in + | the AppServiceProvider's boot method. + | + */ + 'autoload_ignore' => [ + 'Tests', + 'Migrations', + ], + /* |-------------------------------------------------------------------------- | Caching From dbcb94a193205d9f73cc909eee3bba8b19d92ad5 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 19:33:48 -0400 Subject: [PATCH 07/14] Change wording. --- CHANGELOG.md | 3 +++ README.md | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index deac72f..56fe0cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to `laravel-ddd` will be documented in this file. ## [Unversioned] +### Added +- Ability to ignore folders during autoloading via config(`ddd.autoload.ignore`), or register a custom filter callback via `DDD::filterAutoloadPathsUsing(callable $filter)`. + ### Changed - Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results no matter what order runs in production (before or after `cache:clear` or `optimize:clear` commands). diff --git a/README.md b/README.md index c170495..a386fc1 100644 --- a/README.md +++ b/README.md @@ -211,16 +211,16 @@ When `ddd.autoload.factories` is enabled, the package will register a custom fac If your application implements its own factory discovery using `Factory::guessFactoryNamesUsing()`, you should set `ddd.autoload.factories` to `false` to ensure it is not overridden. -### Ignoring Folders During Autoloading -To specify folders that should be skipped during autoloading discovery, add them to the `ddd.autoload_ignore` configuration option. By default, the `Tests` and `Migrations` folders are ignored. +### Ignoring Paths During Autoloading +To specify folders or paths that should be skipped during autoloading discovery, add them to the `ddd.autoload_ignore` configuration option. By default, the `Tests` and `Migrations` folders are ignored. ```php 'autoload_ignore' => [ 'Tests', - 'Migrations', + 'Database/Migrations', ], ``` -Folders specified here are relative to the root of each domain. e.g., src/Domain/Invoicing/. If more advanced filtering is needed, a callback can be registered using the `DDD::filterAutoloadPathsUsing(callback $filter)` in your AppServiceProvider's boot method: +Paths specified here are relative to the root of each domain. e.g., `src/Domain/Invoicing/{path-to-ignore}`. If more advanced filtering is needed, a callback can be registered using the `DDD::filterAutoloadPathsUsing(callback $filter)` in your AppServiceProvider's boot method: ```php use Lunarstorm\LaravelDDD\Facades\DDD; use Symfony\Component\Finder\SplFileInfo; From 8a65cf6fbf1310c89df9577dbb44708b2a0e15e2 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 20:24:46 -0400 Subject: [PATCH 08/14] Corrected autoload_ignore config key. --- CHANGELOG.md | 2 +- src/Support/DomainAutoloader.php | 14 +++++++------- tests/Autoload/IgnoreTest.php | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56fe0cb..2d64fae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to `laravel-ddd` will be documented in this file. ## [Unversioned] ### Added -- Ability to ignore folders during autoloading via config(`ddd.autoload.ignore`), or register a custom filter callback via `DDD::filterAutoloadPathsUsing(callable $filter)`. +- Ability to ignore folders during autoloading via config(`ddd.autoload_ignore`), or register a custom filter callback via `DDD::filterAutoloadPathsUsing(callable $filter)`. ### Changed - Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results no matter what order runs in production (before or after `cache:clear` or `optimize:clear` commands). diff --git a/src/Support/DomainAutoloader.php b/src/Support/DomainAutoloader.php index 025f328..3174afa 100644 --- a/src/Support/DomainAutoloader.php +++ b/src/Support/DomainAutoloader.php @@ -28,7 +28,7 @@ public function __construct() public function autoload(): void { - if (! config()->has('ddd.autoload')) { + if (!config()->has('ddd.autoload')) { return; } @@ -99,10 +99,10 @@ protected function handlePolicies(): void return Arr::wrap(Collection::times(count($classDirnameSegments), function ($index) use ($class, $classDirnameSegments) { $classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index)); - return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; + return $classDirname . '\\Policies\\' . class_basename($class) . 'Policy'; })->reverse()->values()->first(function ($class) { return class_exists($class); - }) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']); + }) ?: [$classDirname . '\\Policies\\' . class_basename($class) . 'Policy']); }); } @@ -115,11 +115,11 @@ protected function handleFactories(): void $appNamespace = static::appNamespace(); - $modelName = Str::startsWith($modelName, $appNamespace.'Models\\') - ? Str::after($modelName, $appNamespace.'Models\\') + $modelName = Str::startsWith($modelName, $appNamespace . 'Models\\') + ? Str::after($modelName, $appNamespace . 'Models\\') : Str::after($modelName, $appNamespace); - return 'Database\\Factories\\'.$modelName.'Factory'; + return 'Database\\Factories\\' . $modelName . 'Factory'; }); } @@ -131,7 +131,7 @@ protected static function finder($paths) ->after('/') ->finish('/'); - $ignoredFolders = collect(config('ddd.autoload.ignore', [])) + $ignoredFolders = collect(config('ddd.autoload_ignore', [])) ->map(fn ($path) => Str::finish($path, '/')); if ($pathAfterDomain->startsWith($ignoredFolders)) { diff --git a/tests/Autoload/IgnoreTest.php b/tests/Autoload/IgnoreTest.php index 364d75b..0e94256 100644 --- a/tests/Autoload/IgnoreTest.php +++ b/tests/Autoload/IgnoreTest.php @@ -28,7 +28,7 @@ expect($cached)->toEqual($expected); - Config::set('ddd.autoload.ignore', ['Commands']); + Config::set('ddd.autoload_ignore', ['Commands']); Artisan::call('ddd:cache'); @@ -43,7 +43,7 @@ expect($cached)->toEqual($expected); - Config::set('ddd.autoload.ignore', ['Providers']); + Config::set('ddd.autoload_ignore', ['Providers']); Artisan::call('ddd:cache'); From ba65fe6aaa1ecdade99067a0d33b02d8e37bc9b9 Mon Sep 17 00:00:00 2001 From: JasperTey Date: Tue, 16 Apr 2024 00:25:13 +0000 Subject: [PATCH 09/14] Fix styling --- src/Support/DomainAutoloader.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Support/DomainAutoloader.php b/src/Support/DomainAutoloader.php index 3174afa..f107dc0 100644 --- a/src/Support/DomainAutoloader.php +++ b/src/Support/DomainAutoloader.php @@ -28,7 +28,7 @@ public function __construct() public function autoload(): void { - if (!config()->has('ddd.autoload')) { + if (! config()->has('ddd.autoload')) { return; } @@ -99,10 +99,10 @@ protected function handlePolicies(): void return Arr::wrap(Collection::times(count($classDirnameSegments), function ($index) use ($class, $classDirnameSegments) { $classDirname = implode('\\', array_slice($classDirnameSegments, 0, $index)); - return $classDirname . '\\Policies\\' . class_basename($class) . 'Policy'; + return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; })->reverse()->values()->first(function ($class) { return class_exists($class); - }) ?: [$classDirname . '\\Policies\\' . class_basename($class) . 'Policy']); + }) ?: [$classDirname.'\\Policies\\'.class_basename($class).'Policy']); }); } @@ -115,11 +115,11 @@ protected function handleFactories(): void $appNamespace = static::appNamespace(); - $modelName = Str::startsWith($modelName, $appNamespace . 'Models\\') - ? Str::after($modelName, $appNamespace . 'Models\\') + $modelName = Str::startsWith($modelName, $appNamespace.'Models\\') + ? Str::after($modelName, $appNamespace.'Models\\') : Str::after($modelName, $appNamespace); - return 'Database\\Factories\\' . $modelName . 'Factory'; + return 'Database\\Factories\\'.$modelName.'Factory'; }); } From b6fca4844adcb924edce2c9fb17c4237edc59b90 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 20:46:39 -0400 Subject: [PATCH 10/14] Update default migrations ignore folder. --- config/ddd.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/ddd.php b/config/ddd.php index dc295fd..b9bfc5b 100644 --- a/config/ddd.php +++ b/config/ddd.php @@ -167,7 +167,7 @@ */ 'autoload_ignore' => [ 'Tests', - 'Migrations', + 'Database/Migrations', ], /* From c4f65d98610bcc5e5b821e5b5b9d4b43e3d5e10b Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 20:55:18 -0400 Subject: [PATCH 11/14] Typo. --- README.md | 2 +- config/ddd.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a386fc1..e93ee24 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,7 @@ To specify folders or paths that should be skipped during autoloading discovery, ], ``` -Paths specified here are relative to the root of each domain. e.g., `src/Domain/Invoicing/{path-to-ignore}`. If more advanced filtering is needed, a callback can be registered using the `DDD::filterAutoloadPathsUsing(callback $filter)` in your AppServiceProvider's boot method: +Paths specified here are relative to the root of each domain. e.g., `src/Domain/Invoicing/{path-to-ignore}`. If more advanced filtering is needed, a callback can be registered using `DDD::filterAutoloadPathsUsing(callback $filter)` in your AppServiceProvider's boot method: ```php use Lunarstorm\LaravelDDD\Facades\DDD; use Symfony\Component\Finder\SplFileInfo; diff --git a/config/ddd.php b/config/ddd.php index b9bfc5b..66d8901 100644 --- a/config/ddd.php +++ b/config/ddd.php @@ -161,7 +161,7 @@ | e.g., src/Domain/Invoicing/ | | If more advanced filtering is needed, a callback can be registered - | using the `DDD::filterAutoloadPathsUsing(callback $filter)` in + | using `DDD::filterAutoloadPathsUsing(callback $filter)` in | the AppServiceProvider's boot method. | */ From 331c289147045cae60dcd41b7405a60bcfe8952a Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Mon, 15 Apr 2024 21:27:50 -0400 Subject: [PATCH 12/14] Another typo. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d64fae..b49a40b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to `laravel-ddd` will be documented in this file. - Ability to ignore folders during autoloading via config(`ddd.autoload_ignore`), or register a custom filter callback via `DDD::filterAutoloadPathsUsing(callable $filter)`. ### Changed -- Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results no matter what order runs in production (before or after `cache:clear` or `optimize:clear` commands). +- Internal: Domain cache is no longer quietly cleared on laravel's `cache:clearing` event, so that `ddd:cache` yields consistent results no matter which order it runs in production (before or after `cache:clear` or `optimize:clear` commands). ## [1.1.0] - 2024-04-07 ### Added From d14017bc9385b09fc53523386e8d7cbee35eb3f9 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Tue, 16 Apr 2024 08:29:26 -0400 Subject: [PATCH 13/14] Ensure config sample in readme is up-to-date. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e93ee24..13a2f1b 100644 --- a/README.md +++ b/README.md @@ -421,7 +421,7 @@ return [ */ 'autoload_ignore' => [ 'Tests', - 'Migrations', + 'Database/Migrations', ], /* From 21a381c6d7ca26c0e97dbe9bbccf43ffa84721c3 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Tue, 16 Apr 2024 08:30:19 -0400 Subject: [PATCH 14/14] Add new autoload_ignore key to upgrade stub. --- config/ddd.php.stub | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/config/ddd.php.stub b/config/ddd.php.stub index cd6a2bb..6c10645 100644 --- a/config/ddd.php.stub +++ b/config/ddd.php.stub @@ -150,6 +150,26 @@ return [ 'factories' => true, ], + /* + |-------------------------------------------------------------------------- + | Autoload Ignore Folders + |-------------------------------------------------------------------------- + | + | Folders that should be skipped during autoloading discovery, + | relative to the root of each domain. + | + | e.g., src/Domain/Invoicing/ + | + | If more advanced filtering is needed, a callback can be registered + | using `DDD::filterAutoloadPathsUsing(callback $filter)` in + | the AppServiceProvider's boot method. + | + */ + 'autoload_ignore' => [ + 'Tests', + 'Database/Migrations', + ], + /* |-------------------------------------------------------------------------- | Caching