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

Skip to content

Commit 45c59f1

Browse files
[Messenger] use Envelope internally, return void, add EnvelopeHandlerInterface and other cleanups
1 parent bbcdf31 commit 45c59f1

23 files changed

+111
-179
lines changed

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

Lines changed: 6 additions & 7 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 <[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,15 +33,15 @@ 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)
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:
45-
return $next($envelope);
42+
$next($envelope);
43+
44+
return;
4645
}
4746

4847
$sender = $this->senderLocator->getSender($envelope);
@@ -56,6 +55,6 @@ public function handle($envelope, callable $next)
5655
}
5756
}
5857

59-
return $next($envelope);
58+
$next($envelope);
6059
}
6160
}

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 !== $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/DataCollector/MessengerDataCollector.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ private function collectMessage(string $busName, array $tracedMessage)
9393
'caller' => $tracedMessage['caller'],
9494
);
9595

96-
if (array_key_exists('result', $tracedMessage)) {
97-
$result = $tracedMessage['result'];
98-
$debugRepresentation['result'] = array(
99-
'type' => \is_object($result) ? \get_class($result) : \gettype($result),
100-
'value' => $result,
101-
);
102-
}
103-
10496
if (isset($tracedMessage['exception'])) {
10597
$exception = $tracedMessage['exception'];
10698

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: 3 additions & 6 deletions
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,14 +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
{
42-
$results = array();
43-
4443
foreach ($this->handlers as $handler) {
45-
$results[] = $handler($message);
44+
$handler($envelope->getMessage());
4645
}
47-
48-
return $results;
4946
}
5047
}

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->getMessageName();
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)
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)
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-
return $middleware->handle($message, $next);
7657
};
7758

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

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ interface MessageBusInterface
1919
/**
2020
* Dispatches the given message.
2121
*
22-
* The bus can return a value coming from handlers, but is not required to do so.
23-
*
2422
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
25-
*
26-
* @return mixed
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
2725
*/
28-
public function dispatch($message);
26+
public function dispatch($message, string $name = null): void;
2927
}
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 <[email protected]>
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 <[email protected]>
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: 6 additions & 7 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,14 +34,14 @@ public function __construct(MiddlewareInterface $inner, $activated)
3534
}
3635

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

0 commit comments

Comments
 (0)