diff --git a/src/Support/Autoloader.php b/src/Support/Autoloader.php index a1ffa4c..479adf5 100644 --- a/src/Support/Autoloader.php +++ b/src/Support/Autoloader.php @@ -26,9 +26,9 @@ class Autoloader protected string $appNamespace; - protected array $discoveredCommands = []; + protected array $registeredCommands = []; - protected array $discoveredProviders = []; + protected array $registeredProviders = []; protected bool $isBooted = false; @@ -37,26 +37,37 @@ public function __construct() $this->appNamespace = $this->resolveAppNamespace(); } - public function boot(): void + public function boot() { - // if ($this->isBooted) { - // // dump('Autoloader Already booted'); - // return; - // } - if (! config()->has('ddd.autoload')) { - return; + return $this; } + // if ($this->isBooted) { + // return $this; + // } + $this - ->when(config('ddd.autoload.providers') === true, fn ($autoloader) => $autoloader->handleProviders()) - ->when(app()->runningInConsole() && config('ddd.autoload.commands') === true, fn ($autoloader) => $autoloader->handleCommands()) - ->when(config('ddd.autoload.policies') === true, fn ($autoloader) => $autoloader->handlePolicies()) - ->when(config('ddd.autoload.factories') === true, fn ($autoloader) => $autoloader->handleFactories()); + ->when(config('ddd.autoload.providers') === true, fn () => $this->handleProviders()) + ->when(app()->runningInConsole() && config('ddd.autoload.commands') === true, fn () => $this->handleCommands()) + ->when(config('ddd.autoload.policies') === true, fn () => $this->handlePolicies()) + ->when(config('ddd.autoload.factories') === true, fn () => $this->handleFactories()); + + if (app()->runningInConsole()) { + ConsoleApplication::starting(function ($artisan) { + foreach ($this->registeredCommands as $command) { + $artisan->resolve($command); + } + }); + } $this->isBooted = true; + + return $this; } + public function run() {} + protected function normalizePaths($path): array { return collect($path) @@ -86,8 +97,10 @@ protected function handleProviders() ? DomainCache::get('domain-providers') : $this->discoverProviders(); + $this->registeredProviders = []; + foreach ($providers as $provider) { - $this->discoveredProviders[$provider] = $provider; + $this->registeredProviders[$provider] = $provider; app()->register($provider); } @@ -100,29 +113,32 @@ protected function handleCommands() ? DomainCache::get('domain-commands') : $this->discoverCommands(); + $this->registeredCommands = []; + foreach ($commands as $command) { - $this->discoveredCommands[$command] = $command; + $this->registeredCommands[$command] = $command; $this->registerCommand($command); } return $this; } - public function getDiscoveredCommands(): array + public function getRegisteredCommands(): array { - return $this->discoveredCommands; + return $this->registeredCommands; } - public function getDiscoveredProviders(): array + public function getRegisteredProviders(): array { - return $this->discoveredProviders; + return $this->registeredProviders; } protected function registerCommand($class) { - ConsoleApplication::starting(function ($artisan) use ($class) { - $artisan->resolve($class); - }); + // ConsoleApplication::starting(function ($artisan) use ($class) { + // dump('resolving command', $class, $this->registeredCommands); + // $artisan->resolve($class); + // }); } protected function handlePolicies() @@ -222,8 +238,8 @@ protected function discoverCommands(): array } $paths = $this->normalizePaths( - $configValue === true ? - $this->getAllLayerPaths() + $configValue === true + ? $this->getAllLayerPaths() : $configValue ); diff --git a/tests/Autoload/CommandTest.php b/tests/Autoload/CommandTest.php index 4dc9563..e446820 100644 --- a/tests/Autoload/CommandTest.php +++ b/tests/Autoload/CommandTest.php @@ -7,21 +7,21 @@ uses(BootsTestApplication::class); -beforeEach(function () { - $this->setupTestApplication(); - DomainCache::clear(); -}); - afterEach(function () { DomainCache::clear(); }); describe('without autoload', function () { - it('does not register the command', function ($className, $command) { + beforeEach(function () { + DomainCache::clear(); + $this->afterApplicationRefreshed(function () { + $this->setupTestApplication(); app('ddd.autoloader')->boot(); }); + }); + it('does not register the command', function ($className, $command) { $this->refreshApplicationWithConfig([ 'ddd.autoload.commands' => false, ]); @@ -33,14 +33,19 @@ ['Infrastructure\Commands\LogPrune', 'log:prune'], ['Application\Commands\ApplicationSync', 'application:sync'], ]); -})->skip(); +}); describe('with autoload', function () { - it('registers existing commands', function ($className, $command, $output) { + beforeEach(function () { + DomainCache::clear(); + $this->afterApplicationRefreshed(function () { + $this->setupTestApplication(); app('ddd.autoloader')->boot(); }); + }); + it('registers existing commands', function ($className, $command, $output) { $this->refreshApplicationWithConfig([ 'ddd.autoload.commands' => true, ]); @@ -58,13 +63,13 @@ ['Infrastructure\Commands\LogPrune', 'log:prune', 'System logs pruned!'], ['Application\Commands\ApplicationSync', 'application:sync', 'Application state synced!'], ]); -})->skip(); +}); describe('caching', function () { it('remembers the last cached state', function () { - DomainCache::set('domain-commands', []); - $this->afterApplicationRefreshed(function () { + $this->setupTestApplication(); + DomainCache::set('domain-commands', []); app('ddd.autoloader')->boot(); }); @@ -79,10 +84,10 @@ }); it('can bust the cache', function () { - DomainCache::set('domain-commands', []); - DomainCache::clear(); - $this->afterApplicationRefreshed(function () { + $this->setupTestApplication(); + DomainCache::set('domain-commands', []); + DomainCache::clear(); app('ddd.autoloader')->boot(); }); @@ -94,4 +99,4 @@ $this->artisan('log:prune')->assertSuccessful(); $this->artisan('application:sync')->assertSuccessful(); }); -})->skip(); +}); diff --git a/tests/Autoload/ProviderTest.php b/tests/Autoload/ProviderTest.php index 982bc3d..01d76a7 100644 --- a/tests/Autoload/ProviderTest.php +++ b/tests/Autoload/ProviderTest.php @@ -1,6 +1,5 @@ false, ]); - expect(DDD::autoloader()->getDiscoveredProviders())->toBeEmpty(); - expect(fn () => app($binding))->toThrow(Exception::class); })->with([ ['invoicing'], diff --git a/tests/Command/OptimizeTest.php b/tests/Command/OptimizeTest.php index 11932c8..510c311 100644 --- a/tests/Command/OptimizeTest.php +++ b/tests/Command/OptimizeTest.php @@ -46,7 +46,7 @@ }); it('can clear the cache', function () { - Artisan::call('ddd:cache'); + Artisan::call('ddd:optimize'); expect(DomainCache::get('domain-providers'))->not->toBeNull(); expect(DomainCache::get('domain-commands'))->not->toBeNull(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 24a2242..da96a0c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -92,9 +92,11 @@ protected function refreshApplicationWithConfig(array $config) { $this->appConfig = $config; - $this->refreshApplication(); + // $this->afterApplicationRefreshed(fn () => $this->appConfig = []); - $this->afterApplicationRefreshed(fn () => $this->appConfig = []); + $this->reloadApplication(); + + $this->appConfig = []; return $this; }