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

[Feature]: Extension boot method priority #974

Open
CupNoodles opened this issue May 15, 2022 · 4 comments
Open

[Feature]: Extension boot method priority #974

CupNoodles opened this issue May 15, 2022 · 4 comments

Comments

@CupNoodles
Copy link
Contributor

What is the feature?

I'm not sure if this is already possible, and if it is then please let me know how and I'll close this issue!

Basically, I have a lot of Extensions and many of them listen to the same events, and in some cases it's necessary to know which order the Extensions will be booted in. Right now, the order seems to differ from server to server for me, implying that it has something to do with PHP file listing order which we don't have control over (and don't need).

As an example, if extension A adds a button to a menu and extension B removes the button (if it's been added) and then adds it's own button, then if the extensions load [A,B] then we get one button, as expected, but if they boot in [B,A] order then we end up with 2 buttons. If I don't have control over extension A, then there's no way for extension B to conditionally remove extension A's button in it's boot() method, without knowing for sure that it'll be booted after extension A.

I've got a sample branch of how this could work at https://github.com/tastyigniter/TastyIgniter/compare/3.x...CupNoodles:dk/extension_boot_priority?expand=1. Adding the boot_priority property to your Extension class would then conditionally sort your boot method.

Version

Other (please detail)

@sampoyigi
Copy link
Member

I've encountered this problem before, and what I usually do is determine which of the events to boot once all extensions (service providers) have been booted, and then use the App::booted callback within the extension boot method.

App::booted(function() {
 ...
});

Another approach would be to implement something similar to Laravel Middleware priorities or just use 'installed.json' to ensure that extensions are loaded in the order in which they were installed, which will also work nicely with the require manifest key. Would accept a PR for either approach :)

@CupNoodles
Copy link
Contributor Author

Oho! Thanks for the tip about App:booted that will almost definitely solve my issue for now, although eventually we will need something a bit more robust since we may need to order multiple extensions within App::booted as well.

Is the require manifest key supposed to work to make sure a required ext is booted before the ext itself? I was playing around with it and saw that the boot methods were happening in (i think) a fixed order on each server no matter what extensions were required for what. The comment on listByDependencies() seemed very promising but I don't believe it's being called before boot().

@sampoyigi
Copy link
Member

Yes, that is the intended purpose of the require manifest key, however for now, it just ensures that the defined extensions in the require key are installed before the 'manifest' extension is installed.

@CupNoodles
Copy link
Contributor Author

Sorry, I'm re-visiting this and realize that I was using the require manifest key wrong (didn't realize that my extension's extensionMeta() function was overriding the old style extension.json). ExtensionManager->listByDependencies() seems like all the ordering we actually need, so what if instead of using installed.json we just call listByDepenencies() in ExtensionsManager->bootExtensions()?

Something like CupNoodles@646e2f5 - will make a PR into 3.x unless there's something I'm missing here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants