8000 [Messenger] make Envelope first class citizen for middleware handlers · symfony/symfony@23527d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23527d2

Browse files
[Messenger] make Envelope first class citizen for middleware handlers
1 parent 53cecc4 commit 23527d2

20 files changed

+95
-164
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numbe 6D40 rDiff 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
@@ -15,14 +15,13 @@
1515
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
1616
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
1717
use Symfony\Component\Messenger\Envelope;
18-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1918
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
2019

2120
/**
2221
* @author Samuel Roze <samuel.roze@gmail.com>
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(ReceivedMessage::class)) {
4441
// It's a received message. Do not send it back:

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ CHANGELOG
66

77
* The component is not experimental anymore
88
* [BC BREAK] `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
9+
* [BC BREAK] `MiddlewareInterface::handle()` now require an `Envelope` as first argument
10+
* [BC BREAK] `EnvelopeAwareInterface` has been removed
911
* [BC BREAK] The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
1012
`Serializer` as a second argument.
1113
* [BC BREAK] `SenderLocator` has been renamed to `ContainerSenderLocator`
1214
Be careful as there is still a `SenderLocator` class, but it does not rely on a `ContainerInterface` to find senders.
1315
Instead, it accepts the sender instance itself instead of its identifier in the container.
1416
* [BC BREAK] `MessageSubscriberInterface::getHandledMessages()` return value has changed. The value of an array item
1517
needs to be an associative array or the method name.
16-
* `ValidationMiddleware::handle()` and `SendMessageMiddleware::handle()` now require an `Envelope` object
1718
* `EnvelopeItemInterface` doesn't extend `Serializable` anymore
1819
* [BC BREAK] The `ConsumeMessagesCommand` class now takes an instance of `Psr\Container\ContainerInterface`
1920
as first constructor argument

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,4 @@ public function getMessage()
8484
{
8585
return $this->message;
8686
}
87-
88-
/**
89-
* @param object $target
90-
*
91-
* @return Envelope|object The original message or the envelope if the target supports it
92-
* (i.e implements {@link EnvelopeAwareInterface}).
93-
*/
94-
public function getMessageFor($target)
95-
{
96-
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
97-
}
9887
}

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)(Envelope::wrap($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 <samuel.roze@gmail.com>
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 <maxime.steinhausser@gmail.com>
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 <maxime.steinhausser@gmail.com>
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 <samuel.roze@gmail.com>
@@ -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 <samuel.roze@gmail.com>
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\Middleware\Configuration\ValidationConfiguration;
1817
use Symfony\Component\Validator\Validator\ValidatorInterface;
1918

2019
/**
2120
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
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;

src/Symfony/Component/Messenger/Tests/EnvelopeTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
1616
use Symfony\Component\Messenger\Envelope;
17-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1817
use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration;
1918
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2019

@@ -76,7 +75,3 @@ public function testAll()
7675
$this->assertSame($validationConfig, $configs[ValidationConfiguration::class]);
7776
}
7877
}
79-
80-
class FooConfigurationConsumer implements EnvelopeAwareInterface
81-
{
82-
}

0 commit comments

Comments
 (0)
0