10000 Minor fixes from Ryan · symfony/symfony@3f7de71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f7de71

Browse files
committed
Minor fixes from Ryan
1 parent d0717e9 commit 3f7de71

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

src/Symfony/Component/Messenger/Exception/QueuedMessageHandlingException.php renamed to src/Symfony/Component/Messenger/Exception/DelayedMessageHandlingException.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@
1717
*
1818
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
1919
*/
20-
class QueuedMessageHandlingException extends \RuntimeException implements ExceptionInterface
20+
class DelayedMessageHandlingException extends \RuntimeException implements ExceptionInterface
2121
{
2222
private $exceptions;
2323

2424
public function __construct(array $exceptions)
2525
{
26-
$message = sprintf(
27-
"Some handlers for queued messages threw an exception: \n\n%s",
28-
implode(", \n", array_map(function (\Throwable $e) {
26+
$exceptionMessages = implode(", \n", array_map(
27+
function (\Throwable $e) {
2928
return \get_class($e).': '.$e->getMessage();
30-
}, $exceptions))
31-
);
29+
},
30+
$exceptions
31+
));
32+
33+
if (count($exceptions) === 1) {
34+
$message = sprintf("A delayed message handler threw an exception: \n\n%s", $exceptionMessages);
35+
} else {
36+
$message = sprintf("Some delayed message handlers threw an exception: \n\n%s", $exceptionMessages);
37+
}
3238

3339
$this->exceptions = $exceptions;
3440

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\Messenger\Middleware;
1313

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\Exception\QueuedMessageHandlingException;
16-
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBus;
15+
use Symfony\Component\Messenger\Exception\DelayedMessageHandlingException;
16+
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
1717

1818
/**
1919
* Allow to configure messages to be handled after the current bus is finished.
@@ -36,33 +36,32 @@ class DispatchAfterCurrentBusMiddleware implements MiddlewareInterface
3636
private $queue = [];
3737

3838
/**
39-
* @var bool Indicates if we are running the middleware or not. I.e, are we called during a dispatch?
39+
* @var bool This property is used to signal if we are inside a the first/root call to
40+
* MessageBusInterface::dispatch() or if dispatch has been called inside a message handler.
4041
*/
41-
private $isRunning = false;
42+
private $isRootDispatchCallRunning = false;
4243

4344
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4445
{
45-
if (null !== $envelope->last(DispatchAfterCurrentBus::class)) {
46-
if (!$this->isRunning) {
47-
throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBus::class));
46+
if (null !== $envelope->last(DispatchAfterCurrentBusStamp::class)) {
47+
if (!$this->isRootDispatchCallRunning) {
48+
throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBusStamp::class));
4849
}
4950
$this->queue[] = new QueuedEnvelope($envelope, $stack);
5051

5152
return $envelope;
5253
}
5354

54-
if ($this->isRunning) {
55+
if ($this->isRootDispatchCallRunning) {
5556
/*
56-
* If we dont have a stamp and a call to MessageBusInterface::dispatch()
57-
* a second time, just continue as normal.
58-
*
59-
* We should not run the stored messages until first call is finished.
57+
* A call to MessageBusInterface::dispatch() was made from inside the main bus handling,
58+
* but the message does not have the stamp. So, process it like normal.
6059
*/
6160
return $stack->next()->handle($envelope, $stack);
6261
}
6362

6463
// First time we get here, mark as inside a "root dispatch" call:
65-
$this->isRunning = true;
64+
$this->isRootDispatchCallRunning = true;
6665
try {
6766
// Execute the whole middleware stack & message handling for main dispatch:
6867
$returnedEnvelope = $stack->next()->handle($envelope, $stack);
@@ -74,7 +73,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
7473
* on the preceding command.
7574
*/
7675
$this->queue = [];
77-
$this->isRunning = false;
76+
$this->isRootDispatchCallRunning = false;
7877

7978
throw $exception;
8079
}
@@ -91,9 +90,9 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
9190
}
9291
}
9392

94-
$this->isRunning = false;
93+
$this->isRootDispatchCallRunning = false;
9594
if (\count($exceptions) > 0) {
96-
throw new QueuedMessageHandlingException($exceptions);
95+
throw new DelayedMessageHandlingException($exceptions);
9796
}
9897

9998
return $returnedEnvelope;

src/Symfony/Component/Messenger/Stamp/DispatchAfterCurrentBus.php renamed to src/Symfony/Component/Messenger/Stamp/DispatchAfterCurrentBusStamp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
*
2121
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2222
*/
23-
class DispatchAfterCurrentBus implements StampInterface
23+
class DispatchAfterCurrentBusStamp implements StampInterface
2424
{
2525
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\Messenger\Envelope;
17-
use Symfony\Component\Messenger\Exception\QueuedMessageHandlingException;
17+
use Symfony\Component\Messenger\Exception\DelayedMessageHandlingException;
1818
use Symfony\Component\Messenger\MessageBus;
1919
use Symfony\Component\Messenger\MessageBusInterface;
2020
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
2121
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
2222
use Symfony\Component\Messenger\Middleware\StackInterface;
23-
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBus;
23+
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
2424
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2525

2626
class DispatchAfterCurrentBusMiddlewareTest extends TestCase
@@ -44,8 +44,8 @@ public function testEventsInNewTransactionAreHandledAfterMainMessage()
4444
$messageBus = new MessageBus([
4545
$middleware,
4646
new DispatchingMiddleware($eventBus, [
47-
new Envelope($firstEvent, new DispatchAfterCurrentBus()),
48-
new Envelope($secondEvent, new DispatchAfterCurrentBus()),
47+
new Envelope($firstEvent, new DispatchAfterCurrentBusStamp()),
48+
new Envelope($secondEvent, new DispatchAfterCurrentBusStamp()),
4949
$thirdEvent, // Not in a new transaction
5050
]),
5151
$handlingMiddleware,
@@ -80,8 +80,8 @@ public function testThrowingEventsHandlingWontStopExecution()
8080
$messageBus = new MessageBus([
8181
$middleware,
8282
new DispatchingMiddleware($eventBus, [
83-
new Envelope($firstEvent, new DispatchAfterCurrentBus()),
84-
new Envelope($secondEvent, new DispatchAfterCurrentBus()),
83+
new Envelope($firstEvent, new DispatchAfterCurrentBusStamp()),
84+
new Envelope($secondEvent, new DispatchAfterCurrentBusStamp()),
8585
]),
8686
$handlingMiddleware,
8787
]);
@@ -93,7 +93,7 @@ public function testThrowingEventsHandlingWontStopExecution()
9393
// Next event is still handled despite the previous exception:
9494
$this->expectHandledMessage($handlingMiddleware, 2, $secondEvent);
9595

96-
$this->expectException(QueuedMessageHandlingException::class);
96+
$this->expectException(DelayedMessageHandlingException::class);
9797
$this->expectExceptionMessage('RuntimeException: Some exception while handling first event');
9898

9999
$messageBus->dispatch($message);

0 commit comments

Comments
 (0)
0