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

Skip to content

Commit 43ce7dd

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

28 files changed

+89
-64
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\HttpFoundation\StreamedResponse;
2828
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2929
use Symfony\Component\HttpKernel\HttpKernelInterface;
30+
use Symfony\Component\Messenger\Envelope;
3031
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
3132
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
3233
use Symfony\Component\Security\Csrf\CsrfToken;
@@ -394,7 +395,7 @@ protected function isCsrfTokenValid(string $id, ?string $token): bool
394395
*
395396
* @final
396397
*/
397-
protected function dispatchMessage($message)
398+
protected function dispatchMessage($message): Envelope
398399
{
399400
if (!$this->container->has('message_bus')) {
400401
throw new \LogicException('The message bus is not enabled in your application. Try running "composer require symfony/messenger".');

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 <[email protected]>
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)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(ValidatorInterface $validator)
3131
/**
3232
* {@inheritdoc}
3333
*/
34-
public function handle(Envelope $envelope, callable $next): void
34+
public function handle(Envelope $envelope, callable $next): Envelope
3535
{
3636
$message = $envelope->getMessage();
3737
$groups = null;
@@ -45,6 +45,6 @@ public function handle(Envelope $envelope, callable $next): void
4545
throw new ValidationFailedException($message, $violations);
4646
}
4747

48-
$next($envelope);
48+
return $next($envelope);
4949
}
5050
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,8 @@ public function dummyMethodForSomeBus()
846846

847847
class UselessMiddleware implements MiddlewareInterface
848848
{
849-
public function handle(Envelope $message, callable $next): void
849+
public function handle(Envelope $message, callable $next): Envelope
850850
{
851-
$next($message);
851+
return $next($message);
852852
}
853853
}

src/Symfony/Component/Messenger/Tests/MessageBusTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ public function testItCallsMiddleware()
4949
->method('handle')
5050
->with($envelope, $this->anything())
5151
->will($this->returnCallback(function ($envelope, $next) {
52-
$next($envelope);
52+
return $next($envelope);
5353
}));
5454

5555
$secondMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();
5656
$secondMiddleware->expects($this->once())
5757
->method('handle')
5858
->with($envelope, $this->anything())
59+
->willReturn($envelope)
5960
;
6061

6162
$bus = new MessageBus(array(
@@ -77,21 +78,22 @@ public function testThatAMiddlewareCanAddSomeStampsToTheEnvelope()
7778
->method('handle')
7879
->with($envelope, $this->anything())
7980
->will($this->returnCallback(function ($envelope, $next) {
80-
$next($envelope->with(new AnEnvelopeStamp()));
81+
return $next($envelope->with(new AnEnvelopeStamp()));
8182
}));
8283

8384
$secondMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();
8485
$secondMiddleware->expects($this->once())
8586
->method('handle')
8687
->with($envelopeWithAnotherStamp, $this->anything())
8788
->will($this->returnCallback(function ($envelope, $next) {
88-
$next($envelope);
89+
return $next($envelope);
8990
}));
9091

9192
$thirdMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();
9293
$thirdMiddleware->expects($this->once())
9394
->method('handle')
9495
->with($envelopeWithAnotherStamp, $this->anything())
96+
->willReturn($envelopeWithAnotherStamp)
9597
;
9698

9799
$bus = new MessageBus(array(
@@ -116,13 +118,14 @@ public function testThatAMiddlewareCanUpdateTheMessageWhileKeepingTheEnvelopeSta
116118
->method('handle')
117119
->with($envelope, $this->anything())
118120
->will($this->returnCallback(function ($message, $next) use ($expectedEnvelope) {
119-
$next($expectedEnvelope);
121+
return $next($expectedEnvelope);
120122
}));
121123

122124
$secondMiddleware = $this->getMockBuilder(MiddlewareInterface::class)->getMock();
123125
$secondMiddleware->expects($this->once())
124126
->method('handle')
125127
->with($expectedEnvelope, $this->anything())
128+
->willReturn($envelope)
126129
;
127130

128131
$bus = new MessageBus(array(

src/Symfony/Component/Messenger/Tests/Middleware/ActivationMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testExecuteMiddlewareOnDeactivated()
6363
$envelope = new Envelope($message);
6464

6565
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
66-
$next->expects($this->once())->method('__invoke')->with($envelope);
66+
$next->expects($this->once())->method('__invoke')->with($envelope)->willReturn($envelope);
6767

6868
$middleware = $this->createMock(MiddlewareInterface::class);
6969
$middleware->expects($this->never())->method('handle');

src/Symfony/Component/Messenger/Tests/Middleware/HandleMessageMiddlewareTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testItCallsTheHandlerAndNextMiddleware()
3333
)));
3434

3535
$handler->expects($this->once())->method('__invoke')->with($message);
36-
$next->expects($this->once())->method('__invoke')->with($envelope);
36+
$next->expects($this->once())->method('__invoke')->with($envelope)->willReturn($envelope);
3737

3838
$middleware->handle($envelope, $next);
3939
}
@@ -53,6 +53,6 @@ public function testAllowNoHandlers()
5353
{
5454
$middleware = new HandleMessageMiddleware(new HandlerLocator(array()), true);
5555

56-
$this->assertNull($middleware->handle(new Envelope(new DummyMessage('Hey')), function () {}));
56+
$this->assertInstanceOf(Envelope::class, $middleware->handle(new Envelope(new DummyMessage('Hey')), function ($envelope) { return $envelope; }));
5757
}
5858
}

src/Symfony/Component/Messenger/Tests/Middleware/LoggingMiddlewareTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function testDebugLogAndNextMiddleware()
3434
->expects($this->once())
3535
->method('__invoke')
3636
->with($envelope)
37+
->willReturn($envelope)
3738
;
3839

3940
(new LoggingMiddleware($logger))->handle($envelope, $next);

0 commit comments

Comments
 (0)