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

Skip to content

[Messenger] Remove the "obscure" message subscriber configuration #28294

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions UPGRADE-4.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ Messenger

* `EnvelopeItemInterface` doesn't extend `Serializable` anymore
* The `handle` method of the `Symfony\Component\Messenger\Middleware\ValidationMiddleware` and `Symfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware` middlewares now requires an `Envelope` object to be given (because they implement the `EnvelopeAwareInterface`). When using these middleware with the provided `MessageBus`, you will not have to do anything. If you use the middlewares any other way, you can use `Envelope::wrap($message)` to create an envelope for your message.
* `MessageSubscriberInterface::getHandledMessages()` return value has changed. The value of an array item
needs to be an associative array or the method name.

Before:
```php
return [
[FirstMessage::class, 0],
[SecondMessage::class, -10],
];
```

After:
```php
yield FirstMessage::class => ['priority' => 0];
yield SecondMessage::class => ['priority => -10];
```

Before:
```php
return [
SecondMessage::class => ['secondMessageMethod', 20],
];
```

After:
```php
yield SecondMessage::class => [
'method' => 'secondMessageMethod',
'priority' => 20,
];
```

Security
--------
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ CHANGELOG
4.2.0
-----

* [BC BREAK] `MessageSubscriberInterface::getHandledMessages()` return value has changed. The value of an array item
needs to be an associative array or the method name.
* `ValidationMiddleware::handle()` and `SendMessageMiddleware::handle()` now require an `Envelope` object
* `EnvelopeItemInterface` doesn't extend `Serializable` anymore

Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,21 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
}

if (\is_array($method)) {
if (isset($method[0]) && isset($method[1])) {
$messagePriority = $method[1];
$method = $method[0];
} elseif (isset($method['method']) || isset($method['bus'])) {
if (isset($method['bus'])) {
if (!\in_array($method['bus'], $busIds)) {
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);

throw new RuntimeException(sprintf('Invalid configuration %s for message "%s": bus "%s" does not exist.', $messageClassLocation, $messageClass, $method['bus']));
}

$buses = array($method['bus']);
}
if (isset($method['bus'])) {
if (!\in_array($method['bus'], $busIds)) {
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);

if (isset($method['priority'])) {
$messagePriority = $method['priority'];
throw new RuntimeException(sprintf('Invalid configuration %s for message "%s": bus "%s" does not exist.', $messageClassLocation, $messageClass, $method['bus']));
}

$method = $method['method'] ?? '__invoke';
} else {
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);
$buses = array($method['bus']);
}

throw new RuntimeException(sprintf('Invalid configuration %s for message "%s".', $messageClassLocation, $messageClass));
if (isset($method['priority'])) {
$messagePriority = $method['priority'];
}

$method = $method['method'] ?? '__invoke';
}

if (!\class_exists($messageClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,25 @@ interface MessageSubscriberInterface extends MessageHandlerInterface
*
* It returns a list of messages like in the following example:
*
* return [MyMessage::class];
* yield MyMessage::class;
*
* It can also change the priority per classes.
*
* return [
* [FirstMessage::class, 0],
* [SecondMessage::class, -10],
* ];
* yield FirstMessage::class => ['priority' => 0];
* yield SecondMessage::class => ['priority => -10];
*
* It can also specify a method and/or a priority per message:
* It can also specify a method, a priority and/or a bus per message:
*
* return [
* FirstMessage::class => 'firstMessageMethod',
* SecondMessage::class => ['secondMessageMethod', 20],
* yield FirstMessage::class => ['method' => 'firstMessageMethod'];
* yield SecondMessage::class => [
* 'method' => 'secondMessageMethod',
* 'priority' => 20,
* 'bus' => 'my_bus_name',
* ];
*
* The benefit of using `yield` instead of returning an array is that you can `yield` multiple times the
* same key and therefore subscribe to the same message multiple times with different options.
*
* The `__invoke` method of the handler will be called as usual with the message to handle.
*/
public static function getHandledMessages(): iterable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,7 @@ class PrioritizedHandler implements MessageSubscriberInterface
{
public static function getHandledMessages(): iterable
{
return array(
array(SecondMessage::class, 10),
);
yield SecondMessage::class => array('priority' => 10);
}

public function __invoke()
Expand All @@ -735,10 +733,8 @@ class HandlerMappingMethods implements MessageSubscriberInterface
{
public static function getHandledMessages(): iterable
{
return array(
DummyMessage::class => 'dummyMethod',
SecondMessage::class => array('secondMessage', 20),
);
yield DummyMessage::class => 'dummyMethod';
yield SecondMessage::class => array('method' => 'secondMessage', 'priority' => 20);
}

public function dummyMethod()
Expand Down