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

Skip to content

[Messenger] Add a SenderLocator decoupled from ContainerInterface#28399

Merged
fabpot merged 1 commit into
symfony:masterfrom
fabpot:sender-locator
Sep 8, 2018
Merged

[Messenger] Add a SenderLocator decoupled from ContainerInterface#28399
fabpot merged 1 commit into
symfony:masterfrom
fabpot:sender-locator

Conversation

@fabpot

@fabpot fabpot commented Sep 8, 2018

Copy link
Copy Markdown
Member
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(),
    ])),
]);

@sroze sroze left a comment

Copy link
Copy Markdown
Contributor

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? 😉

-----

* [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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

fabpot commented Sep 8, 2018

Copy link
Copy Markdown
Member Author

Now with some tests.

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

Copy link
Copy Markdown
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
Copy Markdown
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