-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Add a SenderLocator decoupled from ContainerInterface #28399
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
Conversation
ffb169c
to
11a457f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test for the SenderLocator
? 😉
@@ -4,6 +4,8 @@ CHANGELOG | |||
4.2.0 | |||
----- | |||
|
|||
* [BC BREAK] `SenderLocator` has been renamed to `ContainerSenderLocator` | |||
Be careful as there is still a `SenderLocator` class, but it does not rely on a ContainerInterface to find senders |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- but it does not rely on a ContainerInterface to find senders
+ but it does not rely on a `ContainerInterface` to find senders. Instead, it accepts the sender instance itself instead of its identifier in the container.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} | ||
|
||
return null; | ||
return self::getValueFromMessageRouting($this->messageToSenderMapping, $message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Especially with the BC-break, it would be very good to have an explicit exception if this return value is not a SenderInterface
. For example:
if (!$sender instanceof SenderInterface) {
throw new RuntimeException(sprintf('The sender instance provided for the message "%s" should be of type "%s" but got "%s"', $message, SenderInterface::class, is_object($sender) ? get_class($sender) : gettype($sender));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
11a457f
to
4204d54
Compare
return $mapping['*']; | ||
$sender = self::getValueFromMessageRouting($this->messageToSenderMapping, $message); | ||
if (!$sender instanceof SenderInterface) { | ||
throw new RuntimeException(sprintf('The sender instance provided for message "%s" should be of type "%s" but got "%s".', \get_class($message), SenderInterface::class, is_object($sender) ? get_class($sender) : gettype($sender))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2nd get_class
hidden 😉
ecc9a92
to
9bed828
Compare
Now with some tests. |
/** | ||
* @author Samuel Roze <[email protected]> | ||
*/ | ||
abstract class AbstractSenderLocator implements SenderLocatorInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reasoning for using an abstract class over a trait for this static
method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it is used in SendMessageMiddleware
, I think it's "better" like this.
9bed828
to
e658e15
Compare
…erInterface (fabpot) This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Add a SenderLocator decoupled from ContainerInterface | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | upcoming For handler locators, we have a generic `HandlerLocator` class that takes a simple mapping instead of a service locator. The same did not exist for sender locators. So, this PR adds this possibility as well. That allows for something like this: ```php new MessageBus([ new SendMessageMiddleware(new SenderLocator([ Message::class => new AmqpTransport($encoderDecoder, $encoderDecoder, $connection), ])), new HandleMessageMiddleware(new HandlerLocator([ Message::class => new MessageHandler(), ])), ]); ``` Commits ------- e658e15 [Messenger] added a SenderLocator decoupled from ContainerInterface
For handler locators, we have a generic
HandlerLocator
class that takes a simple mapping instead of a service locator. The same did not exist for sender locators. So, this PR adds this possibility as well. That allows for something like this: