Skip to content

Commit

Permalink
Prevent booting root level items more than once.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Dec 13, 2024
1 parent daf6b04 commit d14a4cb
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/Providers/AddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ abstract class AddonServiceProvider extends ServiceProvider
*/
protected $translations = true;

protected static array $autoloaded = [];
private static array $autoloaded = [];

private static array $bootedAddons = [];

public function boot()
{
Expand Down Expand Up @@ -218,6 +220,8 @@ public function boot()
->bootFieldsets()
->bootPublishAfterInstall()
->bootAddon();

static::$bootedAddons[] = $this->getAddon()->id();
});
}

Expand Down Expand Up @@ -456,6 +460,10 @@ protected function bootVite()

protected function bootConfig()
{
if (! $this->shouldBootRootItems()) {
return $this;
}

$filename = $this->getAddon()->slug();
$directory = $this->getAddon()->directory();
$origin = "{$directory}config/{$filename}.php";
Expand All @@ -475,6 +483,10 @@ protected function bootConfig()

protected function bootTranslations()
{
if (! $this->shouldBootRootItems()) {
return $this;
}

$slug = $this->getAddon()->slug();
$directory = $this->getAddon()->directory();
$origin = "{$directory}lang";
Expand Down Expand Up @@ -515,6 +527,10 @@ protected function bootPublishables()

protected function bootRoutes()
{
if (! $this->shouldBootRootItems()) {
return $this;
}

$directory = $this->getAddon()->directory();

$web = Arr::get(
Expand Down Expand Up @@ -622,6 +638,10 @@ protected function bootUpdateScripts()

protected function bootViews()
{
if (! $this->shouldBootRootItems()) {
return $this;
}

if (file_exists($this->getAddon()->directory().'resources/views')) {
$this->loadViewsFrom(
$this->getAddon()->directory().'resources/views',
Expand All @@ -634,6 +654,10 @@ protected function bootViews()

public function registerScript(string $path)
{
if (! $this->shouldBootRootItems()) {
return;
}

$name = $this->getAddon()->packageName();
$version = $this->getAddon()->version();
$filename = pathinfo($path, PATHINFO_FILENAME);
Expand Down Expand Up @@ -748,6 +772,10 @@ protected function bootPublishAfterInstall()

protected function bootBlueprints()
{
if (! $this->shouldBootRootItems()) {
return $this;
}

if (! file_exists($path = "{$this->getAddon()->directory()}resources/blueprints")) {
return $this;
}
Expand All @@ -762,6 +790,10 @@ protected function bootBlueprints()

protected function bootFieldsets()
{
if (! $this->shouldBootRootItems()) {
return $this;
}

if (! file_exists($path = "{$this->getAddon()->directory()}resources/fieldsets")) {
return $this;
}
Expand Down Expand Up @@ -820,4 +852,22 @@ protected function autoloadFilesFromFolder($folder, $requiredClass = null)

return $autoloadable;
}

private function shouldBootRootItems()
{
$addon = $this->getAddon();

// 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 (in_array($addon->id(), static::$bootedAddons)) {
return false;
}

// We only want to boot root items if the provider is in the autoloaded directory.
// i.e. It's the "root" provider. If it's in a subdirectory maybe the developer
// is organizing their providers. Things like tags etc. can be autoloaded but
// root level things like routes, views, config, blueprints, etc. will not.
return dirname((new \ReflectionClass(static::class))->getFileName()) === $addon->directory().$addon->autoload();
}
}

0 comments on commit d14a4cb

Please sign in to comment.