[Messenger] Add asynchronous message handling support to `HandlerTrait`
| Q | A |
|---|---|
| Branch? | 7.1 |
| Bug fix? | no |
| New feature? | yes |
| Deprecations? | no |
| Issues | - |
| License | MIT |
This HandlerTrait was initially designed to handle synchronous messages only, so a message handler is immediately required. However, it's conceptually correct and sometimes necessary that a command bus dispatches commands asynchronously, which is not currently possible with this trait. Otherwise, you'll face this error (if allow_no_handlers is true):
Message of type "FooCommand" was handled zero times. Exactly one handler is expected...
which is expected, because in this case the envelope will be marked as handled (HandledStamp) only when it's processed in the background by HandleMessageMiddleware. However, the envelope will contain at least a SentStamp which we can track.
So this PR proposes to unlock that capability in HandlerTrait by adding a new flag property $allowAsyncHandling, checking for SentStamp, and returning null if no handler is found.
The goal of this new flag $allowAsyncHandling is to force you to manually enable this capability for those buses allowed to handle both approaches at the same time (e.i. CommandBus).