Skip to content

Commit

Permalink
Autoload ignore & custom filter callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Apr 15, 2024
1 parent 1b80095 commit 932f656
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/DomainManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Lunarstorm\LaravelDDD;

class DomainManager
{
/**
* The autoload path filter callback.
*
* @var callable|null
*/
protected $autoloadFilter;

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

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

public function getAutoloadFilter(): ?callable
{
return $this->autoloadFilter;
}
}
18 changes: 18 additions & 0 deletions src/Facades/DDD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Lunarstorm\LaravelDDD\Facades;

use Illuminate\Support\Facades\Facade;

/**
* @see \Lunarstorm\LaravelDDD\DomainManager
*
* @method static void filterAutoloadPathsUsing(callable $filter)
*/
class DDD extends Facade
{
protected static function getFacadeAccessor()
{
return \Lunarstorm\LaravelDDD\DomainManager::class;
}
}
16 changes: 0 additions & 16 deletions src/Facades/LaravelDDD.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/LaravelDDD.php

This file was deleted.

6 changes: 6 additions & 0 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class LaravelDDDServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$this->app->scoped(DomainManager::class, function () {
return new DomainManager();
});

$this->app->bind('ddd', DomainManager::class);

/*
* This class is a Package Service Provider
*
Expand Down
24 changes: 22 additions & 2 deletions src/Support/DomainAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand All @@ -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();
Expand Down
102 changes: 102 additions & 0 deletions tests/Autoload/IgnoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Lunarstorm\LaravelDDD\Facades\DDD;
use Lunarstorm\LaravelDDD\Support\DomainCache;
use Symfony\Component\Finder\SplFileInfo;

beforeEach(function () {
Config::set('ddd.domain_path', 'src/Domain');
Config::set('ddd.domain_namespace', 'Domain');
$this->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');
});
2 changes: 2 additions & 0 deletions tests/Command/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 932f656

Please sign in to comment.