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

Skip to content

Commit ae46a43

Browse files
[Messenger] make Envelope first class citizen for middleware handlers
1 parent d901c6d commit ae46a43

31 files changed

+140
-232
lines changed

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

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

1414
use Doctrine\Common\Persistence\ManagerRegistry;
1515
use Doctrine\ORM\EntityManagerInterface;
16+
use Symfony\Component\Messenger\Envelope;
1617
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1718

1819
/**
@@ -31,7 +32,10 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
3132
$this->entityManagerName = $entityManagerName;
3233
}
3334

34-
public function handle($message, callable $next): void
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function handle(Envelope $envelope, callable $next): void
3539
{
3640
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);
3741

@@ -41,7 +45,7 @@ public function handle($message, callable $next): void
4145

4246
$entityManager->getConnection()->beginTransaction();
4347
try {
44-
$next($message);
48+
$next($envelope);
4549
$entityManager->flush();
4650
$entityManager->getConnection()->commit();
4751
} catch (\Throwable $exception) {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
use Symfony\Component\Messenger\Asynchronous\Routing\AbstractSenderLocator;
1515
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
1616
use Symfony\Component\Messenger\Envelope;
17-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1817
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1918
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2019

2120
/**
2221
* @author Samuel Roze <[email protected]>
2322
* @author Tobias Schultze <http://tobion.de>
2423
*/
25-
class SendMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
24+
class SendMessageMiddleware implements MiddlewareInterface
2625
{
2726
private $senderLocator;
2827
private $messagesToSendAndHandleMapping;
@@ -34,11 +33,9 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3433
}
3534

3635
/**
37-
* @param Envelope $envelope
38-
*
3936
* {@inheritdoc}
4037
*/
41-
public function handle($envelope, callable $next): void
38+
public function handle(Envelope $envelope, callable $next): void
4239
{
4340
if ($envelope->get(ReceivedStamp::class)) {
4441
// It's a received message. Do not send it back:

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ CHANGELOG
77
* The component is not experimental anymore
88
* All the changes below are BC BREAKS
99
* `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
10+
* `MiddlewareInterface::handle()` now require an `Envelope` as first argument
11+
* `EnvelopeAwareInterface` has been removed
1012
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
1113
`Serializer` as a second argument.
1214
* `SenderLocator` has been renamed to `ContainerSenderLocator`

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ public function __construct($message, StampInterface ...$stamps)
3535
}
3636
}
3737

38-
/**
39-
* Wrap a message into an envelope if not already wrapped.
40-
*
41-
* @param Envelope|object $message
42-
*/
43-
public static function wrap($message): self
44-
{
45-
return $message instanceof self ? $message : new self($message);
46-
}
47-
4838
/**
4939
* @return Envelope a new Envelope instance with additional stamp
5040
*/
@@ -59,15 +49,6 @@ public function with(StampInterface ...$stamps): self
5949
return $cloned;
6050
}
6151

62-
public function withMessage($message): self
63-
{
64-
$cloned = clone $this;
65-
66-
$cloned->message = $message;
67-
68-
return $cloned;
69-
}
70-
7152
public function get(string $stampFqcn): ?StampInterface
7253
{
7354
return $this->stamps[$stampFqcn] ?? null;
@@ -88,15 +69,4 @@ public function getMessage()
8869
{
8970
return $this->message;
9071
}
91-
92-
/**
93-
* @param object $target
94-
*
95-
* @return Envelope|object The original message or the envelope if the target supports it
96-
* (i.e implements {@link EnvelopeAwareInterface}).
97-
*/
98-
public function getMessageFor($target)
99-
{
100-
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
101-
}
10272
}

src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Symfony/Component/Messenger/MessageBus.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,21 @@ public function dispatch($message): void
4444
throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message)));
4545
}
4646

47-
\call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
47+
$this->callableForNextMiddleware(0)($message instanceof Envelope ? $message : new Envelope($message));
4848
}
4949

50-
private function callableForNextMiddleware(int $index, Envelope $currentEnvelope): callable
50+
private function callableForNextMiddleware(int $index): callable
5151
{
5252
if (null === $this->indexedMiddlewareHandlers) {
5353
$this->indexedMiddlewareHandlers = \is_array($this->middlewareHandlers) ? array_values($this->middlewareHandlers) : iterator_to_array($this->middlewareHandlers, false);
5454
}
5555

5656
if (!isset($this->indexedMiddlewareHandlers[$index])) {
57-
return function () {};
57+
return static function () {};
5858
}
5959

60-
$middleware = $this->indexedMiddlewareHandlers[$index];
61-
62-
return function ($message) use ($middleware, $index, $currentEnvelope) {
63-
if ($message instanceof Envelope) {
64-
$currentEnvelope = $message;
65-
} else {
66-
$message = $currentEnvelope->withMessage($message);
67-
}
68-
69-
if (!$middleware instanceof EnvelopeAwareInterface) {
70-
// Do not provide the envelope if the middleware cannot read it:
71-
$message = $message->getMessage();
72-
}
73-
74-
$middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope));
60+
return function (Envelope $envelope) use ($index) {
61+
$this->indexedMiddlewareHandlers[$index]->handle($envelope, $this->callableForNextMiddleware($index + 1));
7562
};
7663
}
7764
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111

1212
namespace Symfony\Component\Messenger\Middleware;
1313

14+
use Symfony\Component\Messenger\Envelope;
1415
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;
1516

1617
/**
1718
* @author Samuel Roze <[email protected]>
1819
*/
1920
class AllowNoHandlerMiddleware implements MiddlewareInterface
2021
{
21-
public function handle($message, callable $next): void
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function handle(Envelope $envelope, callable $next): void
2226
{
2327
try {
24-
$next($message);
28+
$next($envelope);
2529
} catch (NoHandlerForMessageException $e) {
2630
// We allow not having a handler for this message.
2731
}

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

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

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1615
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1716

1817
/**
1918
* Execute the inner middleware according to an activation strategy.
2019
*
2120
* @author Maxime Steinhausser <[email protected]>
2221
*/
23-
class ActivationMiddlewareDecorator implements MiddlewareInterface, EnvelopeAwareInterface
22+
class ActivationMiddlewareDecorator implements MiddlewareInterface
2423
{
2524
private $inner;
2625
private $activated;
@@ -35,12 +34,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
3534
}
3635

3736
/**
38-
* @param Envelope $envelope
37+
* {@inheritdoc}
3938
*/
40-
public function handle($envelope, callable $next): void
39+
public function handle(Envelope $envelope, callable $next): void
4140
{
4241
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
43-
$this->inner->handle($envelope->getMessageFor($this->inner), $next);
42+
$this->inner->handle($envelope, $next);
4443
} else {
4544
$next($envelope);
4645
}

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

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

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1615
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1716
use Symfony\Component\Stopwatch\Stopwatch;
1817

@@ -21,7 +20,7 @@
2120
*
2221
* @author Maxime Steinhausser <[email protected]>
2322
*/
24-
class TraceableMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
23+
class TraceableMiddleware implements MiddlewareInterface
2524
{
2625
private $inner;
2726
private $stopwatch;
@@ -37,9 +36,9 @@ public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, st
3736
}
3837

3938
/**
40-
* @param Envelope $envelope
39+
* {@inheritdoc}
4140
*/
42-
public function handle($envelope, callable $next): void
41+
public function handle(Envelope $envelope, callable $next): void
4342
{
4443
$class = \get_class($this->inner);
4544
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
@@ -51,9 +50,9 @@ public function handle($envelope, callable $next): void
5150
$this->stopwatch->start($eventName, $this->eventCategory);
5251

5352
try {
54-
$this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) {
53+
$this->inner->handle($envelope, function (Envelope $envelope) use ($next, $eventName) {
5554
$this->stopwatch->stop($eventName);
56-
$next($message);
55+
$next($envelope);
5756
$this->stopwatch->start($eventName, $this->eventCategory);
5857
});
5958
} finally {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Messenger\Middleware;
1313

14+
use Symfony\Component\Messenger\Envelope;
1415
use Symfony\Component\Messenger\Handler\Locator\HandlerLocatorInterface;
1516

1617
/**
@@ -28,11 +29,12 @@ public function __construct(HandlerLocatorInterface $messageHandlerResolver)
2829
/**
2930
* {@inheritdoc}
3031
*/
31-
public function handle($message, callable $next): void
32+
public function handle(Envelope $envelope, callable $next): void
3233
{
34+
$message = $envelope->getMessage();
3335
$handler = $this->messageHandlerResolver->resolve($message);
3436
$handler($message);
3537

36-
$next($message);
38+
$next($envelope);
3739
}
3840
}

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

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

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\Messenger\Envelope;
1516

1617
/**
1718
* @author Samuel Roze <[email protected]>
@@ -28,12 +29,13 @@ public function __construct(LoggerInterface $logger)
2829
/**
2930
* {@inheritdoc}
3031
*/
31-
public function handle($message, callable $next): void
32+
public function handle(Envelope $envelope, callable $next): void
3233
{
34+
$message = $envelope->getMessage();
3335
$this->logger->debug('Starting handling message {class}', $this->createContext($message));
3436

3537
try {
36-
$next($message);
38+
$next($envelope);
3739
} catch (\Throwable $e) {
3840
$this->logger->warning('An exception occurred while handling message {class}', array_merge(
3941
$this->createContext($message),

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@
1111

1212
namespace Symfony\Component\Messenger\Middleware;
1313

14+
use Symfony\Component\Messenger\Envelope;
15+
1416
/**
1517
* @author Samuel Roze <[email protected]>
1618
*/
1719
interface MiddlewareInterface
1820
{
1921
/**
20-
* @param object $message
22+
* @param callable|NextInterface $next
2123
*/
22-
public function handle($message, callable $next): void;
24+
public function handle(Envelope $envelope, callable $next): void;
25+
}
26+
27+
/**
28+
* @internal
29+
*/
30+
interface NextInterface
31+
{
32+
public function __invoke(Envelope $envelope): void;
2333
}

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

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

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1615
use Symfony\Component\Messenger\Exception\ValidationFailedException;
1716
use Symfony\Component\Messenger\Stamp\ValidationStamp;
1817
use Symfony\Component\Validator\Validator\ValidatorInterface;
1918

2019
/**
2120
* @author Tobias Nyholm <[email protected]>
2221
*/
23-
class ValidationMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
22+
class ValidationMiddleware implements MiddlewareInterface
2423
{
2524
private $validator;
2625

@@ -30,9 +29,9 @@ public function __construct(ValidatorInterface $validator)
3029
}
3130

3231
/**
33-
* @param Envelope $envelope
32+
* {@inheritdoc}
3433
*/
35-
public function handle($envelope, callable $next): void
34+
public function handle(Envelope $envelope, callable $next): void
3635
{
3736
$message = $envelope->getMessage();
3837
$groups = null;

0 commit comments

Comments
 (0)