Skip to content

Commit

Permalink
Require app instance
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperTey committed Nov 16, 2024
1 parent b8fa04c commit c6c08a4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 42 deletions.
54 changes: 26 additions & 28 deletions src/LaravelDDDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,6 @@ class LaravelDDDServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$this->app->scoped(DomainManager::class, function () {
return new DomainManager;
});

$this->app->scoped(AutoloadManager::class, function () {
return new AutoloadManager;
});

$this->app->scoped(ComposerManager::class, function () {
return ComposerManager::make(app()->basePath('composer.json'));
});

$this->app->scoped(ConfigManager::class, function () {
return new ConfigManager(config_path('ddd.php'));
});

$this->app->scoped(StubManager::class, function () {
return new StubManager;
});

$this->app->bind('ddd', DomainManager::class);
$this->app->bind('ddd.autoloader', AutoloadManager::class);
$this->app->bind('ddd.config', ConfigManager::class);
$this->app->bind('ddd.composer', ComposerManager::class);
$this->app->bind('ddd.stubs', StubManager::class);

/*
* This class is a Package Service Provider
*
Expand Down Expand Up @@ -119,8 +93,6 @@ protected function registerMigrations()

public function packageBooted()
{
Autoload::boot();

Autoload::run();

if ($this->app->runningInConsole() && method_exists($this, 'optimizes')) {
Expand All @@ -134,6 +106,32 @@ public function packageBooted()

public function packageRegistered()
{
$this->app->scoped(DomainManager::class, function () {
return new DomainManager;
});

$this->app->scoped(AutoloadManager::class, function ($app) {
return new AutoloadManager($app);
});

$this->app->scoped(ComposerManager::class, function () {
return ComposerManager::make(app()->basePath('composer.json'));
});

$this->app->scoped(ConfigManager::class, function () {
return new ConfigManager(config_path('ddd.php'));
});

$this->app->scoped(StubManager::class, function () {
return new StubManager;
});

$this->app->bind('ddd', DomainManager::class);
$this->app->bind('ddd.autoloader', AutoloadManager::class);
$this->app->bind('ddd.config', ConfigManager::class);
$this->app->bind('ddd.composer', ComposerManager::class);
$this->app->bind('ddd.stubs', StubManager::class);

$this->registerMigrations();
}
}
39 changes: 29 additions & 10 deletions src/Support/AutoloadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class AutoloadManager
{
use Conditionable;

protected $app;

protected string $appNamespace;

protected static array $registeredCommands = [];
Expand All @@ -40,11 +42,24 @@ class AutoloadManager

protected bool $ran = false;

public function __construct()
/**
* Create a new autoloader instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function __construct($app)
{
$this->appNamespace = $this->resolveAppNamespace();
$this->app = $app;

$this->appNamespace = $this->app->getNamespace();
}

// public function __construct(protected \Illuminate\Contracts\Foundation\Application $app)
// {
// $this->appNamespace = $this->resolveAppNamespace();
// }

public function boot()
{
if (! config()->has('ddd.autoload')) {
Expand All @@ -54,7 +69,7 @@ public function boot()
$this
->flush()
->when(config('ddd.autoload.providers') === true, fn () => $this->handleProviders())
->when(app()->runningInConsole() && config('ddd.autoload.commands') === true, fn () => $this->handleCommands())
->when($this->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());

Expand All @@ -79,7 +94,7 @@ public function hasRun(): bool
protected function flush()
{
foreach (static::$registeredProviders as $provider) {
app()->forgetInstance($provider);
$this->app?->forgetInstance($provider);
}

static::$registeredProviders = [];
Expand All @@ -106,14 +121,14 @@ public function getAllLayerPaths(): array
DomainResolver::domainPath(),
DomainResolver::applicationLayerPath(),
...array_values(config('ddd.layers', [])),
])->map(fn ($path) => app()->basePath($path))->toArray();
])->map(fn ($path) => $this->app->basePath($path))->toArray();
}

protected function getCustomLayerPaths(): array
{
return collect([
...array_values(config('ddd.layers', [])),
])->map(fn ($path) => app()->basePath($path))->toArray();
])->map(fn ($path) => $this->app->basePath($path))->toArray();
}

protected function handleProviders()
Expand All @@ -123,14 +138,14 @@ protected function handleProviders()
: $this->discoverProviders();

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

static::$registeredProviders = [];

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

return $this;
Expand All @@ -157,11 +172,15 @@ public function run()
return $this;
}

if (! $this->isBooted()) {
$this->boot();
}

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

if (app()->runningInConsole() && ! $this->isConsoleBooted()) {
if ($this->app->runningInConsole() && ! $this->isConsoleBooted()) {
ConsoleApplication::starting(function (ConsoleApplication $artisan) {
foreach (static::$registeredCommands as $command) {
$artisan->resolve($command);
Expand Down
7 changes: 3 additions & 4 deletions tests/Support/AutoloaderTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

use Lunarstorm\LaravelDDD\Facades\Autoload;
use Lunarstorm\LaravelDDD\Support\AutoloadManager;

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

it('can run', function () {
$autoloader = new AutoloadManager;

$autoloader->boot();
Autoload::run();
})->throwsNoExceptions();

beforeEach(function () {
Expand Down Expand Up @@ -39,7 +38,7 @@
});

it('can discover paths to all layers', function () {
$autoloader = new AutoloadManager;
$autoloader = app(AutoloadManager::class);

$expected = [
app()->basePath('src/Domain'),
Expand Down

0 comments on commit c6c08a4

Please sign in to comment.