Skip to content

Commit

Permalink
Make autoload resolve arrays static.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspertey committed Nov 16, 2024
1 parent 5b58587 commit 121ab0f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 37 deletions.
52 changes: 26 additions & 26 deletions src/Support/AutoloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class AutoloadManager

protected string $appNamespace;

protected array $registeredCommands = [];
protected static array $registeredCommands = [];

protected array $registeredProviders = [];
protected static array $registeredProviders = [];

protected array $resolvedPolicies = [];
protected static array $resolvedPolicies = [];

protected array $resolvedFactories = [];
protected static array $resolvedFactories = [];

protected bool $booted = false;

Expand Down Expand Up @@ -72,17 +72,17 @@ public function isConsoleBooted(): bool

protected function flush()
{
foreach ($this->registeredProviders as $provider) {
foreach (static::$registeredProviders as $provider) {
app()->forgetInstance($provider);
}

$this->registeredProviders = [];
static::$registeredProviders = [];

$this->registeredCommands = [];
static::$registeredCommands = [];

$this->resolvedPolicies = [];
static::$resolvedPolicies = [];

$this->resolvedFactories = [];
static::$resolvedFactories = [];

return $this;
}
Expand Down Expand Up @@ -116,14 +116,14 @@ protected function handleProviders()
? DomainCache::get('domain-providers')
: $this->discoverProviders();

foreach ($this->registeredProviders as $provider) {
foreach (static::$registeredProviders as $provider) {
app()->forgetInstance($provider);
}

$this->registeredProviders = [];
static::$registeredProviders = [];

foreach ($providers as $provider) {
$this->registeredProviders[$provider] = $provider;
static::$registeredProviders[$provider] = $provider;
app()->register($provider);
}

Expand All @@ -136,24 +136,24 @@ protected function handleCommands()
? DomainCache::get('domain-commands')
: $this->discoverCommands();

$this->registeredCommands = [];
static::$registeredCommands = [];

foreach ($commands as $command) {
$this->registeredCommands[$command] = $command;
static::$registeredCommands[$command] = $command;
}

return $this;
}

protected function run()
{
foreach ($this->registeredProviders as $provider) {
foreach (static::$registeredProviders as $provider) {
app()->register($provider);
}

if (app()->runningInConsole() && ! $this->isConsoleBooted()) {
ConsoleApplication::starting(function (ConsoleApplication $artisan) {
foreach ($this->registeredCommands as $command) {
foreach (static::$registeredCommands as $command) {
$artisan->resolve($command);
}
});
Expand All @@ -166,37 +166,37 @@ protected function run()

public function getRegisteredCommands(): array
{
return $this->registeredCommands;
return static::$registeredCommands;
}

public function getRegisteredProviders(): array
{
return $this->registeredProviders;
return static::$registeredProviders;
}

public function getResolvedPolicies(): array
{
return $this->resolvedPolicies;
return static::$resolvedPolicies;
}

public function getResolvedFactories(): array
{
return $this->resolvedFactories;
return static::$resolvedFactories;
}

protected function handlePolicies()
{
Gate::guessPolicyNamesUsing(function (string $class): array|string {
if (array_key_exists($class, $this->resolvedPolicies)) {
return $this->resolvedPolicies[$class];
if (array_key_exists($class, static::$resolvedPolicies)) {
return static::$resolvedPolicies[$class];
}

if ($model = DomainObject::fromClass($class, 'model')) {
$resolved = (new Domain($model->domain))
->object('policy', "{$model->name}Policy")
->fullyQualifiedName;

$this->resolvedPolicies[$class] = $resolved;
static::$resolvedPolicies[$class] = $resolved;

return $resolved;
}
Expand All @@ -220,12 +220,12 @@ protected function handlePolicies()
protected function handleFactories()
{
Factory::guessFactoryNamesUsing(function (string $modelName) {
if (array_key_exists($modelName, $this->resolvedFactories)) {
return $this->resolvedFactories[$modelName];
if (array_key_exists($modelName, static::$resolvedFactories)) {
return static::$resolvedFactories[$modelName];
}

if ($factoryName = DomainFactory::resolveFactoryName($modelName)) {
$this->resolvedFactories[$modelName] = $factoryName;
static::$resolvedFactories[$modelName] = $factoryName;

return $factoryName;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Autoload/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
$artisanCommands = collect(Artisan::all());

expect($artisanCommands)->not->toHaveKeys(array_keys($this->commands));
})->only();
});

it('does not register the commands', function () {
config()->set('ddd.autoload.commands', false);
Expand Down
10 changes: 0 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lunarstorm\LaravelDDD\Tests;

use Illuminate\Console\Application as ConsoleApplication;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -229,15 +228,6 @@ protected function setupTestApplication()
$this->setAutoloadPathInComposer('Application', 'src/Application');
$this->setAutoloadPathInComposer('Infrastructure', 'src/Infrastructure');

$this->resetArtisan();

return $this;
}

protected function resetArtisan()
{
ConsoleApplication::forgetBootstrappers();

return $this;
}

Expand Down

0 comments on commit 121ab0f

Please sign in to comment.