From f38f9601155b1f137ba349607b8627b2f6014f67 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Thu, 14 Nov 2024 17:50:57 -0500 Subject: [PATCH] Prevent autoloader from booting more than once. --- src/Support/Autoloader.php | 21 ++++++++------------- tests/Autoload/CommandTest.php | 7 ++++++- tests/Autoload/FactoryTest.php | 22 +++++++++++++--------- tests/Autoload/ProviderTest.php | 4 ++-- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/Support/Autoloader.php b/src/Support/Autoloader.php index c874e93..84d3935 100644 --- a/src/Support/Autoloader.php +++ b/src/Support/Autoloader.php @@ -30,6 +30,8 @@ class Autoloader protected array $discoveredProviders = []; + protected bool $isBooted = false; + public function __construct() { $this->appNamespace = $this->resolveAppNamespace(); @@ -37,17 +39,13 @@ public function __construct() public function boot(): void { - if (! config()->has('ddd.autoload')) { + if ($this->isBooted) { return; } - // if ($discoveredCommands = $this->getDiscoveredCommands()) { - // dump('Commands were already discovered', $discoveredCommands); - // } - - // if ($discoveredProviders = $this->getDiscoveredProviders()) { - // dump('Providers were already discovered', $discoveredProviders); - // } + if (! config()->has('ddd.autoload')) { + return; + } $this ->handleProviders() @@ -55,6 +53,8 @@ public function boot(): void ->when(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()); + + $this->isBooted = true; } protected function normalizePaths($path): array @@ -251,11 +251,6 @@ public function cacheCommands() return $this; } - protected function flush() - { - return $this; - } - protected function resolveAppNamespace() { try { diff --git a/tests/Autoload/CommandTest.php b/tests/Autoload/CommandTest.php index d62e31b..55dc36b 100644 --- a/tests/Autoload/CommandTest.php +++ b/tests/Autoload/CommandTest.php @@ -9,6 +9,7 @@ beforeEach(function () { $this->setupTestApplication(); + DomainCache::clear(); }); afterEach(function () { @@ -17,6 +18,10 @@ describe('without autoload', function () { it('does not register the command', function ($className, $command) { + $this->afterApplicationRefreshed(function () { + app('ddd.autoloader')->boot(); + }); + $this->refreshApplicationWithConfig([ 'ddd.autoload.commands' => false, ]); @@ -32,7 +37,7 @@ describe('with autoload', function () { it('registers existing commands', function ($className, $command, $output) { - $this->afterApplicationCreated(function () { + $this->afterApplicationRefreshed(function () { app('ddd.autoloader')->boot(); }); diff --git a/tests/Autoload/FactoryTest.php b/tests/Autoload/FactoryTest.php index 79a4a4e..6b6543c 100644 --- a/tests/Autoload/FactoryTest.php +++ b/tests/Autoload/FactoryTest.php @@ -1,21 +1,23 @@ setupTestApplication(); - - Config::set('ddd.domain_namespace', 'Domain'); }); describe('autoload enabled', function () { beforeEach(function () { - Config::set('ddd.autoload.factories', true); - - $this->afterApplicationCreated(function () { + $this->afterApplicationRefreshed(function () { app('ddd.autoloader')->boot(); }); + + $this->refreshApplicationWithConfig([ + 'ddd.autoload.factories' => true, + ]); }); it('can resolve domain factory', function ($modelClass, $expectedFactoryClass) { @@ -48,11 +50,13 @@ describe('autoload disabled', function () { beforeEach(function () { - Config::set('ddd.autoload.factories', false); - - $this->afterApplicationCreated(function () { + $this->afterApplicationRefreshed(function () { app('ddd.autoloader')->boot(); }); + + $this->refreshApplicationWithConfig([ + 'ddd.autoload.factories' => false, + ]); }); it('cannot resolve factories that rely on autoloading', function ($modelClass) { diff --git a/tests/Autoload/ProviderTest.php b/tests/Autoload/ProviderTest.php index 57bcacd..6b18e18 100644 --- a/tests/Autoload/ProviderTest.php +++ b/tests/Autoload/ProviderTest.php @@ -60,7 +60,7 @@ describe('with autoload', function () { it('registers the provider in domain layer', function () { - $this->afterApplicationCreated(function () { + $this->afterApplicationRefreshed(function () { app('ddd.autoloader')->boot(); }); @@ -73,7 +73,7 @@ }); it('registers the provider in application layer', function () { - $this->afterApplicationCreated(function () { + $this->afterApplicationRefreshed(function () { app('ddd.autoloader')->boot(); });