12
12
namespace Symfony \Component \Messenger \Middleware ;
13
13
14
14
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 ;
17
17
18
18
/**
19
19
* Allow to configure messages to be handled after the current bus is finished.
@@ -36,33 +36,32 @@ class DispatchAfterCurrentBusMiddleware implements MiddlewareInterface
36
36
private $ queue = [];
37
37
38
38
/**
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.
40
41
*/
41
- private $ isRunning = false ;
42
+ private $ isRootDispatchCallRunning = false ;
42
43
43
44
public function handle (Envelope $ envelope , StackInterface $ stack ): Envelope
44
45
{
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));
48
49
}
49
50
$ this ->queue [] = new QueuedEnvelope ($ envelope , $ stack );
50
51
51
52
return $ envelope ;
52
53
}
53
54
54
- if ($ this ->isRunning ) {
55
+ if ($ this ->isRootDispatchCallRunning ) {
55
56
/*
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.
60
59
*/
61
60
return $ stack ->next ()->handle ($ envelope , $ stack );
62
61
}
63
62
64
63
// First time we get here, mark as inside a "root dispatch" call:
65
- $ this ->isRunning = true ;
64
+ $ this ->isRootDispatchCallRunning = true ;
66
65
try {
67
66
// Execute the whole middleware stack & message handling for main dispatch:
68
67
$ returnedEnvelope = $ stack ->next ()->handle ($ envelope , $ stack );
@@ -74,7 +73,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
74
73
* on the preceding command.
75
74
*/
76
75
$ this ->queue = [];
77
- $ this ->isRunning = false ;
76
+ $ this ->isRootDispatchCallRunning = false ;
78
77
79
78
throw $ exception ;
80
79
}
@@ -91,9 +90,9 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
91
90
}
92
91
}
93
92
94
- $ this ->isRunning = false ;
93
+ $ this ->isRootDispatchCallRunning = false ;
95
94
if (\count ($ exceptions ) > 0 ) {
96
- throw new QueuedMessageHandlingException ($ exceptions );
95
+ throw new DelayedMessageHandlingException ($ exceptions );
97
96
}
98
97
99
98
return $ returnedEnvelope ;
0 commit comments