cms icon indicating copy to clipboard operation
cms copied to clipboard

[5.x] Autoload add-on tags, widgets, modifiers etc from folder

Open ryanmitchell opened this issue 7 months ago • 7 comments

@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

ryanmitchell avatar Jan 05 '24 22:01 ryanmitchell

My hero

edalzell avatar Jan 05 '24 22:01 edalzell

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?

https://github.com/statamic/cms/blob/51668c9cef9d20884b032a169fc9fa776debf566/src/Providers/AddonServiceProvider.php#L514-L521

duncanmcclean avatar Jan 15 '24 11:01 duncanmcclean

I've added support for UpdateScripts too

ryanmitchell avatar Jan 15 '24 11:01 ryanmitchell

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.

jasonvarga avatar Jan 19 '24 20:01 jasonvarga

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.

jasonvarga avatar Jan 19 '24 21:01 jasonvarga

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

ryanmitchell avatar Jan 19 '24 21:01 ryanmitchell

@jasonvarga just a proof of concept and would need refined, but you could use the existing addon manifest cache to do it... something like https://github.com/statamic/cms/pull/9270/commits/0114c4cf4e73983e65acdc8376e8e3a29394d100

ryanmitchell avatar Jan 20 '24 14:01 ryanmitchell