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

Skip to content

Commit 8651758

Browse files
committed
feature #28294 [Messenger] Remove the "obscure" message subscriber configuration (sroze)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Messenger] Remove the "obscure" message subscriber configuration | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | ø | License | MIT | Doc PR | ø As described in #28275, all of the configuration can be done using yield and that we could remove the support for other ways (especially the obscure return `[['method', -10]]` syntax) as I believe this would clarify the configuration a lot. Commits ------- cf2ad86 Remove the "obscure" message subscriber configuration
2 parents 7504535 + cf2ad86 commit 8651758

File tree

5 files changed

+58
-35
lines changed

5 files changed

+58
-35
lines changed

UPGRADE-4.2.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,37 @@ Messenger
8989

9090
* `EnvelopeItemInterface` doesn't extend `Serializable` anymore
9191
* 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.
92+
* `MessageSubscriberInterface::getHandledMessages()` return value has changed. The value of an array item
93+
needs to be an associative array or the method name.
94+
95+
Before:
96+
```php
97+
return [
98+
[FirstMessage::class, 0],
99+
[SecondMessage::class, -10],
100+
];
101+
```
102+
103+
After:
104+
```php
105+
yield FirstMessage::class => ['priority' => 0];
106+
yield SecondMessage::class => ['priority => -10];
107+
```
108+
109+
Before:
110+
```php
111+
return [
112+
SecondMessage::class => ['secondMessageMethod', 20],
113+
];
114+
```
115+
116+
After:
117+
```php
118+
yield SecondMessage::class => [
119+
'method' => 'secondMessageMethod',
120+
'priority' => 20,
121+
];
122+
```
92123

93124
Security
94125
--------

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* [BC BREAK] `MessageSubscriberInterface::getHandledMessages()` return value has changed. The value of an array item
8+
needs to be an associative array or the method name.
79
* `ValidationMiddleware::handle()` and `SendMessageMiddleware::handle()` now require an `Envelope` object
810
* `EnvelopeItemInterface` doesn't extend `Serializable` anymore
911
* [BC BREAK] The `ConsumeMessagesCommand` class now takes an instance of `Psr\Container\ContainerInterface`

src/Symfony/Component/Messenger/DependencyInjection/MessengerPass.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,30 +111,21 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
111111
}
112112

113113
if (\is_array($method)) {
114-
if (isset($method[0]) && isset($method[1])) {
115-
$messagePriority = $method[1];
116-
$method = $method[0];
117-
} elseif (isset($method['method']) || isset($method['bus'])) {
118-
if (isset($method['bus'])) {
119-
if (!\in_array($method['bus'], $busIds)) {
120-
$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);
121-
122-
throw new RuntimeException(sprintf('Invalid configuration %s for message "%s": bus "%s" does not exist.', $messageClassLocation, $messageClass, $method['bus']));
123-
}
124-
125-
$buses = array($method['bus']);
126-
}
114+
if (isset($method['bus'])) {
115+
if (!\in_array($method['bus'], $busIds)) {
116+
$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);
127117

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

132-
$method = $method['method'] ?? '__invoke';
133-
} else {
134-
$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);
121+
$buses = array($method['bus']);
122+
}
135123

136-
throw new RuntimeException(sprintf('Invalid configuration %s for message "%s".', $messageClassLocation, $messageClass));
124+
if (isset($method['priority'])) {
125+
$messagePriority = $method['priority'];
137126
}
127+
128+
$method = $method['method'] ?? '__invoke';
138129
}
139130

140131
if (!\class_exists($messageClass) && !\interface_exists($messageClass)) {

src/Symfony/Component/Messenger/Handler/MessageSubscriberInterface.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@ interface MessageSubscriberInterface extends MessageHandlerInterface
2525
*
2626
* It returns a list of messages like in the following example:
2727
*
28-
* return [MyMessage::class];
28+
* yield MyMessage::class;
2929
*
3030
* It can also change the priority per classes.
3131
*
32-
* return [
33-
* [FirstMessage::class, 0],
34-
* [SecondMessage::class, -10],
35-
* ];
32+
* yield FirstMessage::class => ['priority' => 0];
33+
* yield SecondMessage::class => ['priority => -10];
3634
*
37-
* It can also specify a method and/or a priority per message:
35+
* It can also specify a method, a priority and/or a bus per message:
3836
*
39-
* return [
40-
* FirstMessage::class => 'firstMessageMethod',
41-
* SecondMessage::class => ['secondMessageMethod', 20],
37+
* yield FirstMessage::class => ['method' => 'firstMessageMethod'];
38+
* yield SecondMessage::class => [
39+
* 'method' => 'secondMessageMethod',
40+
* 'priority' => 20,
41+
* 'bus' => 'my_bus_name',
4242
* ];
4343
*
44+
* The benefit of using `yield` instead of returning an array is that you can `yield` multiple times the
45+
* same key and therefore subscribe to the same message multiple times with different options.
46+
*
4447
* The `__invoke` method of the handler will be called as usual with the message to handle.
4548
*/
4649
public static function getHandledMessages(): iterable;

src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,7 @@ class PrioritizedHandler implements MessageSubscriberInterface
723723
{
724724
public static function getHandledMessages(): iterable
725725
{
726-
return array(
727-
array(SecondMessage::class, 10),
728-
);
726+
yield SecondMessage::class => array('priority' => 10);
729727
}
730728

731729
public function __invoke()
@@ -737,10 +735,8 @@ class HandlerMappingMethods implements MessageSubscriberInterface
737735
{
738736
public static function getHandledMessages(): iterable
739737
{
740-
return array(
741-
DummyMessage::class => 'dummyMethod',
742-
SecondMessage::class => array('secondMessage', 20),
743-
);
738+
yield DummyMessage::class => 'dummyMethod';
739+
yield SecondMessage::class => array('method' => 'secondMessage', 'priority' => 20);
744740
}
745741

746742
public function dummyMethod()

0 commit comments

Comments
 (0)