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

Skip to content

Commit 59ff103

Browse files
[Messenger] use Envelope internally, return void, add EnvelopeHandlerInterface and other cleanups
1 parent 3db3ad2 commit 59ff103

28 files changed

+229
-281
lines changed

src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
1616
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
1717
use Symfony\Component\Messenger\Envelope;
18-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1918
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
2019

2120
/**
2221
* @author Samuel Roze <[email protected]>
2322
* @author Tobias Schultze <http://tobion.de>
2423
*/
25-
class SendMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
24+
class SendMessageMiddleware implements MiddlewareInterface
2625
{
2726
private $senderLocator;
2827
private $messagesToSendAndHandleMapping;
@@ -34,32 +33,28 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3433
}
3534

3635
/**
37-
* @param Envelope $envelope
38-
*
3936
* {@inheritdoc}
4037
*/
41-
public function handle($envelope, callable $next)
38+
public function handle(Envelope $envelope, callable $next): void
4239
{
4340
if ($envelope->get(ReceivedMessage::class)) {
4441
// It's a received message. Do not send it back:
45-
return $next($envelope);
42+
$next($envelope);
43+
44+
return;
4645
}
4746

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

5049
if ($sender) {
5150
$sender->send($envelope);
5251

53-
if (!$this->mustSendAndHandle($envelope->getMessage())) {
52+
if (!AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $envelope)) {
53+
// message has no corresponding handler
5454
return;
5555
}
5656
}
5757

5858
return $next($envelope);
5959
}
60-
61-
private function mustSendAndHandle($message): bool
62-
{
63-
return (bool) AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $message);
64-
}
6560
}

src/Symfony/Component/Messenger/Asynchronous/Routing/AbstractSenderLocator.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,39 @@
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+
$name = $envelope->getMessageName();
26+
27+
if (null !== $name && isset($mapping[$name])) {
28+
return $mapping[$name];
2529
}
26-
if ($parentsMapping = array_intersect_key($mapping, class_parents($message))) {
27-
return current($parentsMapping);
30+
31+
if (isset($mapping[$class = \get_class($envelope->getMessage())])) {
32+
return $mapping[$class];
2833
}
29-
if ($interfaceMapping = array_intersect_key($mapping, class_implements($message))) {
30-
return current($interfaceMapping);
34+
35+
foreach (class_implements($class) as $name) {
36+
if (isset($mapping[$name])) {
37+
return $mapping[$name];
38+
}
3139
}
32-
if (isset($mapping['*'])) {
33-
return $mapping['*'];
40+
41+
foreach (class_parents($class) as $name) {
42+
if (isset($mapping[$name])) {
43+
return $mapping[$name];
44+
}
3445
}
3546

36-
return null;
47+
return $mapping['*'] ?? null;
3748
}
3849
}

src/Symfony/Component/Messenger/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
}

src/Symfony/Component/Messenger/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".', $envelope->getMessageName() ?? \get_class($envelope->getMessage()), SenderInterface::class, \is_object($sender) ? \get_class($sender) : \gettype($sender)));
4142
}
4243

4344
return $sender;

src/Symfony/Component/Messenger/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
}

src/Symfony/Component/Messenger/DataCollector/MessengerDataCollector.php

Lines changed: 14 additions & 24 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
/**
@@ -98,14 +93,6 @@ private function collectMessage(string $busName, array $tracedMessage)
9893
'caller' => $tracedMessage['caller'],
9994
);
10095

101-
if (array_key_exists('result', $tracedMessage)) {
102-
$result = $tracedMessage['result'];
103-
$debugRepresentation['result'] = array(
104-
'type' => \is_object($result) ? \get_class($result) : \gettype($result),
105-
'value' => $result,
106-
);
107-
}
108-
10996
if (isset($tracedMessage['exception'])) {
11097
$exception = $tracedMessage['exception'];
11198

@@ -120,18 +107,21 @@ private function collectMessage(string $busName, array $tracedMessage)
120107

121108
public function getExceptionsCount(string $bus = null): int
122109
{
123-
return array_reduce($this->getMessages($bus), function (int $carry, Data $message) {
124-
return $carry += isset($message['exception']) ? 1 : 0;
125-
}, 0);
110+
$count = 0;
111+
foreach ($this->getMessages($bus) as $message) {
112+
$count += (int) isset($message['exception']);
113+
}
114+
115+
return $count;
126116
}
127117

128-
public function getMessages(string $bus = null): array
118+
public function getMessages(string $bus = null): iterable
129119
{
130-
$messages = $this->data['messages'] ?? array();
131-
132-
return $bus ? array_filter($messages, function (Data $message) use ($bus): bool {
133-
return $bus === $message['bus'];
134-
}) : $messages;
120+
foreach ($this->data['messages'] ?? array() as $message) {
121+
if (null === $bus || $bus === $message['bus']) {
122+
yield $message;
123+
}
124+
}
135125
}
136126

137127
public function getBuses(): array

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ final class Envelope
2727
*/
2828
public function __construct($message, array $items = array())
2929
{
30+
if (!\is_object($message)) {
31+
throw new \TypeException(sprintf('Invalid argument provided to "%s()": expected object but got %s.', __METHOD__, \gettype($message)));
32+
}
3033
$this->message = $message;
3134
foreach ($items as $item) {
3235
$this->items[\get_class($item)] = $item;
@@ -55,15 +58,6 @@ public function with(EnvelopeItemInterface $item): self
5558
return $cloned;
5659
}
5760

58-
public function withMessage($message): self
59-
{
60-
$cloned = clone $this;
61-
62-
$cloned->message = $message;
63-
64-
return $cloned;
65-
}
66-
6761
public function get(string $itemFqcn): ?EnvelopeItemInterface
6862
{
6963
return $this->items[$itemFqcn] ?? null;
@@ -85,14 +79,10 @@ public function getMessage()
8579
return $this->message;
8680
}
8781

88-
/**
89-
* @param object $target
90-
*
91-
* @return Envelope|object The original message or the envelope if the target supports it
92-
* (i.e implements {@link EnvelopeAwareInterface}).
93-
*/
94-
public function getMessageFor($target)
82+
public function getMessageName(): ?string
9583
{
96-
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
84+
$config = $this->items[MessageConfiguration::class] ?? null;
85+
86+
return $config ? $config->getName() : null;
9787
}
9888
}

src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Samuel Roze <[email protected]>
2020
*/
21-
class ChainHandler
21+
class ChainHandler implements EnvelopeHandlerInterface
2222
{
2323
/**
2424
* @var callable[]
@@ -30,21 +30,17 @@ 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

3737
$this->handlers = $handlers;
3838
}
3939

40-
public function __invoke($message)
40+
public function __invoke(Envelope $envelope)
4141
{
42-
$results = array();
43-
4442
foreach ($this->handlers as $handler) {
45-
$results[] = $handler($message);
43+
$handler($envelope instanceof EnvelopeHandlerInterface ? $envelope : $envelope->getMessage());
4644
}
47-
48-
return $results;
4945
}
5046
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
* A marker interface for handlers that want to receive messages wrapped in their envelopes.
16+
*
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
interface EnvelopeHandlerInterface extends MessageHandlerInterface
20+
{
21+
public function __invoke(Envelope $envelope);
22+
}

0 commit comments

Comments
 (0)