Skip to content

Commit

Permalink
Prevent autoloader from booting more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 14, 2024
1 parent b0337f5 commit f38f960
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
21 changes: 8 additions & 13 deletions src/Support/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ class Autoloader

protected array $discoveredProviders = [];

protected bool $isBooted = false;

public function __construct()
{
$this->appNamespace = $this->resolveAppNamespace();
}

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()
->when(app()->runningInConsole(), fn ($autoloader) => $autoloader->handleProviders())
->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
Expand Down Expand Up @@ -251,11 +251,6 @@ public function cacheCommands()
return $this;
}

protected function flush()
{
return $this;
}

protected function resolveAppNamespace()
{
try {
Expand Down
7 changes: 6 additions & 1 deletion tests/Autoload/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

beforeEach(function () {
$this->setupTestApplication();
DomainCache::clear();
});

afterEach(function () {
Expand All @@ -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,
]);
Expand All @@ -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();
});

Expand Down
22 changes: 13 additions & 9 deletions tests/Autoload/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Lunarstorm\LaravelDDD\Tests\BootsTestApplication;

uses(BootsTestApplication::class);

beforeEach(function () {
$this->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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Autoload/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -73,7 +73,7 @@
});

it('registers the provider in application layer', function () {
$this->afterApplicationCreated(function () {
$this->afterApplicationRefreshed(function () {
app('ddd.autoloader')->boot();
});

Expand Down

0 comments on commit f38f960

Please sign in to comment.