Problem/Motivation
A pattern I found myself using a lot lately, when having to support multiple alternative strategies to fulfill a set of requirements, involves defining a plugin for each strategy to be implemented. Depending on the requirements, the first strategy applying to the current context or all strategies applying to it would then be run.
A possible example of this pattern would be the implementation of a service allowing to open images encoded in multiple formats. The service would internally rely on different plugins, each one supporting a different format. Each format plugin would be checked for applicability and the first match would be used to actually decode the image file.
Proposed resolution
A PoC implementation of the (generic parts of) this pattern are available here. This includes the concept of weighted plugin, since often it's useful to sort plugins by weight/priority when checking their applicability. If supporting this pattern in core is deemed useful, a version of the PoC code would be added to \Drupal\Component\Plugin
.
Remaining tasks
- Validate the proposed solution
- Evaluate the PoC code.
- Create a MR
- Review the MR
Introduced terminology
- Applicable plugin: a plugin applying to a certain context.
- Weighted plugin: a plugin having a numeric weight as part of its definition.
API changes
No change, only additions.
Release notes snippet
TBD