10000 [Messenger] use Envelope internally, return void, add EnvelopeHandler… · symfony/symfony@f80cbca · GitHub
[go: up one dir, main page]

Skip to content

Commit f80cbca

Browse files
[Messenger] use Envelope internally, return void, add EnvelopeHandlerInterface and other cleanups
1 parent a14576c commit f80cbca

20 files changed

+102
-139
lines changed

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/Asynchronous/Routing/AbstractSenderLocator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ abstract class AbstractSenderLocator implements SenderLocatorInterface
2222
{
2323
public static function getValueFromMessageRouting(array $mapping, Envelope $envelope)
2424
{
25+
$name = $envelope->getMessageName();
26+
27+
if (null !== F438 $name && isset($mapping[$name])) {
28+
return $mapping[$name];
29+
}
30+
2531
if (isset($mapping[$class = \get_class($envelope->getMessage())])) {
2632
return $mapping[$class];
2733
}

src/Symfony/Component/Messenger/Asynchronous/Routing/SenderLocator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getSender(Envelope $envelope): ?SenderInterface
3838
}
3939

4040
if (!$sender instanceof SenderInterface) {
41-
throw new RuntimeException(sprintf('The sender instance provided for message "%s" should be of type "%s" but got "%s".', \get_class($envelope->getMessage()), SenderInterface::class, \is_object($sender) ? \get_class($sender) : \gettype($sender)));
41+
throw new RuntimeException(sprintf('The sender instance provided for message "%s" should be of type "%s" but got "%s".', $envelope->getMessageName() ?? \get_class($envelope->getMessage()), SenderInterface::class, \is_object($sender) ? \get_class($sender) : \gettype($sender)));
4242
}
4343

4444
return $sender;

src/Symfony/Component/Messenger/Envelope.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ public function __construct($message, array $items = array())
4141
*
4242
* @param Envelope|object $message
4343
*/
44-
public static function wrap($message): self
44+
public static function wrap($message, string $name = null): self
4545
{
46-
return $message instanceof self ? $message : new self($message);
46+
$envelope = $message instanceof self ? clone $message : new self($message);
47+
if (null !== $name) {
48+
return $envelope->with(new MessageConfiguration($name));
49+
}
50+
unset($envelope->items[MessageConfiguration::class]);
51+
52+
return $envelope;
4753
}
4854

4955
/**
@@ -58,15 +64,6 @@ public function with(EnvelopeItemInterface $item): self
5864
return $cloned;
5965
}
6066

61-
public function withMessage($message): self
62-
{
63-
$cloned = clone $this;
64-
65-
$cloned->message = $message;
66-
67-
return $cloned;
68-
}
69-
7067
public function get(string $itemFqcn): ?EnvelopeItemInterface
7168
{
7269
return $this->items[$itemFqcn] ?? null;
@@ -88,14 +85,10 @@ public function getMessage()
8885
return $this->message;
8986
}
9087

91-
/**
92-
* @param object $target
93-
*
94-
* @return Envelope|object The original message or the envelope if the target supports it
95-
* (i.e implements {@link EnvelopeAwareInterface}).
96-
*/
97-
public function getMessageFor($target)
88+
public function getMessageName(): ?string
9889
{
99-
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
90+
$config = $this->items[MessageConfiguration::class] ?? null;
91+
92+
return $config ? $config->getName() : null;
10093
}
10194
}

src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

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

src/Symfony/Component/Messenger/Handler/ChainHandler.php

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

1212
namespace Symfony\Component\Messenger\Handler;
1313

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

1617
/**
@@ -37,8 +38,10 @@ public function __construct(array $handlers)
3738
$this->handlers = $handlers;
3839
}
3940

40-
public function __invoke($message)
41+
public function __invoke(Envelope $envelope)
4142
{
43+
$message = $envelope->getMessage();
44+
4245
foreach ($this->handlers as $handler) {
4346
$handler($message);
4447
}

src/Symfony/Component/Messenger/Handler/Locator/AbstractHandlerLocator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ abstract class AbstractHandlerLocator implements HandlerLocatorInterface
2424
{
2525
public function getHandler(Envelope $envelope, bool $allowNoHandler = false): ?callable
2626
{
27+
$name = $envelope->ge F438 tMessageName();
28+
29+
if (null !== $name && $handler = $this->getHandlerByName($name)) {
30+
return $handler;
31+
}
2732
$class = \get_class($envelope->getMessage());
2833

2934
if ($handler = $this->getHandlerByName($class)) {

src/Symfony/Component/Messenger/MessageBus.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(iterable $middlewareHandlers = array())
3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function dispatch($message): void
42+
public function dispatch($message, string $name = null): void
4343
{
4444
if (!\is_object($message)) {
4545
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object, but got %s.', __METHOD__, \gettype($message)));
@@ -48,34 +48,15 @@ public function dispatch($message): void
4848
$middlewareIterator = $this->middlewareAggregate->getIterator();
4949

5050
foreach ($middlewareIterator as $middleware) {
51-
$currentEnvelope = Envelope::wrap($message);
52-
53-
// Do not provide the envelope if the middleware cannot read it:
54-
$message = $middleware instanceof EnvelopeAwareInterface ? $currentEnvelope : $currentEnvelope->getMessage();
55-
56-
$next = static function ($message) use ($middlewareIterator, &$currentEnvelope, &$next) {
51+
$next = static function ($envelope) use ($middlewareIterator, &$next) {
5752
$middlewareIterator->next();
5853

59-
if (!$middlewareIterator->valid()) {
60-
return;
61-
}
62-
63-
$middleware = $middlewareIterator->current();
64-
65-
if ($message instanceof Envelope) {
66-
$currentEnvelope = $message;
67-
} else {
68-
$message = $currentEnvelope->withMessage($message);
69-
}
70-
71-
if (!$middleware instanceof EnvelopeAwareInterface) {
72-
$message = $message->getMessage();
54+
if ($middlewareIterator->valid()) {
55+
$middlewareIterator->current()->handle($envelope, $next);
7356
}
74-
75-
$middleware->handle($message, $next);
7657
};
7758

78-
$middleware->handle($message, $next);
59+
$middleware->handle(Envelope::wrap($message), $next);
7960
}
8061
}
8162
}

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface MessageBusInterface
2020
* Dispatches the given message.
2121
*
2222
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
23+
* @param string|null $name The name to use as dispatching key; when not provided,
24+
* this name is derived from the type of the message
2325
*/
24-
public function dispatch($message): void;
26+
public function dispatch($message, string $name = null): void;
2527
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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;
13+
14+
/**
15+
* @author Nicolas Grekas <p@tchwork.com>
16+
*/
17+
final class MessageConfiguration implements EnvelopeItemInterface
18+
{
19+
private $name;
20+
21+
public function __construct(string $name)
22+
{
23+
$this->name = $name;
24+
}
25+
26+
public function getName(): string
27+
{
28+
return $this->name;
29+
}
30+
}

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

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

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/Enh 10000 ancers/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, < D7AE span class="pl-s1">$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: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,26 @@
1212
namespace Symfony\Component\Messenger\Middleware;
1313

1414
use Symfony\Component\Messenger\Envelope;
15-
use Symfony\Component\Messenger\EnvelopeAwareInterface;
1615
use Symfony\Component\Messenger\Handler\Locator\HandlerLocatorInterface;
1716

1817
/**
1918
* @author Samuel Roze <samuel.roze@gmail.com>
2019
*/
21-
class HandleMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
20+
class HandleMessageMiddleware implements MiddlewareInterface
2221
{
2322
private $messageHandlerLocator;
2423

25-
public function __construct(HandlerLocatorInterface $messageHandlerLocator)
24+
public function __construct(HandlerLocatorInterface $messageHandlerLocator, bool $allowNoHandler = false)
2625
{
2726
$this->messageHandlerLocator = $messageHandlerLocator;
27+
$this->allowNoHandler = $allowNoHandler;
2828
}
2929

30-
/**
31-
* @param Envelope $envelope
32-
*/
33-
public function handle($envelope, callable $next): void
30+
public function handle(Envelope $envelope, callable $next): void
3431
{
35-
$handler = $this->messageHandlerLocator->getHandler($envelope);
36-
$handler($envelope->getMessage());
32+
if ($handler = $this->messageHandlerLocator->getHandler($envelope, $this->allowNoHandler)) {
33+
$handler($envelope->getMessage());
34+
}
3735

3836
$next($envelope);
3937
}

0 commit comments

Comments
 (0)
0