Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
Sep 8, 2018

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented Sep 8, 2018

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:

new MessageBus([
    new SendMessageMiddleware(new SenderLocator([
        Message::class => new AmqpTransport($encoderDecoder, $encoderDecoder, $connection),
    ])),
    new HandleMessageMiddleware(new HandlerLocator([
        Message::class => new MessageHandler(),
    ])),
]);

Copy link
Contributor

@sroze sroze left a 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
Copy link
Contributor

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.

?

Copy link
Member Author

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);
Copy link
Contributor

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));
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2nd get_class hidden 😉

@fabpot fabpot force-pushed the sender-locator branch 2 times, most recently from ecc9a92 to 9bed828 Compare September 8, 2018 12:11
@fabpot
Copy link
Member Author

fabpot commented Sep 8, 2018

Now with some tests.

/**
* @author Samuel Roze <[email protected]>
*/
abstract class AbstractSenderLocator implements SenderLocatorInterface
Copy link
Contributor

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?

Copy link
Member Author

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.

@fabpot fabpot merged commit e658e15 into symfony:master Sep 8, 2018
fabpot added a commit that referenced this pull request Sep 8, 2018
…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
This was referenced Nov 3, 2018
@fabpot fabpot deleted the sender-locator branch January 14, 2019 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants