From 5f6df0f1b48d545d73bf2f94b7e0c5c93c77fc1f Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 18 Dec 2024 19:23:08 -0500 Subject: [PATCH] [5.x] Fix addon service provider autoloading (#11285) --- src/Providers/AddonServiceProvider.php | 37 +++++++++++++++++++++----- src/Providers/AppServiceProvider.php | 3 --- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Providers/AddonServiceProvider.php b/src/Providers/AddonServiceProvider.php index 1fd5843a5c..772499f989 100644 --- a/src/Providers/AddonServiceProvider.php +++ b/src/Providers/AddonServiceProvider.php @@ -189,9 +189,6 @@ abstract class AddonServiceProvider extends ServiceProvider public function boot() { - $this->bootedAddons = $this->app->make('statamic.booted-addons'); - $this->autoloadedClasses = $this->app->make('statamic.autoloaded-addon-classes'); - Statamic::booted(function () { if (! $this->getAddon()) { return; @@ -225,7 +222,7 @@ public function boot() ->bootPublishAfterInstall() ->bootAddon(); - $this->bootedAddons[] = $this->getAddon()->id(); + $this->bootedAddons()->push($this->getAddon()->id()); }); } @@ -838,12 +835,12 @@ protected function autoloadFilesFromFolder($folder, $requiredClass = null) continue; } - if ($this->autoloadedClasses->contains($fqcn)) { + if ($this->autoloadedClasses()->contains($fqcn)) { continue; } $autoloadable[] = $fqcn; - $this->autoloadedClasses[] = $fqcn; + $this->autoloadedClasses()->push($fqcn); } return $autoloadable; @@ -856,7 +853,7 @@ private function shouldBootRootItems() // We'll keep track of addons that have been booted to ensure that multiple // providers don't try to boot things twice. This could happen if there are // multiple providers in the root autoload directory (src) of an addon. - if ($this->bootedAddons->contains($addon->id())) { + if ($this->bootedAddons()->contains($addon->id())) { return false; } @@ -869,4 +866,30 @@ private function shouldBootRootItems() return $thisDir === $autoloadDir; } + + private function autoloadedClasses() + { + if ($this->autoloadedClasses) { + return $this->autoloadedClasses; + } + + if (! $this->app->bound($autoloaded = 'statamic.autoloaded-addon-classes')) { + $this->app->instance($autoloaded, collect()); + } + + return $this->autoloadedClasses = $this->app->make($autoloaded); + } + + private function bootedAddons() + { + if ($this->bootedAddons) { + return $this->bootedAddons; + } + + if (! $this->app->bound($booted = 'statamic.booted-addons')) { + $this->app->instance($booted, collect()); + } + + return $this->bootedAddons = $this->app->make($booted); + } } diff --git a/src/Providers/AppServiceProvider.php b/src/Providers/AppServiceProvider.php index 4cbb125489..ada744379f 100644 --- a/src/Providers/AppServiceProvider.php +++ b/src/Providers/AppServiceProvider.php @@ -105,9 +105,6 @@ public function boot() $this->addAboutCommandInfo(); $this->app->make(Schedule::class)->job(new HandleEntrySchedule)->everyMinute(); - - $this->app->instance('statamic.booted-addons', collect()); - $this->app->instance('statamic.autoloaded-addon-classes', collect()); } public function register()