E575 [Messenger] Add `NextInterface`, allowing to unstack the call stack · symfony/symfony@c3002e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3002e0

Browse files
[Messenger] Add NextInterface, allowing to unstack the call stack
1 parent 9aaec94 commit c3002e0

21 files changed

+210
-118
lines changed

src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function handle(Envelope $envelope, callable $next): void
4545

4646
$entityManager->getConnection()->beginTransaction();
4747
try {
48-
$next($envelope);
48+
$next()->handle($envelope, $next);
4949
$entityManager->flush();
5050
$entityManager->getConnection()->commit();
5151
} catch (\Throwable $exception) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function handle(Envelope $envelope, callable $next): void
3939
{
4040
if ($envelope->get(ReceivedStamp::class)) {
4141
// It's a received message. Do not send it back:
42-
$next($envelope);
42+
$next()->handle($envelope, $next);
4343

4444
return;
4545
}
@@ -55,6 +55,6 @@ public function handle(Envelope $envelope, callable $next): void
5555
}
5656
}
5757

58-
$next($envelope);
58+
$next()->handle($envelope, $next);
5959
}
6060
}

src/Symfony/Component/Messenger/MessageBus.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger;
1313

1414
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
15+
use Symfony\Component\Messenger\Middleware\NextMiddleware;
1516

1617
/**
1718
* @author Samuel Roze <samuel.roze@gmail.com>
@@ -64,13 +65,7 @@ public function dispatch($message): void
6465
if (!$middlewareIterator->valid()) {
6566
return;
6667
}
67-
$next = static function (Envelope $envelope) use ($middlewareIterator, &$next) {
68-
$middlewareIterator->next();
69-
70-
if ($middlewareIterator->valid()) {
71-
$middlewareIterator->current()->handle($envelope, $next);
72-
}
73-
};
68+
$next = new NextMiddleware($middlewareIterator);
7469

7570
$middlewareIterator->current()->handle($message instanceof Envelope ? $message : new Envelope($message), $next);
7671
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AllowNoHandlerMiddleware implements MiddlewareInterface
2525
public function handle(Envelope $envelope, callable $next): void
2626
{
2727
try {
28-
$next($envelope);
28+
$next()->handle($envelope, $next);
2929
} catch (NoHandlerForMessageException $e) {
3030
// We allow not having a handler for this message.
3131
}

src/Symfony/Component/Messenger/Middleware/Enhancers/ActivationMiddlewareDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function handle(Envelope $envelope, callable $next): void
4141
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
4242
$this->inner->handle($envelope, $next);
4343
} else {
44-
$next($envelope);
44+
$next()->handle($envelope, $next);
4545
}
4646
}
4747
}

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

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

1414
use Symfony\Component\Messenger\Envelope;
1515
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
16+
use Symfony\Component\Messenger\Middleware\NextInterface;
1617
use Symfony\Component\Stopwatch\Stopwatch;
1718

1819
/**
@@ -50,15 +51,58 @@ public function handle(Envelope $envelope, callable $next): void
5051
$this->stopwatch->start($eventName, $this->eventCategory);
5152

5253
try {
53-
$this->inner->handle($envelope, function (Envelope $envelope) use ($next, $eventName) {
54-
$this->stopwatch->stop($eventName);
55-
$next($envelope);
56-
$this->stopwatch->start($eventName, $this->eventCategory);
57-
});
54+
$this->inner->handle($envelope, new TraceableInnerMiddleware($next, $this->stopwatch, $eventName, $this->eventCategory));
5855
} finally {
5956
if ($this->stopwatch->isStarted($eventName)) {
6057
$this->stopwatch->stop($eventName);
6158
}
6259
}
6360
}
6461
}
62+
63+
/**
64+
* @internal
65+
*/
66+
class TraceableInnerMiddleware implements MiddlewareInterface, NextInterface
67+
{
68+
private $next;
69+
private $stopwatch;
70+
private $eventName;
71+
private $eventCategory;
72+
73+
public function __construct(callable $next, Stopwatch $stopwatch, string $eventName, string $eventCategory)
74+
{
75+
$this->next = $next;
76+
$this->stopwatch = $stopwatch;
77+
$this->eventName = $eventName;
78+
$this->eventCategory = $eventCategory;
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function handle(Envelope $envelope, callable $next): void
85+
{
86+
$this->stopwatch->stop($this->eventName);
87+
if ($this === $next) {
88+
($this->next)()->handle($envelope, $this->next);
89+
} else {
90+
$next()->handle($envelope, $next);
91+
}
92+
$this->stopwatch->start($this->eventName, $this-& C02E gt;eventCategory);
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
public function __invoke(Envelope $envelope = null): MiddlewareInterface
99+
{
100+
if (null !== $envelope) {
101+
$this->stopwatch->stop($this->eventName);
102+
($this->next)()->handle($envelope, $this->next);
103+
$this->stopwatch->start($this->eventName, $this->eventCategory);
104+
}
105+
106+
return $this;
107+
}
108+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public function handle(Envelope $envelope, callable $next): void
3333
{
3434
$handler = $this->messageHandlerLocator->getHandler($envelope);
3535
$handler($envelope->getMessage());
36-
$next($envelope);
36+
$next()->handle($envelope, $next);
3737
}
3838
}

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

Lines changed: 1 addition & 1 deletion
4B92
Original file line numberDiff line numberDiff line change
@@ -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+
$next()->handle($envelope, $next);
4343
} catch (\Throwable $e) {
4444
$context['exception'] = $e;
4545
$this->logger->warning('An exception occurred while handling message {name}', $context);

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,3 @@ interface MiddlewareInterface
2323
*/
2424
public function handle(Envelope $envelope, callable $next): void;
2525
}
26-
27-
/**
28-
* @internal
29-
*/
30-
interface NextInterface
31-
{
32-
public function __invoke(Envelope $envelope): void;
33-
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Middleware;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
16+
/**
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
interface NextInterface
20+
{
21+
/**
22+
* Calls or returns the next middleware to process a message.
23+
*
24+
* @param Envelope|null When an envelope is passed, it should be forwarded to the next middleware's handle() method
25+
*
26+
* @return MiddlewareInterface The next middleware in the chain
27+
*/
28+
public function __invoke(Envelope $envelope = null): MiddlewareInterface;
29+
}

0 commit comments

Comments
 (0)
0