Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd0aec9

Browse files
committed
feature#26945 [Messenger] Support configuring messages when dispatching (ogizanagi)
This reverts commit76b17b0.
1 parent76b17b0 commitcd0aec9

File tree

36 files changed

+648
-125
lines changed

36 files changed

+648
-125
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
useSymfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
1515
useSymfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
16+
useSymfony\Component\Messenger\Envelope;
17+
useSymfony\Component\Messenger\EnvelopeAwareInterface;
1618
useSymfony\Component\Messenger\Middleware\MiddlewareInterface;
1719

1820
/**
1921
* @author Samuel Roze <samuel.roze@gmail.com>
2022
*/
21-
class SendMessageMiddlewareimplements MiddlewareInterface
23+
class SendMessageMiddlewareimplements MiddlewareInterface, EnvelopeAwareInterface
2224
{
2325
private$senderLocator;
2426

@@ -32,17 +34,19 @@ public function __construct(SenderLocatorInterface $senderLocator)
3234
*/
3335
publicfunctionhandle($message,callable$next)
3436
{
35-
if ($messageinstanceof ReceivedMessage) {
36-
return$next($message->getMessage());
37+
$envelope = Envelope::wrap($message);
38+
if ($envelope->get(ReceivedMessage::class)) {
39+
// It's a received message. Do not send it back:
40+
return$next($message);
3741
}
3842

39-
if (!empty($senders =$this->senderLocator->getSendersForMessage($message))) {
43+
if (!empty($senders =$this->senderLocator->getSendersForMessage($envelope->getMessage()))) {
4044
foreach ($sendersas$sender) {
4145
if (null ===$sender) {
4246
continue;
4347
}
4448

45-
$sender->send($message);
49+
$sender->send($envelope);
4650
}
4751

4852
if (!\in_array(null,$senders,true)) {

‎src/Symfony/Component/Messenger/Asynchronous/Transport/ReceivedMessage.php‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
namespaceSymfony\Component\Messenger\Asynchronous\Transport;
1313

1414
useSymfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware;
15+
useSymfony\Component\Messenger\EnvelopeItemInterface;
1516

1617
/**
17-
* Wraps a received message. This is mainly used by the `SendMessageMiddleware` middleware to identify
18+
* Marker config for a received message.
19+
* This is mainly used by the `SendMessageMiddleware` middleware to identify
1820
* a message should not be sent if it was just received.
1921
*
2022
* @see SendMessageMiddleware
2123
*
2224
* @author Samuel Roze <samuel.roze@gmail.com>
2325
*/
24-
finalclass ReceivedMessage
26+
finalclass ReceivedMessageimplements EnvelopeItemInterface
2527
{
26-
private$message;
27-
28-
publicfunction__construct($message)
28+
publicfunctionserialize()
2929
{
30-
$this->message =$message;
30+
return'';
3131
}
3232

33-
publicfunctiongetMessage()
33+
publicfunctionunserialize($serialized)
3434
{
35-
return$this->message;
35+
// noop
3636
}
3737
}

‎src/Symfony/Component/Messenger/Asynchronous/Transport/WrapIntoReceivedMessage.php‎

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

1212
namespaceSymfony\Component\Messenger\Asynchronous\Transport;
1313

14+
useSymfony\Component\Messenger\Envelope;
1415
useSymfony\Component\Messenger\Transport\ReceiverInterface;
1516

1617
/**
@@ -27,12 +28,12 @@ public function __construct(ReceiverInterface $decoratedConsumer)
2728

2829
publicfunctionreceive(callable$handler):void
2930
{
30-
$this->decoratedReceiver->receive(function ($message)use ($handler) {
31-
if (null !==$message) {
32-
$message =newReceivedMessage($message);
31+
$this->decoratedReceiver->receive(function (?Envelope$envelope)use ($handler) {
32+
if (null !==$envelope) {
33+
$envelope =$envelope->with(newReceivedMessage());
3334
}
3435

35-
$handler($message);
36+
$handler($envelope);
3637
});
3738
}
3839

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
namespaceSymfony\Component\Messenger;
13+
14+
/**
15+
* A message wrapped in an envelope with items (configurations, markers, ...).
16+
*
17+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
18+
*
19+
* @experimental in 4.1
20+
*/
21+
finalclass Envelope
22+
{
23+
private$items =array();
24+
private$message;
25+
26+
/**
27+
* @param object $message
28+
* @param EnvelopeItemInterface[] $items
29+
*/
30+
publicfunction__construct($message,array$items =array())
31+
{
32+
$this->message =$message;
33+
foreach ($itemsas$item) {
34+
$this->items[\get_class($item)] =$item;
35+
}
36+
}
37+
38+
/**
39+
* Wrap a message into an envelope if not already wrapped.
40+
*
41+
* @param Envelope|object $message
42+
*/
43+
publicstaticfunctionwrap($message):self
44+
{
45+
return$messageinstanceof self ?$message :newself($message);
46+
}
47+
48+
/**
49+
* @return Envelope a new Envelope instance with additional item
50+
*/
51+
publicfunctionwith(EnvelopeItemInterface$item):self
52+
{
53+
$cloned =clone$this;
54+
55+
$cloned->items[\get_class($item)] =$item;
56+
57+
return$cloned;
58+
}
59+
60+
publicfunctionget(string$itemFqcn): ?EnvelopeItemInterface
61+
{
62+
return$this->items[$itemFqcn] ??null;
63+
}
64+
65+
/**
66+
* @return EnvelopeItemInterface[] indexed by fqcn
67+
*/
68+
publicfunctionall():array
69+
{
70+
return$this->items;
71+
}
72+
73+
/**
74+
* @return object The original message contained in the envelope
75+
*/
76+
publicfunctiongetMessage()
77+
{
78+
return$this->message;
79+
}
80+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
namespaceSymfony\Component\Messenger;
13+
14+
/**
15+
* A Messenger protagonist aware of the message envelope and its content.
16+
*
17+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
18+
*
19+
* @experimental in 4.1
20+
*/
21+
interface EnvelopeAwareInterface
22+
{
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
namespaceSymfony\Component\Messenger;
13+
14+
/**
15+
* An envelope item related to a message.
16+
* This item must be serializable for transport.
17+
*
18+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
19+
*
20+
* @experimental in 4.1
21+
*/
22+
interface EnvelopeItemInterfaceextends \Serializable
23+
{
24+
}

‎src/Symfony/Component/Messenger/MessageBus.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ private function callableForNextMiddleware(int $index): callable
5555
$middleware =$this->indexedMiddlewareHandlers[$index];
5656

5757
returnfunction ($message)use ($middleware,$index) {
58+
$message = Envelope::wrap($message);
59+
if (!$middlewareinstanceof EnvelopeAwareInterface) {
60+
// Do not provide the envelope if the middleware cannot read it:
61+
$message =$message->getMessage();
62+
}
63+
5864
return$middleware->handle($message,$this->callableForNextMiddleware($index +1));
5965
};
6066
}

‎src/Symfony/Component/Messenger/MessageBusInterface.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface MessageBusInterface
2323
*
2424
* The bus can return a value coming from handlers, but is not required to do so.
2525
*
26-
* @param object $message
26+
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
2727
*
2828
* @return mixed
2929
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
namespaceSymfony\Component\Messenger\Middleware\Configuration;
13+
14+
useSymfony\Component\Messenger\EnvelopeItemInterface;
15+
useSymfony\Component\Validator\Constraints\GroupSequence;
16+
17+
/**
18+
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
19+
*
20+
* @experimental in 4.1
21+
*/
22+
finalclass ValidationConfigurationimplements EnvelopeItemInterface
23+
{
24+
private$groups;
25+
26+
/**
27+
* @param string[]|GroupSequence $groups
28+
*/
29+
publicfunction__construct($groups)
30+
{
31+
$this->groups =$groups;
32+
}
33+
34+
publicfunctiongetGroups()
35+
{
36+
return$this->groups;
37+
}
38+
39+
publicfunctionserialize()
40+
{
41+
$isGroupSequence =$this->groupsinstanceof GroupSequence;
42+
43+
returnserialize(array(
44+
'groups' =>$isGroupSequence ?$this->groups->groups :$this->groups,
45+
'is_group_sequence' =>$isGroupSequence,
46+
));
47+
}
48+
49+
publicfunctionunserialize($serialized)
50+
{
51+
list(
52+
'groups' =>$groups,
53+
'is_group_sequence' =>$isGroupSequence
54+
) =unserialize($serialized,array('allowed_classes' =>false));
55+
56+
$this->__construct($isGroupSequence ?newGroupSequence($groups) :$groups);
57+
}
58+
}

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

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

1212
namespaceSymfony\Component\Messenger\Middleware;
1313

14-
useSymfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
1514
usePsr\Log\LoggerInterface;
1615

1716
/**
@@ -51,10 +50,6 @@ public function handle($message, callable $next)
5150

5251
privatefunctioncreateContext($message):array
5352
{
54-
if ($messageinstanceof ReceivedMessage) {
55-
$message =$message->getMessage();
56-
}
57-
5853
returnarray(
5954
'message' =>$message,
6055
'class' =>\get_class($message),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp