Skip to content

Commit 8677998

Browse files
[Messenger] make dispatch(), handle() and send() methods return Envelope
1 parent 5ae0e89 commit 8677998

27 files changed

+87
-63
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* The component is not experimental anymore
88
* All the changes below are BC BREAKS
9-
* `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
9+
* `MessageBusInterface::dispatch()`, `MiddlewareInterface::handle()` and `SenderInterface::send()` return `Envelope`
1010
* `MiddlewareInterface::handle()` now require an `Envelope` as first argument
1111
* `EnvelopeAwareInterface` has been removed
1212
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
@@ -32,7 +32,6 @@ CHANGELOG
3232
* `AbstractHandlerLocator` is now internal
3333
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(Envelope $envelope): ?callable` and shouldn't throw when no handlers are found
3434
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
35-
* `SenderInterface::send()` returns `void`
3635
* Classes in the `Middleware\Enhancers` sub-namespace have been moved to the `Middleware` one
3736
* Classes in the `Asynchronous\Routing` sub-namespace have been moved to the `Transport\Sender\Locator` sub-namespace
3837
* The `Asynchronous/Middleware/SendMessageMiddleware` class has been moved to the `Middleware` namespace

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
* A message wrapped in an envelope with stamps (configurations, markers, ...).
1818
*
1919
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
20+
*
21+
* @final
2022
*/
21-
final class Envelope
23+
class Envelope
2224
{
2325
private $stamps = array();
2426
private $message;

src/Symfony/Component/Messenger/MessageBus.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ public function getIterator()
4949
/**
5050
* {@inheritdoc}
5151
*/
52-
public function dispatch($message): void
52+
public function dispatch($message): Envelope
5353
{
5454
if (!\is_object($message)) {
5555
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object, but got %s.', __METHOD__, \gettype($message)));
5656
}
57+
$envelope = $message instanceof Envelope ? $message : new Envelope($message);
5758
$middlewareIterator = $this->middlewareAggregate->getIterator();
5859

5960
while ($middlewareIterator instanceof \IteratorAggregate) {
@@ -62,16 +63,18 @@ public function dispatch($message): void
6263
$middlewareIterator->rewind();
6364

6465
if (!$middlewareIterator->valid()) {
65-
return;
66+
return $envelope;
6667
}
6768
$next = static function (Envelope $envelope) use ($middlewareIterator, &$next) {
6869
$middlewareIterator->next();
6970

7071
if ($middlewareIterator->valid()) {
71-
$middlewareIterator->current()->handle($envelope, $next);
72+
$envelope = $middlewareIterator->current()->handle($envelope, $next);
7273
}
74+
75+
return $envelope;
7376
};
7477

75-
$middlewareIterator->current()->handle($message instanceof Envelope ? $message : new Envelope($message), $next);
78+
return $middlewareIterator->current()->handle($envelope, $next);
7679
}
7780
}

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface MessageBusInterface
2121
*
2222
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
2323
*/
24-
public function dispatch($message): void;
24+
public function dispatch($message): Envelope;
2525
}

src/Symfony/Component/Messenger/Middleware/ActivationMiddleware.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function handle(Envelope $envelope, callable $next): void
38+
public function handle(Envelope $envelope, callable $next): Envelope
3939
{
4040
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
41-
$this->inner->handle($envelope, $next);
41+
return $this->inner->handle($envelope, $next);
4242
} else {
43-
$next($envelope);
43+
return $next($envelope);
4444
}
4545
}
4646
}

src/Symfony/Component/Messenger/Middleware/HandleMessageMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ public function __construct(HandlerLocatorInterface $messageHandlerLocator, bool
3434
*
3535
* @throws NoHandlerForMessageException When no handler is found and $allowNoHandlers is false
3636
*/
37-
public function handle(Envelope $envelope, callable $next): void
37+
public function handle(Envelope $envelope, callable $next): Envelope
3838
{
3939
if (null !== $handler = $this->messageHandlerLocator->getHandler($envelope)) {
4040
$handler($envelope->getMessage());
41-
$next($envelope);
4241
} elseif (!$this->allowNoHandlers) {
4342
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', \get_class($envelope->getMessage())));
4443
}
44+
45+
return $next($envelope);
4546
}
4647
}

src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(LoggerInterface $logger)
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function handle(Envelope $envelope, callable $next): void
32+
public function handle(Envelope $envelope, callable $next): Envelope
3333
{
3434
$message = $envelope->getMessage();
3535
$context = array(
@@ -39,7 +39,7 @@ public function handle(Envelope $envelope, callable $next): void
3939
$this->logger->debug('Starting handling message {name}', $context);
4040

4141
try {
42-
$next($envelope);
42+
$envelope = $next($envelope);
4343
} catch (\Throwable $e) {
4444
$context['exception'] = $e;
4545
$this->logger->warning('An exception occurred while handling message {name}', $context);
@@ -48,5 +48,7 @@ public function handle(Envelope $envelope, callable $next): void
4848
}
4949

5050
$this->logger->debug('Finished handling message {name}', $context);
51+
52+
return $envelope;
5153
}
5254
}

src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ interface MiddlewareInterface
2121
/**
2222
* @param callable|NextInterface $next
2323
*/
24-
public function handle(Envelope $envelope, callable $next): void;
24+
public function handle(Envelope $envelope, callable $next): Envelope;
2525
}
2626

2727
/**
2828
* @internal
2929
*/
3030
interface NextInterface
3131
{
32-
public function __invoke(Envelope $envelope): void;
32+
public function __invoke(Envelope $envelope): Envelope;
3333
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,24 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function handle(Envelope $envelope, callable $next): void
37+
public function handle(Envelope $envelope, callable $next): Envelope
3838
{
3939
if ($envelope->get(ReceivedStamp::class)) {
4040
// It's a received message. Do not send it back:
41-
$next($envelope);
42-
43-
return;
41+
return $next($envelope);
4442
}
4543

4644
$sender = $this->senderLocator->getSender($envelope);
4745

4846
if ($sender) {
49-
$sender->send($envelope);
47+
$envelope = $sender->send($envelope);
5048

5149
if (!AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $envelope)) {
5250
// message has no corresponding handler
53-
return;
51+
return $envelope;
5452
}
5553
}
5654

57-
$next($envelope);
55+
return $next($envelope);
5856
}
5957
}

src/Symfony/Component/Messenger/Middleware/TraceableMiddleware.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, st
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function handle(Envelope $envelope, callable $next): void
40+
public function handle(Envelope $envelope, callable $next): Envelope
4141
{
4242
$class = \get_class($this->inner);
4343
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
@@ -49,10 +49,12 @@ public function handle(Envelope $envelope, callable $next): void
4949
$this->stopwatch->start($eventName, $this->eventCategory);
5050

5151
try {
52-
$this->inner->handle($envelope, function (Envelope $envelope) use ($next, $eventName) {
52+
return $this->inner->handle($envelope, function (Envelope $envelope) use ($next, $eventName) {
5353
$this->stopwatch->stop($eventName);
54-
$next($envelope);
54+
$envelope = $next($envelope);
5555
$this->stopwatch->start($eventName, $this->eventCategory);
56+
57+
return $envelope;
5658
});
5759
} finally {
5860
if ($this->stopwatch->isStarted($eventName)) {

0 commit comments

Comments
 (0)