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] Autoload add-on tags, widgets, modifiers etc from folder #9270

Merged

Conversation

ryanmitchell
Copy link
Contributor

@ryanmitchell ryanmitchell commented Jan 5, 2024

@edalzell wanted this...

This PR autoloads tags, scopes, modifiers, actions, fieldtypes, widgets and commands based on them being in an appropriately named directory within the add-on src directory.

eg src/Tags/MyTag.php will automatically register, so you dont need to add it to the $tags array.

Closes statamic/ideas#88
Closes statamic/ideas#709

@edalzell
Copy link
Contributor

edalzell commented Jan 5, 2024

My hero

Copy link
Member

@duncanmcclean duncanmcclean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great - Simple Commerce would have a much smaller ServiceProvider after this gets merged.

src/Providers/AddonServiceProvider.php Outdated Show resolved Hide resolved
@duncanmcclean
Copy link
Member

One other thing... GitHub wouldn't let me comment on the relevant lines since they weren't changed.

Can we add the same sort of auto-loading magic to update scripts?

protected function bootUpdateScripts()
{
foreach ($this->updateScripts as $class) {
$class::register($this->getAddon()->package());
}
return $this;
}

@ryanmitchell
Copy link
Contributor Author

I've added support for UpdateScripts too

Commands in src/Console/Commands weren't being picked up due to the incorrect FQCN being constructed here.
Copy link
Member

@duncanmcclean duncanmcclean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I've tested this in a few of my addons and it seems to be working great.

I noticed there was an issue with commands in src/Console/Commands not being picked up due to an incorrect $fqcn. I've just pushed up a fix for it.

@jasonvarga
Copy link
Member

jasonvarga commented Jan 19, 2024

The initial reason this wasn't ever done was because of a potential performance hit. It's probably negligible, but it's not nothing.

On every request, we're now looking for tags, scopes, actions, fieldtypes, modifiers, widgets, commands twice, and update scripts. That's 9 directory traversals, for each installed addon. Have 3 addons installed, and you're looping through directories an extra 27 times per request.

I was thinking maybe we could cache these with an please addons:cache command, like the artisan event:cache command. That increases the scope of this PR quite a bit though, probably.

I'll do some testing on load time differences before/after this PR when I have some time. If someone else also wants to, and share their results, that'd be helpful.

Maybe it'll turn out to not be a big deal after all.

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️

@jasonvarga
Copy link
Member

At first glance it's not really making a dent. Maybe this is a non-issue. I'm using an M1 Mac though which is probably faster than most servers.

@ryanmitchell
Copy link
Contributor Author

I'm happy to work on the cache command if you feel it's required, no problem either way.

@ryanmitchell
Copy link
Contributor Author

@jasonvarga just a proof of concept and would need refined, but you could use the existing addon manifest cache to do it... something like 0114c4c

This reverts commit 0114c4c.
@ryanmitchell ryanmitchell changed the base branch from 4.x to 5.x May 10, 2024 10:43
@ryanmitchell ryanmitchell changed the title [4.x] Autoload add-on tags, widgets, modifiers etc from folder [5.x] Autoload add-on tags, widgets, modifiers etc from folder May 10, 2024
@jasonvarga jasonvarga merged commit 4731d57 into statamic:5.x Sep 27, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Load addon components by convention Auto-registering trait in addon service provider
4 participants