8000 [Messenger] use Envelope internally, return void, add EnvelopeHandler… · symfony/symfony@77b83ab · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 77b83ab

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

36 files changed

+275
-314
lines changed

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

Lines changed: 9 additions & 14 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 <samuel.roze@gmail.com>
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

58-
return $next($envelope);
59-
}
60-
61-
private function mustSendAndHandle($message): bool
62-
{
63-
return (bool) AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $message);
58+
$next($envelope);
6459
}
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 <samuel.roze@gmail.com>
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 <samuel.roze@gmail.com>
@@ -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: 15 additions & 19 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;
@@ -38,9 +41,15 @@ public function __construct($message, array $items = array())
3841
*
3942
* @param Envelope|object $message
4043
*/
41-
public static function wrap($message): self
44+
public static function wrap($message, string $name = null): self
4245
{
43-
return $message instanceof self ? $message : new self($message);
46+
$envelope = $message instanceof self ? clone $message : new self($message);
47+
if (null !== $name) {
48+
return $envelope->with(new MessageConfiguration($name));
49+
}
50+
unset($envelope->items[MessageConfiguration::class]);
51+
52+
return $envelope;
4453
}
4554

4655
/**
@@ -55,15 +64,6 @@ public function with(EnvelopeItemInterface $item): self
5564
return $cloned;
5665
}
5766

58-
public function withMessage($message): self
59-
{
60-
$cloned = clone $this;
61-
62-
$cloned->message = $message;
63-
64-
return $cloned;
65-
}
66-
6767
public function get(string $itemFqcn): ?EnvelopeItemInterface
6868
{
6969
return $this->items[$itemFqcn] ?? null;
@@ -85,14 +85,10 @@ public function getMessage()
8585
return $this->message;
8686
}
8787

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)
88+
public function getMessageName(): ?string
9589
{
96-
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
90+
$config = $this->items[MessageConfiguration::class] ?? null;
91+
92+
return $config ? $config->getName() : null;
9793
}
9894
}

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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
namespace Symfony\Component\Messenger\Handler;
1313

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

1617
/**
1718
* Represents a collection of message handlers.
1819
*
1920
* @author Samuel Roze <samuel.roze@gmail.com>
2021
*/
21-
class ChainHandler
22+
class ChainHandler implements EnvelopeHandlerInterface
2223
{
2324
/**
2425
* @var callable[]
@@ -30,21 +31,17 @@ class ChainHandler
3031
*/
3132
public function __construct(array $handlers)
3233
{
33-
if (empty($handlers)) {
34+
if (!$handlers) {
3435
throw new InvalidArgumentException('A collection of message handlers requires at least one handler.');
3536
}
3637

3738
$this->handlers = $handlers;
3839
}
3940

40-
public function __invoke($message)
41+
public function __invoke(Envelope $envelope)
4142
{
42-
$results = array();
43-
4443
foreach ($this->handlers as $handler) {
45-
$results[] = $handler($message);
44+
$handler($envelope instanceof EnvelopeHandlerInterface ? $envelope : $envelope->getMessage());
4645
}
47-
48-
return $results;
4946
}
5047
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
use Symfony\Component\Messenger\Envelope;
15+
16+
/**
17+
* A marker interface for handlers that want to receive messages wrapped in their envelopes.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
interface EnvelopeHandlerInterface extends MessageHandlerInterface
22+
{
23+
public function __invoke(Envelope $envelope);
24+
}

0 commit comments

Comments
 (0)
0