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

Skip to content

Commit 4b0e015

Browse files
[Messenger] make dispatch(), handle() and send() methods return Envelope
1 parent 08eaf18 commit 4b0e015

29 files changed

+92
-68
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 and a `StackInterface` as second
1111
* `EnvelopeAwareInterface` has been removed
1212
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
@@ -31,7 +31,6 @@ CHANGELOG
3131
* `AbstractHandlerLocator` is now internal
3232
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(Envelope $envelope): ?callable` and shouldn't throw when no handlers are found
3333
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
34-
* `SenderInterface::send()` returns `void`
3534
* Classes in the `Middleware\Enhancers` sub-namespace have been moved to the `Middleware` one
3635
* Classes in the `Asynchronous\Routing` sub-namespace have been moved to the `Transport\Sender\Locator` sub-namespace
3736
* The `Asynchronous/Middleware/SendMessageMiddleware` class has been moved to the `Middleware` namespace

src/Symfony/Component/Messenger/MessageBus.php

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

6061
while ($middlewareIterator instanceof \IteratorAggregate) {
@@ -63,10 +64,10 @@ public function dispatch($message): void
6364
$middlewareIterator->rewind();
6465

6566
if (!$middlewareIterator->valid()) {
66-
return;
67+
return $envelope;
6768
}
6869
$stack = new StackMiddleware($middlewareIterator);
6970

70-
$middlewareIterator->current()->handle($message instanceof Envelope ? $message : new Envelope($message), $stack);
71+
return $middlewareIterator->current()->handle($envelope, $stack);
7172
}
7273
}

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: 4 additions & 4 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, StackInterface $stack): void
38+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3939
{
4040
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
41-
$this->inner->handle($envelope, $stack);
42-
} else {
43-
$stack->next()->handle($envelope, $stack);
41+
return $this->inner->handle($envelope, $stack);
4442
}
43+
44+
return $stack->next()->handle($envelope, $stack);
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, StackInterface $stack): void
37+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3838
{
3939
if (null !== $handler = $this->messageHandlerLocator->getHandler($envelope)) {
4040
$handler($envelope->getMessage());
41-
$stack->next()->handle($envelope, $stack);
4241
} elseif (!$this->allowNoHandlers) {
4342
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', \get_class($envelope->getMessage())));
4443
}
44+
45+
return $stack->next()->handle($envelope, $stack);
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, StackInterface $stack): void
32+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3333
{
3434
$message = $envelope->getMessage();
3535
$context = array(
@@ -39,7 +39,7 @@ public function handle(Envelope $envelope, StackInterface $stack): void
3939
$this->logger->debug('Starting handling message {name}', $context);
4040

4141
try {
42-
$stack->next()->handle($envelope, $stack);
42+
$envelope = $stack->next()->handle($envelope, $stack);
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, StackInterface $stack): 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
*/
1919
interface MiddlewareInterface
2020
{
21-
public function handle(Envelope $envelope, StackInterface $stack): void;
21+
public function handle(Envelope $envelope, StackInterface $stack): Envelope;
2222
}

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, StackInterface $stack): void
37+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3838
{
3939
if ($envelope->get(ReceivedStamp::class)) {
4040
// It's a received message. Do not send it back:
41-
$stack->next()->handle($envelope, $stack);
42-
43-
return;
41+
return $stack->next()->handle($envelope, $stack);
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-
$stack->next()->handle($envelope, $stack);
55+
return $stack->next()->handle($envelope, $stack);
5856
}
5957
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function next(): MiddlewareInterface
4141
return $iterator->current();
4242
}
4343

44-
public function handle(Envelope $envelope, StackInterface $stack): void
44+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4545
{
46-
// no-op: this is the last null middleware
46+
return $envelope;
4747
}
4848
}

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

Lines changed: 7 additions & 5 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, StackInterface $stack): void
40+
public function handle(Envelope $envelope, StackInterface $stack): 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,7 +49,7 @@ public function handle(Envelope $envelope, StackInterface $stack): void
4949
$this->stopwatch->start($eventName, $this->eventCategory);
5050

5151
try {
52-
$this->inner->handle($envelope, new TraceableInnerMiddleware($stack, $this->stopwatch, $eventName, $this->eventCategory));
52+
return $this->inner->handle($envelope, new TraceableInnerMiddleware($stack, $this->stopwatch, $eventName, $this->eventCategory));
5353
} finally {
5454
if ($this->stopwatch->isStarted($eventName)) {
5555
$this->stopwatch->stop($eventName);
@@ -79,15 +79,17 @@ public function __construct(StackInterface $stack, Stopwatch $stopwatch, string
7979
/**
8080
* {@inheritdoc}
8181
*/
82-
public function handle(Envelope $envelope, StackInterface $stack): void
82+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
8383
{
8484
$this->stopwatch->stop($this->eventName);
8585
if ($this === $stack) {
86-
$this->stack->next()->handle($envelope, $this->stack);
86+
$envelope = $this->stack->next()->handle($envelope, $this->stack);
8787
} else {
88-
$stack->next()->handle($envelope, $stack);
88+
$envelope = $stack->next()->handle($envelope, $stack);
8989
}
9090
$this->stopwatch->start($this->eventName, $this->eventCategory);
91+
92+
return $envelope;
9193
}
9294

9395
/**

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, StackInterface $stack): void
34+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3535
{
3636
$message = $envelope->getMessage();
3737
$groups = null;
@@ -45,6 +45,6 @@ public function handle(Envelope $envelope, StackInterface $stack): void
4545
throw new ValidationFailedException($message, $violations);
4646
}
4747

48-
$stack->next()->handle($envelope, $stack);
48+
return $stack->next()->handle($envelope, $stack);
4949
}
5050
}

src/Symfony/Component/Messenger/Tests/DataCollector/MessengerDataCollectorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
16+
use Symfony\Component\Messenger\Envelope;
1617
use Symfony\Component\Messenger\MessageBusInterface;
1718
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1819
use Symfony\Component\Messenger\TraceableMessageBus;
@@ -36,9 +37,10 @@ protected function setUp()
3637
public function testHandle()
3738
{
3839
$message = new DummyMessage('dummy message');
40+
$envelope = new Envelope($message);
3941

4042
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
41-
$bus->method('dispatch')->with($message);
43+
$bus->method('dispatch')->with($message)->willReturn($envelope);
4244
$bus = new TraceableMessageBus($bus);
4345

4446
$collector = new MessengerDataCollector();
@@ -124,9 +126,11 @@ public function testHandleWithException()
124126
public function testKeepsOrderedDispatchCalls()
125127
{
126128
$firstBus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
129+
$firstBus->method('dispatch')->willReturn(new Envelope(new \stdClass()));
127130
$firstBus = new TraceableMessageBus($firstBus);
128131

129132
$secondBus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
133+
$secondBus->method('dispatch')->willReturn(new Envelope(new \stdClass()));
130134
$secondBus = new TraceableMessageBus($secondBus);
131135

132136
$collector = new MessengerDataCollector();

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

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

848848
class UselessMiddleware implements MiddlewareInterface
849849
{
850-
public function handle(Envelope $message, StackInterface $stack): void
850+
public function handle(Envelope $message, StackInterface $stack): Envelope
851851
{
852-
$stack->next()->handle($message, $stack);
852+
return $stack->next()->handle($message, $stack);
853853
}
854854
}

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, $stack) {
52-
$stack->next()->handle($envelope, $stack);
52+
return $stack->next()->handle($envelope, $stack);
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, $stack) {
80-
$stack->next()->handle($envelope->with(new AnEnvelopeStamp()), $stack);
81+
return $stack->next()->handle($envelope->with(new AnEnvelopeStamp()), $stack);
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, $stack) {
88-
$stack->next()->handle($envelope, $stack);
89+
return $stack->next()->handle($envelope, $stack);
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 ($envelope, $stack) use ($expectedEnvelope) {
119-
$stack->next()->handle($expectedEnvelope, $stack);
121+
return $stack->next()->handle($expectedEnvelope, $stack);
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testExecuteMiddlewareOnActivated()
3131
$stack->expects($this->never())->method('next');
3232

3333
$middleware = $this->createMock(MiddlewareInterface::class);
34-
$middleware->expects($this->once())->method('handle')->with($envelope, $stack);
34+
$middleware->expects($this->once())->method('handle')->with($envelope, $stack)->willReturn($envelope);
3535

3636
$decorator = new ActivationMiddleware($middleware, true);
3737

@@ -50,7 +50,7 @@ public function testExecuteMiddlewareOnActivatedWithCallable()
5050
$stack->expects($this->never())->method('next');
5151

5252
$middleware = $this->createMock(MiddlewareInterface::class);
53-
$middleware->expects($this->once())->method('handle')->with($envelope, $stack);
53+
$middleware->expects($this->once())->method('handle')->with($envelope, $stack)->willReturn($envelope);
5454

5555
$decorator = new ActivationMiddleware($middleware, $activated);
5656

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ public function testAllowNoHandlers()
5050
{
5151
$middleware = new HandleMessageMiddleware(new HandlerLocator(array()), true);
5252

53-
$this->assertNull($middleware->handle(new Envelope(new DummyMessage('Hey')), new StackMiddleware()));
53+
$this->assertInstanceOf(Envelope::class, $middleware->handle(new Envelope(new DummyMessage('Hey')), new StackMiddleware()));
5454
}
5555
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ protected function getStackMock(bool $nextIsCalled = true)
2323
$nextMiddleware
2424
->expects($nextIsCalled ? $this->once() : $this->never())
2525
->method('handle')
26+
->will($this->returnCallback(function ($envelope, StackInterface $stack) {
27+
return $envelope;
28+
}))
2629
;
2730

2831
$stack = $this->createMock(StackInterface::class);

0 commit comments

Comments
 (0)