Skip to content

Commit a036279

Browse files
[Messenger] internal cleanups
1 parent 662977c commit a036279

26 files changed

+131
-140
lines changed

Asynchronous/Middleware/SendMessageMiddleware.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,17 @@ public function handle(Envelope $envelope, callable $next): void
4444
return;
4545
}
4646

47-
$sender = $this->senderLocator->getSenderForMessage($envelope->getMessage());
47+
$sender = $this->senderLocator->getSender($envelope);
4848

4949
if ($sender) {
5050
$sender->send($envelope);
5151

52-
if (!$this->mustSendAndHandle($envelope->getMessage())) {
52+
if (!AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $envelope)) {
53+
// message has no corresponding handler
5354
return;
5455
}
5556
}
5657

5758
$next($envelope);
5859
}
59-
60-
private function mustSendAndHandle($message): bool
61-
{
62-
return (bool) AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $message);
63-
}
6460
}

Asynchronous/Routing/AbstractSenderLocator.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@
1111

1212
namespace Symfony\Component\Messenger\Asynchronous\Routing;
1313

14+
use Symfony\Component\Messenger\Envelope;
15+
1416
/**
1517
* @author Samuel Roze <[email protected]>
1618
*
1719
* @internal
1820
*/
1921
abstract class AbstractSenderLocator implements SenderLocatorInterface
2022
{
21-
public static function getValueFromMessageRouting(array $mapping, $message)
23+
public static function getValueFromMessageRouting(array $mapping, Envelope $envelope)
2224
{
23-
if (isset($mapping[\get_class($message)])) {
24-
return $mapping[\get_class($message)];
25-
}
26-
if ($parentsMapping = array_intersect_key($mapping, class_parents($message))) {
27-
return current($parentsMapping);
25+
if (isset($mapping[$class = \get_class($envelope->getMessage())])) {
26+
return $mapping[$class];
2827
}
29-
if ($interfaceMapping = array_intersect_key($mapping, class_implements($message))) {
30-
return current($interfaceMapping);
28+
29+
foreach (class_parents($class) as $name) {
30+
if (isset($mapping[$name])) {
31+
return $mapping[$name];
32+
}
3133
}
32-
if (isset($mapping['*'])) {
33-
return $mapping['*'];
34+
35+
foreach (class_implements($class) as $name) {
36+
if (isset($mapping[$name])) {
37+
return $mapping[$name];
38+
}
3439
}
3540

36-
return null;
41+
return $mapping['*'] ?? null;
3742
}
3843
}

Asynchronous/Routing/ContainerSenderLocator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Asynchronous\Routing;
1313

1414
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\Messenger\Envelope;
1516
use Symfony\Component\Messenger\Transport\SenderInterface;
1617

1718
/**
@@ -31,9 +32,9 @@ public function __construct(ContainerInterface $senderServiceLocator, array $mes
3132
/**
3233
* {@inheritdoc}
3334
*/
34-
public function getSenderForMessage($message): ?SenderInterface
35+
public function getSender(Envelope $envelope): ?SenderInterface
3536
{
36-
$senderId = self::getValueFromMessageRouting($this->messageToSenderIdMapping, $message);
37+
$senderId = self::getValueFromMessageRouting($this->messageToSenderIdMapping, $envelope);
3738

3839
return $senderId ? $this->senderServiceLocator->get($senderId) : null;
3940
}

Asynchronous/Routing/SenderLocator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Messenger\Asynchronous\Routing;
1313

14+
use Symfony\Component\Messenger\Envelope;
1415
use Symfony\Component\Messenger\Exception\RuntimeException;
1516
use Symfony\Component\Messenger\Transport\SenderInterface;
1617

@@ -29,15 +30,15 @@ public function __construct(array $messageToSenderMapping)
2930
/**
3031
* {@inheritdoc}
3132
*/
32-
public function getSenderForMessage($message): ?SenderInterface
33+
public function getSender(Envelope $envelope): ?SenderInterface
3334
{
34-
$sender = self::getValueFromMessageRouting($this->messageToSenderMapping, $message);
35+
$sender = self::getValueFromMessageRouting($this->messageToSenderMapping, $envelope);
3536
if (null === $sender) {
3637
return null;
3738
}
3839

3940
if (!$sender instanceof SenderInterface) {
40-
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)));
41+
throw new RuntimeException(sprintf('The sender instance provided for message "%s" should be of type "%s" but got "%s".', \get_class($envelope->getMessage()), SenderInterface::class, \is_object($sender) ? \get_class($sender) : \gettype($sender)));
4142
}
4243

4344
return $sender;

Asynchronous/Routing/SenderLocatorInterface.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Messenger\Asynchronous\Routing;
1313

14+
use Symfony\Component\Messenger\Envelope;
1415
use Symfony\Component\Messenger\Transport\SenderInterface;
1516

1617
/**
@@ -21,10 +22,6 @@ interface SenderLocatorInterface
2122
{
2223
/**
2324
* Gets the sender (if applicable) for the given message object.
24-
*
25-
* @param object $message
26-
*
27-
* @return SenderInterface|null
2825
*/
29-
public function getSenderForMessage($message): ?SenderInterface;
26+
public function getSender(Envelope $envelope): ?SenderInterface;
3027
}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ CHANGELOG
2727
* `Envelope`'s constructor and `with()` method now accept `StampInterface` objects as variadic parameters
2828
* Renamed and moved `ReceivedMessage`, `ValidationConfiguration` and `SerializerConfiguration` in the `Stamp` namespace
2929
* Removed the `WrapIntoReceivedMessage`
30+
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
31+
* `MessengerDataCollector::getMessages()` returns an iterable, not just an array anymore
32+
* `AbstractHandlerLocator` is now internal
33+
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(Envelope $envelope)`
34+
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
35+
* `SenderInterface::send()` returns `void`
3036

3137
4.1.0
3238
-----

DataCollector/MessengerDataCollector.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
1818
use Symfony\Component\Messenger\TraceableMessageBus;
1919
use Symfony\Component\VarDumper\Caster\ClassStub;
20-
use Symfony\Component\VarDumper\Cloner\Data;
2120

2221
/**
2322
* @author Samuel Roze <[email protected]>
@@ -55,14 +54,10 @@ public function lateCollect()
5554
}
5655

5756
// Order by call time
58-
usort($messages, function (array $a, array $b): int {
59-
return $a[1] > $b[1] ? 1 : -1;
60-
});
57+
usort($messages, function ($a, $b) { return $a[1] <=> $b[1]; });
6158

6259
// Keep the messages clones only
63-
$this->data['messages'] = array_map(function (array $item): Data {
64-
return $item[0];
65-
}, $messages);
60+
$this->data['messages'] = array_column($messages, 0);
6661
}
6762

6863
/**
@@ -112,18 +107,21 @@ private function collectMessage(string $busName, array $tracedMessage)
112107

113108
public function getExceptionsCount(string $bus = null): int
114109
{
115-
return array_reduce($this->getMessages($bus), function (int $carry, Data $message) {
116-
return $carry += isset($message['exception']) ? 1 : 0;
117-
}, 0);
110+
$count = 0;
111+
foreach ($this->getMessages($bus) as $message) {
112+
$count += (int) isset($message['exception']);
113+
}
114+
115+
return $count;
118116
}
119117

120-
public function getMessages(string $bus = null): array
118+
public function getMessages(string $bus = null): iterable
121119
{
122-
$messages = $this->data['messages'] ?? array();
123-
124-
return $bus ? array_filter($messages, function (Data $message) use ($bus): bool {
125-
return $bus === $message['bus'];
126-
}) : $messages;
120+
foreach ($this->data['messages'] ?? array() as $message) {
121+
if (null === $bus || $bus === $message['bus']) {
122+
yield $message;
123+
}
124+
}
127125
}
128126

129127
public function getBuses(): array

Envelope.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ final class Envelope
2828
*/
2929
public function __construct($message, StampInterface ...$stamps)
3030
{
31+
if (!\is_object($message)) {
32+
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object but got %s.', __METHOD__, \gettype($message)));
33+
}
3134
$this->message = $message;
3235

3336
foreach ($stamps as $stamp) {

Handler/ChainHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ChainHandler
3030
*/
3131
public function __construct(array $handlers)
3232
{
33-
if (empty($handlers)) {
33+
if (!$handlers) {
3434
throw new InvalidArgumentException('A collection of message handlers requires at least one handler.');
3535
}
3636

Handler/Locator/AbstractHandlerLocator.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,39 @@
1111

1212
namespace Symfony\Component\Messenger\Handler\Locator;
1313

14+
use Symfony\Component\Messenger\Envelope;
1415
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
1516

1617
/**
1718
* @author Miha Vrhovnik <[email protected]>
1819
* @author Samuel Roze <[email protected]>
20+
*
21+
* @internal
1922
*/
2023
abstract class AbstractHandlerLocator implements HandlerLocatorInterface
2124
{
22-
public function resolve($message): callable
25+
public function getHandler(Envelope $envelope): callable
2326
{
24-
$class = \get_class($message);
27+
$class = \get_class($envelope->getMessage());
2528

26-
if ($handler = $this->getHandler($class)) {
29+
if ($handler = $this->getHandlerByName($class)) {
2730
return $handler;
2831
}
2932

30-
foreach (class_implements($class, false) as $interface) {
31-
if ($handler = $this->getHandler($interface)) {
33+
foreach (class_parents($class) as $name) {
34+
if ($handler = $this->getHandlerByName($name)) {
3235
return $handler;
3336
}
3437
}
3538

36-
foreach (class_parents($class, false) as $parent) {
37-
if ($handler = $this->getHandler($parent)) {
39+
foreach (class_implements($class) as $name) {
40+
if ($handler = $this->getHandlerByName($name)) {
3841
return $handler;
3942
}
4043
}
4144

4245
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', $class));
4346
}
4447

45-
abstract protected function getHandler(string $class): ?callable;
48+
abstract protected function getHandlerByName(string $name): ?callable;
4649
}

0 commit comments

Comments
 (0)