Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Fix autoloading when addon has multiple service providers #11128

Merged
merged 7 commits into from
Dec 17, 2024
64 changes: 61 additions & 3 deletions src/Providers/AddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ abstract class AddonServiceProvider extends ServiceProvider
*/
protected $translations = true;

private static array $autoloaded = [];

private static array $bootedAddons = [];

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

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

Expand Down Expand Up @@ -454,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 @@ -473,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 @@ -513,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 @@ -620,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 @@ -632,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 @@ -746,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 @@ -760,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 All @@ -783,7 +817,8 @@ protected function autoloadFilesFromFolder($folder, $requiredClass = null)
return [];
}

$path = $addon->directory().$addon->autoload().'/'.$folder;
$reflection = new \ReflectionClass(static::class);
$path = dirname($reflection->getFileName()).'/'.$folder;

if (! $this->app['files']->exists($path)) {
return [];
Expand All @@ -797,19 +832,42 @@ protected function autoloadFilesFromFolder($folder, $requiredClass = null)
}

$class = $file->getBasename('.php');
$fqcn = $this->namespace().'\\'.str_replace('/', '\\', $folder).'\\'.$class;
$fqcn = $reflection->getNamespaceName().'\\'.str_replace('/', '\\', $folder).'\\'.$class;

if ((new \ReflectionClass($fqcn))->isAbstract() || (new \ReflectionClass($fqcn))->isInterface()) {
continue;
}

if ($requiredClass && ! is_subclass_of($fqcn, $requiredClass)) {
return;
continue;
}

if (in_array($fqcn, static::$autoloaded)) {
continue;
}

$autoloadable[] = $fqcn;
static::$autoloaded[] = $fqcn;
}

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();
}
}
Loading