Refactor installer class to load dependencies when needed #2764
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes proposed in this Pull Request:
This PR makes 2 main refactors to the Installer class.
1. Remove validation
In the constructor we were explicitly passing all classes which implement
InstallableInterface
and then validating to check if they implemented the same interface. Since the container is responsible for passing the right dependencies we don't need to do another loop through those classes to validate them.2. Load dependencies when needed
Passing the dependencies in the constructor means it will call the constructor of all the dependencies, even when we don't use them. Since the Install class relies on a hook being called and some other additional conditions, we can retrieve the necessary classes from the container only when they are needed. This is a minor optimization in how the extension constructs itself, but together with other optimizations it will make a difference.
If we look at the before and after of a front end request we can see that it reduces the time spent resolving container dependencies.
Before:
After:
Since we changed the load order this was initially generating a fatal error:
This was because the
DBServiceProvider
wasn't registered as the provider of this class, so it wasn't resolving when loading in a different order.Detailed test instructions:
add_filter( 'woocommerce_gla_force_run_install', '__return_true' );
gla_db_version
gla_install_version
wp_gla_budget_recommendations
Changelog entry