8000 Uses a `MessageSubscriberConfiguration` object to describe the priori… · symfony/symfony@332e182 · GitHub
[go: up one dir, main page]

Skip to content

Commit 332e182

Browse files
committed
Uses a MessageSubscriberConfiguration object to describe the priority and method name
1 parent df44094 commit 332e182

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\DependencyInjection\Reference;
2222
use Symfony\Component\Messenger\Handler\ChainHandler;
2323
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
24+
use Symfony\Component\Messenger\Handler\MessageSubscriberConfiguration;
2425
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
2526
use Symfony\Component\Messenger\TraceableMessageBus;
2627
use Symfony\Component\Messenger\Handler\MethodOnObjectHandler;
@@ -79,43 +80,28 @@ private function registerHandlers(ContainerBuilder $container)
7980
foreach ($container->findTaggedServiceIds($this->handlerTag, true) as $serviceId => $tags) {
8081
foreach ($tags as $tag) {
8182
$r = $container->getReflectionClass($container->getDefinition($serviceId)->getClass());
83+
$handles = isset($tag['handles']) ? array($tag['handles']) : $this->guessHandledClasses($r, $serviceId);
8284

83-
if (isset($tag['handles'])) {
84-
$handles = isset($tag['method']) ? array($tag['handles'] => $tag['method']) : array($tag['handles']);
85-
} else {
86-
$handles = $this->guessHandledClasses($r, $serviceId);
87-
}
88-
89-
$priority = $tag['priority'] ?? 0;
90-
91-
foreach ($handles as $messageClass => $method) {
92-
if (\is_int($messageClass)) {
93-
$messageClass = $method;
94-
$method = '__invoke';
95-
}
96-
97-
if (\is_array($messageClass)) {
98-
$messagePriority = $messageClass[1];
99-
$messageClass = $messageClass[0];
100-
} else {
101-
$messagePriority = $priority;
85+
foreach ($handles as $messageSubscriberConfiguration) {
86+
if (\is_string($messageSubscriberConfiguration)) {
87+
$messageSubscriberConfiguration = new MessageSubscriberConfiguration($messageSubscriberConfiguration);
10288
}
10389

104-
if (\is_array($method)) {
105-
$messagePriority = $method[1];
106-
$method = $method[0];
90+
if (!$messageSubscriberConfiguration instanceof MessageSubscriberConfiguration) {
91+
throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s:%s()" must return strings or "MessageSubscriberConfiguration" objects.', $serviceId, $r->getName(), 'getHandledMessages'));
10792
}
10893

109-
if (!\class_exists($messageClass)) {
110-
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageHandlerInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);
94+
if (!\class_exists($messageClass = $messageSubscriberConfiguration->getMessageClass())) {
95+
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageHandlerInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $messageSubscriberConfiguration->getMethod());
11196

11297
throw new RuntimeException(sprintf('Invalid handler service "%s": message class "%s" %s does not exist.', $serviceId, $messageClass, $messageClassLocation));
11398
}
11499

115-
if (!$r->hasMethod($method)) {
100+
if (!$r->hasMethod($method = $messageSubscriberConfiguration->getMethod())) {
116101
throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s::%s()" does not exist.', $serviceId, $r->getName(), $method));
117102
}
118103

104+
$messagePriority = $messageSubscriberConfiguration->getPriority();
119105
if ('__invoke' !== $method) {
120106
$wrapperDefinition = (new Definition(MethodOnObjectHandler::class))->setArguments(array(new Reference($serviceId), $method));
121107

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Handler;
13+
14+
/**
15+
* The message subscriber will return a set of `MessageSubscriberConfiguration` to describe which
16+
* message it will subscribe to.
17+
*
18+
* @author Samuel Roze <samuel.roze@gmail.com>
19+
*/
20+
class MessageSubscriberConfiguration
21+
{
22+
private $messageClass;
23+
private $priority;
24+
private $method;
25+
26+
public function __construct(string $messageClass, int $priority = 0, string $method = '__invoke')
27+
{
28+
$this->messageClass = $messageClass;
29+
$this->priority = $priority;
30+
$this->method = $method;
31+
}
32+
33+
public function getMessageClass(): string
34+
{
35+
return $this->messageClass;
36+
}
37+
38+
public function getPriority(): int
39+
{
40+
return $this->priority;
41+
}
42+
43+
public function getMethod(): string
44+
{
45+
return $this->method;
46+
}
47+
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,28 @@ interface MessageSubscriberInterface extends MessageHandlerInterface
2323
*
2424
* It returns a list of messages like in the following example:
2525
*
26-
* return [MyMessage::class];
26+
* return [
27+
* MyMessage::class,
28+
* ];
2729
*
2830
* It can also change the priority per classes.
2931
*
3032
* return [
31-
* [FirstMessage::class, 0],
32-
* [SecondMessage::class, -10],
33+
* new MessageSubscriberConfiguration(FirstMessage::class, 0),
34+
* new MessageSubscriberConfiguration(SecondMessage::class, -10),
3335
* ];
3436
*
3537
* It can also specify a method and/or a priority per message:
3638
*
3739
* return [
38-
* FirstMessage::class => 'firstMessageMethod',
39-
* SecondMessage::class => ['secondMessageMethod', 20],
40+
* new MessageSubscriberConfiguration(FirstMessage::class, 0, 'firstMessageMethod'),
41+
* new MessageSubscriberConfiguration(SecondMessage::class, 20, 'secondMessageMethod'),
4042
* ];
4143
*
42-
* The `__invoke` method of the handler will be called as usual with the message to handle.
44+
* If not configured otherwise, the `__invoke` method of the handler will be called as usual with
45+
* the message to handle.
4346
*
4447
* @return array
4548
*/
46-
public static function getHandledMessages(): array;
49+
public static function getHandledMessages(): iterable;
4750
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
2323
use Symfony\Component\Messenger\DependencyInjection\MessengerPass;
2424
use Symfony\Component\Messenger\Handler\ChainHandler;
25+
use Symfony\Component\Messenger\Handler\MessageSubscriberConfiguration;
2526
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
2627
use Symfony\Component\Messenger\Handler\MethodOnObjectHandler;
2728
use Symfony\Component\Messenger\MessageBusInterface;
@@ -390,7 +391,7 @@ public function __invoke(UndefinedMessage $message)
390391

391392
class UndefinedMessageHandlerViaInterface implements MessageSubscriberInterface
392393
{
393-
public static function getHandledMessages(): array
394+
public static function getHandledMessages(): iterable
394395
{
395396
return array(UndefinedMessage::class);
396397
}
@@ -445,7 +446,7 @@ class PrioritizedHandler implements MessageSubscriberInterface
445446
public static function getHandledMessages(): array
446447
{
447448
return array(
448-
array(SecondMessage::class, 10),
449+
new MessageSubscriberConfiguration(SecondMessage::class, 10),
449450
);
450451
}
451452

@@ -459,8 +460,8 @@ class HandlerMappingMethods implements MessageSubscriberInterface
459460
public static function getHandledMessages(): array
460461
{
461462
return array(
462-
DummyMessage::class => 'dummyMethod',
463-
SecondMessage::class => array('secondMessage', 20),
463+
new MessageSubscriberConfiguration(DummyMessage::class, 0, 'dummyMethod'),
464+
new MessageSubscriberConfiguration(SecondMessage::class, 20, 'secondMessage'),
464465
);
465466
}
466467

@@ -475,17 +476,17 @@ public function secondMessage()
475476

476477
class HandlerMappingWithNonExistentMethod implements MessageSubscriberInterface
477478
{
478-
public static function getHandledMessages(): array
479+
public static function getHandledMessages(): iterable
479480
{
480481
return array(
481-
DummyMessage::class => 'dummyMethod',
482+
new MessageSubscriberConfiguration(DummyMessage::class, 0, 'dummyMethod'),
482483
);
483484
}
484485
}
485486

486487
class HandleNoMessageHandler implements MessageSubscriberInterface
487488
{
488-
public static function getHandledMessages(): array
489+
public static function getHandledMessages(): iterable
489490
{
490491
return array();
491492
}

0 commit comments

Comments
 (0)
0