From c6c08a4b1a782eac52ca9b74413a1d3aee9f6ac8 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Fri, 15 Nov 2024 22:33:14 -0500 Subject: [PATCH] Require app instance --- src/LaravelDDDServiceProvider.php | 54 +++++++++++++++---------------- src/Support/AutoloadManager.php | 39 ++++++++++++++++------ tests/Support/AutoloaderTest.php | 7 ++-- 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/LaravelDDDServiceProvider.php b/src/LaravelDDDServiceProvider.php index f275afa..11d843b 100644 --- a/src/LaravelDDDServiceProvider.php +++ b/src/LaravelDDDServiceProvider.php @@ -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 * @@ -119,8 +93,6 @@ protected function registerMigrations() public function packageBooted() { - Autoload::boot(); - Autoload::run(); if ($this->app->runningInConsole() && method_exists($this, 'optimizes')) { @@ -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(); } } diff --git a/src/Support/AutoloadManager.php b/src/Support/AutoloadManager.php index 9d698e2..f35351d 100644 --- a/src/Support/AutoloadManager.php +++ b/src/Support/AutoloadManager.php @@ -24,6 +24,8 @@ class AutoloadManager { use Conditionable; + protected $app; + protected string $appNamespace; protected static array $registeredCommands = []; @@ -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')) { @@ -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()); @@ -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 = []; @@ -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() @@ -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; @@ -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); diff --git a/tests/Support/AutoloaderTest.php b/tests/Support/AutoloaderTest.php index a39deab..0e83050 100644 --- a/tests/Support/AutoloaderTest.php +++ b/tests/Support/AutoloaderTest.php @@ -1,5 +1,6 @@ boot(); + Autoload::run(); })->throwsNoExceptions(); beforeEach(function () { @@ -39,7 +38,7 @@ }); it('can discover paths to all layers', function () { - $autoloader = new AutoloadManager; + $autoloader = app(AutoloadManager::class); $expected = [ app()->basePath('src/Domain'),