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

Commit957f9a7

Browse files
[Messenger] collect all stamps added on Envelope as collections
1 parent99856a9 commit957f9a7

File tree

9 files changed

+43
-44
lines changed

9 files changed

+43
-44
lines changed

‎src/Symfony/Component/Messenger/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ CHANGELOG
3333
*`ActivationMiddlewareDecorator` has been renamed`ActivationMiddleware`
3434
*`AllowNoHandlerMiddleware` has been removed in favor of a new constructor argument on`HandleMessageMiddleware`
3535
* The`ContainerHandlerLocator`,`AbstractHandlerLocator`,`SenderLocator` and`AbstractSenderLocator` classes have been removed
36+
*`Envelope::all()` takes a new optional`$stampFqcn` argument and returns the stamps for the specified FQCN, or all stamps by their class name
37+
*`Envelope::get()` has been renamed`Envelope::getLast()`
3638

3739
4.1.0
3840
-----

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct($message, StampInterface ...$stamps)
3636
$this->message =$message;
3737

3838
foreach ($stampsas$stamp) {
39-
$this->stamps[\get_class($stamp)] =$stamp;
39+
$this->stamps[\get_class($stamp)][] =$stamp;
4040
}
4141
}
4242

@@ -48,22 +48,26 @@ public function with(StampInterface ...$stamps): self
4848
$cloned =clone$this;
4949

5050
foreach ($stampsas$stamp) {
51-
$cloned->stamps[\get_class($stamp)] =$stamp;
51+
$cloned->stamps[\get_class($stamp)][] =$stamp;
5252
}
5353

5454
return$cloned;
5555
}
5656

57-
publicfunctionget(string$stampFqcn): ?StampInterface
57+
publicfunctiongetLast(string$stampFqcn): ?StampInterface
5858
{
59-
return$this->stamps[$stampFqcn] ??null;
59+
returnisset($this->stamps[$stampFqcn]) ?end($this->stamps[$stampFqcn]) :null;
6060
}
6161

6262
/**
63-
* @return StampInterface[] indexedbyfqcn
63+
* @return StampInterface[]|StampInterface[][] The stamps for the specified FQCN, or all stampsbytheir class name
6464
*/
65-
publicfunctionall():array
65+
publicfunctionall(string$stampFqcn =null):array
6666
{
67+
if (null !==$stampFqcn) {
68+
return$this->stamps[$stampFqcn] ??array();
69+
}
70+
6771
return$this->stamps;
6872
}
6973

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(SendersLocatorInterface $sendersLocator)
3535
*/
3636
publicfunctionhandle(Envelope$envelope,StackInterface$stack):Envelope
3737
{
38-
if ($envelope->get(ReceivedStamp::class)) {
38+
if ($envelope->all(ReceivedStamp::class)) {
3939
// it's a received message, do not send it back
4040
return$stack->next()->handle($envelope,$stack);
4141
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
3838
$message =$envelope->getMessage();
3939
$groups =null;
4040
/** @var ValidationStamp|null $validationStamp */
41-
if ($validationStamp =$envelope->get(ValidationStamp::class)) {
41+
if ($validationStamp =$envelope->getLast(ValidationStamp::class)) {
4242
$groups =$validationStamp->getGroups();
4343
}
4444

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testConstruct()
2929

3030
$this->assertSame($dummy,$envelope->getMessage());
3131
$this->assertArrayHasKey(ReceivedStamp::class,$stamps =$envelope->all());
32-
$this->assertSame($receivedStamp,$stamps[ReceivedStamp::class]);
32+
$this->assertSame($receivedStamp,$stamps[ReceivedStamp::class][0]);
3333
}
3434

3535
publicfunctiontestWithReturnsNewInstance()
@@ -39,13 +39,13 @@ public function testWithReturnsNewInstance()
3939
$this->assertNotSame($envelope,$envelope->with(newReceivedStamp()));
4040
}
4141

42-
publicfunctiontestGet()
42+
publicfunctiontestGetLast()
4343
{
4444
$receivedStamp =newReceivedStamp();
4545
$envelope =newEnvelope($dummy =newDummyMessage('dummy'),$receivedStamp);
4646

47-
$this->assertSame($receivedStamp,$envelope->get(ReceivedStamp::class));
48-
$this->assertNull($envelope->get(ValidationStamp::class));
47+
$this->assertSame($receivedStamp,$envelope->getLast(ReceivedStamp::class));
48+
$this->assertNull($envelope->getLast(ValidationStamp::class));
4949
}
5050

5151
publicfunctiontestAll()
@@ -57,8 +57,8 @@ public function testAll()
5757

5858
$stamps =$envelope->all();
5959
$this->assertArrayHasKey(ReceivedStamp::class,$stamps);
60-
$this->assertSame($receivedStamp,$stamps[ReceivedStamp::class]);
60+
$this->assertSame($receivedStamp,$stamps[ReceivedStamp::class][0]);
6161
$this->assertArrayHasKey(ValidationStamp::class,$stamps);
62-
$this->assertSame($validationStamp,$stamps[ValidationStamp::class]);
62+
$this->assertSame($validationStamp,$stamps[ValidationStamp::class][0]);
6363
}
6464
}

‎src/Symfony/Component/Messenger/Tests/TraceableMessageBusTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testItTracesDispatchWithEnvelope()
5656
$this->assertCount(1,$tracedMessages =$traceableBus->getDispatchedMessages());
5757
$this->assertArraySubset(array(
5858
'message' =>$message,
59-
'stamps' =>array($stamp),
59+
'stamps' =>array(array($stamp)),
6060
'caller' =>array(
6161
'name' =>'TraceableMessageBusTest.php',
6262
'file' =>__FILE__,

‎src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php‎

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@
1818
useSymfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1919
useSymfony\Component\Messenger\Transport\Serialization\Serializer;
2020
useSymfony\Component\SerializerasSerializerComponent;
21-
useSymfony\Component\Serializer\Encoder\JsonEncoder;
2221
useSymfony\Component\Serializer\Normalizer\ObjectNormalizer;
2322

2423
class SerializerTestextends TestCase
2524
{
2625
publicfunctiontestEncodedIsDecodable()
2726
{
28-
$serializer =newSerializer(
29-
newSerializerComponent\Serializer(array(newObjectNormalizer()),array('json' =>newJsonEncoder()))
30-
);
27+
$serializer =newSerializer();
3128

3229
$envelope =newEnvelope(newDummyMessage('Hello'));
3330

@@ -36,9 +33,7 @@ public function testEncodedIsDecodable()
3633

3734
publicfunctiontestEncodedWithStampsIsDecodable()
3835
{
39-
$serializer =newSerializer(
40-
newSerializerComponent\Serializer(array(newObjectNormalizer()),array('json' =>newJsonEncoder()))
41-
);
36+
$serializer =newSerializer();
4237

4338
$envelope = (newEnvelope(newDummyMessage('Hello')))
4439
->with(newSerializerStamp(array(ObjectNormalizer::GROUPS =>array('foo'))))
@@ -50,9 +45,7 @@ public function testEncodedWithStampsIsDecodable()
5045

5146
publicfunctiontestEncodedIsHavingTheBodyAndTypeHeader()
5247
{
53-
$serializer =newSerializer(
54-
newSerializerComponent\Serializer(array(newObjectNormalizer()),array('json' =>newJsonEncoder()))
55-
);
48+
$serializer =newSerializer();
5649

5750
$encoded =$serializer->encode(newEnvelope(newDummyMessage('Hello')));
5851

@@ -81,11 +74,7 @@ public function testUsesTheCustomFormatAndContext()
8174

8275
publicfunctiontestEncodedWithSymfonySerializerForStamps()
8376
{
84-
$serializer =newSerializer(
85-
newSerializerComponent\Serializer(array(newObjectNormalizer()),array('json' =>newJsonEncoder())),
86-
'json',
87-
array()
88-
);
77+
$serializer =newSerializer();
8978

9079
$envelope = (newEnvelope(newDummyMessage('Hello')))
9180
->with($serializerStamp =newSerializerStamp(array(ObjectNormalizer::GROUPS =>array('foo'))))
@@ -102,7 +91,7 @@ public function testEncodedWithSymfonySerializerForStamps()
10291

10392
$decoded =$serializer->decode($encoded);
10493

105-
$this->assertEquals($serializerStamp,$decoded->get(SerializerStamp::class));
106-
$this->assertEquals($validationStamp,$decoded->get(ValidationStamp::class));
94+
$this->assertEquals($serializerStamp,$decoded->getLast(SerializerStamp::class));
95+
$this->assertEquals($validationStamp,$decoded->getLast(ValidationStamp::class));
10796
}
10897
}

‎src/Symfony/Component/Messenger/Tests/WorkerTest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public function testWorkerDispatchTheReceivedMessage()
4242

4343
publicfunctiontestWorkerDoesNotWrapMessagesAlreadyWrappedWithReceivedMessage()
4444
{
45-
$envelope =(newEnvelope(newDummyMessage('API')))->with(newReceivedStamp());
45+
$envelope =newEnvelope(newDummyMessage('API'));
4646
$receiver =newCallbackReceiver(function ($handler)use ($envelope) {
4747
$handler($envelope);
4848
});
49+
$envelope =$envelope->with(newReceivedStamp());
4950

5051
$bus =$this->getMockBuilder(MessageBusInterface::class)->getMock();
5152
$bus->expects($this->at(0))->method('dispatch')->with($envelope)->willReturn($envelope);

‎src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
useSymfony\Component\Messenger\Stamp\SerializerStamp;
1818
useSymfony\Component\Serializer\Encoder\JsonEncoder;
1919
useSymfony\Component\Serializer\Encoder\XmlEncoder;
20+
useSymfony\Component\Serializer\Normalizer\ArrayDenormalizer;
2021
useSymfony\Component\Serializer\Normalizer\ObjectNormalizer;
2122
useSymfony\Component\Serializer\SerializerasSymfonySerializer;
2223
useSymfony\Component\Serializer\SerializerInterfaceasSymfonySerializerInterface;
@@ -34,9 +35,9 @@ class Serializer implements SerializerInterface
3435
private$format;
3536
private$context;
3637

37-
publicfunction__construct(SymfonySerializerInterface$serializer,string$format ='json',array$context =array())
38+
publicfunction__construct(SymfonySerializerInterface$serializer =null,string$format ='json',array$context =array())
3839
{
39-
$this->serializer =$serializer;
40+
$this->serializer =$serializer ??self::create()->serializer;
4041
$this->format =$format;
4142
$this->context =$context;
4243
}
@@ -48,7 +49,7 @@ public static function create(): self
4849
}
4950

5051
$encoders =array(newXmlEncoder(),newJsonEncoder());
51-
$normalizers =array(newObjectNormalizer());
52+
$normalizers =array(newArrayDenormalizer(),newObjectNormalizer());
5253
$serializer =newSymfonySerializer($normalizers,$encoders);
5354

5455
returnnewself($serializer);
@@ -70,9 +71,8 @@ public function decode(array $encodedEnvelope): Envelope
7071
$stamps =$this->decodeStamps($encodedEnvelope);
7172

7273
$context =$this->context;
73-
/** @var SerializerStamp|null $serializerStamp */
74-
if ($serializerStamp =$stamps[SerializerStamp::class] ??null) {
75-
$context =$serializerStamp->getContext() +$context;
74+
if (isset($stamps[SerializerStamp::class])) {
75+
$context =end($stamps[SerializerStamp::class])->getContext() +$context;
7676
}
7777

7878
$message =$this->serializer->deserialize($encodedEnvelope['body'],$encodedEnvelope['headers']['type'],$this->format,$context);
@@ -87,7 +87,7 @@ public function encode(Envelope $envelope): array
8787
{
8888
$context =$this->context;
8989
/** @var SerializerStamp|null $serializerStamp */
90-
if ($serializerStamp =$envelope->get(SerializerStamp::class)) {
90+
if ($serializerStamp =$envelope->getLast(SerializerStamp::class)) {
9191
$context =$serializerStamp->getContext() +$context;
9292
}
9393

@@ -107,21 +107,24 @@ private function decodeStamps(array $encodedEnvelope): array
107107
continue;
108108
}
109109

110-
$stamps[] =$this->serializer->deserialize($value,substr($name,\strlen(self::STAMP_HEADER_PREFIX)),$this->format,$this->context);
110+
$stamps[] =$this->serializer->deserialize($value,substr($name,\strlen(self::STAMP_HEADER_PREFIX)).'[]',$this->format,$this->context);
111+
}
112+
if ($stamps) {
113+
$stamps =array_merge(...$stamps);
111114
}
112115

113116
return$stamps;
114117
}
115118

116119
privatefunctionencodeStamps(Envelope$envelope):array
117120
{
118-
if (!$stamps =$envelope->all()) {
121+
if (!$allStamps =$envelope->all()) {
119122
returnarray();
120123
}
121124

122125
$headers =array();
123-
foreach ($stampsas$stamp) {
124-
$headers[self::STAMP_HEADER_PREFIX.\get_class($stamp)] =$this->serializer->serialize($stamp,$this->format,$this->context);
126+
foreach ($allStampsas$class =>$stamps) {
127+
$headers[self::STAMP_HEADER_PREFIX.$class] =$this->serializer->serialize($stamps,$this->format,$this->context);
125128
}
126129

127130
return$headers;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp