Better CLI loading for ServiceInterface implementations #438
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.
Description
Currently, if you want to load a
ServiceInterface
-implementing class in the WP-CLI context, such as a class inheriting fromAbstractPostType
orAbstractTaxonomy
, you're out of luck. And you really should load those in the CLI context.Only classes implementing
ServiceCliInterface
are loaded in the CLI context, and they're aren't loaded in the web context. Only classes implementingServiceInterface
are loaded in the web context, and you've guessed it.This could be worked around by just making no such distinction, but that lowers developer flexibility and makes you unable not to load e.g. CLI commands in the web context.
Instead of such compromises, this PR proposes an alternative way to indicate a
ServiceInterface
should be loaded in CLI contexts using a class attribute, a cool new PHP 8 feature, alongside our good and trusted friendReflectionClass
. We have been using this without any performance penalty on the web and nothing noticeable in WP-CLI for months now.If this PR is merged, you'll be able to ensure a service class gets loaded in the WP-CLI context like so:
Additionally, if your class is a child of any class annotated with the
ShouldLoadInCliContext
attribute, it will be loaded, so you can annotateAbstractTaxonomy
with themark of the devilattribute or do things like these:I believe this is a valuable backend addition to eightshift-libs and it has helped us to entirely eliminate issues with loading post types, taxonomies and other shared service interfaces in our projects. I am open to any and all comments.
If this gets merged, please also mark
AbstractPostType
andAbstractTaxonomy
with the attribute.