Skip to content

Commit

Permalink
[5.x] Autoload add-on tags, widgets, modifiers etc from folder (#9270)
Browse files Browse the repository at this point in the history
Co-authored-by: Duncan McClean <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Sep 27, 2024
1 parent 94ba406 commit 4731d57
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions src/Providers/AddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ public function bootEvents()

protected function bootTags()
{
foreach ($this->tags as $class) {
$tags = collect($this->tags)
->merge($this->autoloadFilesFromFolder('Tags', Tags::class))
->unique();

foreach ($tags as $class) {
$class::register();
}

Expand All @@ -249,7 +253,11 @@ protected function bootTags()

protected function bootScopes()
{
foreach ($this->scopes as $class) {
$scopes = collect($this->scopes)
->merge($this->autoloadFilesFromFolder('Scopes', Scope::class))
->unique();

foreach ($scopes as $class) {
$class::register();
}

Expand All @@ -258,7 +266,11 @@ protected function bootScopes()

protected function bootActions()
{
foreach ($this->actions as $class) {
$actions = collect($this->actions)
->merge($this->autoloadFilesFromFolder('Actions', Action::class))
->unique();

foreach ($actions as $class) {
$class::register();
}

Expand All @@ -276,7 +288,11 @@ protected function bootDictionaries()

protected function bootFieldtypes()
{
foreach ($this->fieldtypes as $class) {
$fieldtypes = collect($this->fieldtypes)
->merge($this->autoloadFilesFromFolder('Fieldtypes', Fieldtype::class))
->unique();

foreach ($fieldtypes as $class) {
$class::register();
}

Expand All @@ -285,7 +301,11 @@ protected function bootFieldtypes()

protected function bootModifiers()
{
foreach ($this->modifiers as $class) {
$modifiers = collect($this->modifiers)
->merge($this->autoloadFilesFromFolder('Modifiers', Modifier::class))
->unique();

foreach ($modifiers as $class) {
$class::register();
}

Expand All @@ -294,7 +314,11 @@ protected function bootModifiers()

protected function bootWidgets()
{
foreach ($this->widgets as $class) {
$widgets = collect($this->widgets)
->merge($this->autoloadFilesFromFolder('Widgets', Widget::class))
->unique();

foreach ($widgets as $class) {
$class::register();
}

Expand Down Expand Up @@ -322,7 +346,13 @@ protected function bootPolicies()
protected function bootCommands()
{
if ($this->app->runningInConsole()) {
$this->commands($this->commands);
$commands = collect($this->commands)
->merge($this->autoloadFilesFromFolder('Commands', Command::class))
->merge($this->autoloadFilesFromFolder('Console/Commands', Command::class))
->unique()
->all();

$this->commands($commands);
}

return $this;
Expand Down Expand Up @@ -507,7 +537,11 @@ protected function bootMiddleware()

protected function bootUpdateScripts()
{
foreach ($this->updateScripts as $class) {
$scripts = collect($this->updateScripts)
->merge($this->autoloadFilesFromFolder('UpdateScripts', UpdateScript::class))
->unique();

foreach ($scripts as $class) {
$class::register($this->getAddon()->package());
}

Expand Down Expand Up @@ -667,4 +701,26 @@ protected function bootFieldsets()

return $this;
}

protected function autoloadFilesFromFolder($folder, $requiredClass)
{
$addon = $this->getAddon();
$path = $addon->directory().$addon->autoload().'/'.$folder;

if (! $this->app['files']->exists($path)) {
return [];
}

$autoloadable = [];

foreach ($this->app['files']->files($path) as $file) {
$class = $file->getBasename('.php');
$fqcn = $this->namespace().'\\'.str_replace('/', '\\', $folder).'\\'.$class;
if (is_subclass_of($fqcn, $requiredClass)) {
$autoloadable[] = $fqcn;
}
}

return $autoloadable;
}
}

0 comments on commit 4731d57

Please sign in to comment.