17
17
use Symfony \Component \Messenger \Stamp \SerializerStamp ;
18
18
use Symfony \Component \Serializer \Encoder \JsonEncoder ;
19
19
use Symfony \Component \Serializer \Encoder \XmlEncoder ;
20
+ use Symfony \Component \Serializer \Normalizer \ArrayDenormalizer ;
20
21
use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
21
22
use Symfony \Component \Serializer \Serializer as SymfonySerializer ;
22
23
use Symfony \Component \Serializer \SerializerInterface as SymfonySerializerInterface ;
@@ -34,9 +35,9 @@ class Serializer implements SerializerInterface
34
35
private $ format ;
35
36
private $ context ;
36
37
37
- public function __construct (SymfonySerializerInterface $ serializer , string $ format = 'json ' , array $ context = array ())
38
+ public function __construct (SymfonySerializerInterface $ serializer = null , string $ format = 'json ' , array $ context = array ())
38
39
{
39
- $ this ->serializer = $ serializer ;
40
+ $ this ->serializer = $ serializer ?? self :: create ()-> serializer ;
40
41
$ this ->format = $ format ;
41
42
$ this ->context = $ context ;
42
43
}
@@ -48,7 +49,7 @@ public static function create(): self
48
49
}
49
50
50
51
$ encoders = array (new XmlEncoder (), new JsonEncoder ());
51
- $ normalizers = array (new ObjectNormalizer ());
52
+ $ normalizers = array (new ArrayDenormalizer (), new ObjectNormalizer ());
52
53
$ serializer = new SymfonySerializer ($ normalizers , $ encoders );
53
54
54
55
return new self ($ serializer );
@@ -70,9 +71,8 @@ public function decode(array $encodedEnvelope): Envelope
70
71
$ stamps = $ this ->decodeStamps ($ encodedEnvelope );
71
72
72
73
$ 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 ;
76
76
}
77
77
78
78
$ message = $ this ->serializer ->deserialize ($ encodedEnvelope ['body ' ], $ encodedEnvelope ['headers ' ]['type ' ], $ this ->format , $ context );
@@ -87,7 +87,7 @@ public function encode(Envelope $envelope): array
87
87
{
88
88
$ context = $ this ->context ;
89
89
/** @var SerializerStamp|null $serializerStamp */
90
- if ($ serializerStamp = $ envelope ->get (SerializerStamp::class)) {
90
+ if ($ serializerStamp = $ envelope ->getLast (SerializerStamp::class)) {
91
91
$ context = $ serializerStamp ->getContext () + $ context ;
92
92
}
93
93
@@ -107,21 +107,24 @@ private function decodeStamps(array $encodedEnvelope): array
107
107
continue ;
108
108
}
109
109
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 );
111
114
}
112
115
113
116
return $ stamps ;
114
117
}
115
118
116
119
private function encodeStamps (Envelope $ envelope ): array
117
120
{
118
- if (!$ stamps = $ envelope ->all ()) {
121
+ if (!$ allStamps = $ envelope ->all ()) {
119
122
return array ();
120
123
}
121
124
122
125
$ headers = array ();
123
- foreach ($ stamps as $ stamp ) {
124
- $ headers [self ::STAMP_HEADER_PREFIX .\get_class ( $ stamp ) ] = $ this ->serializer ->serialize ($ stamp , $ this ->format , $ this ->context );
126
+ foreach ($ allStamps as $ class => $ stamps ) {
127
+ $ headers [self ::STAMP_HEADER_PREFIX .$ class ] = $ this ->serializer ->serialize ($ stamps , $ this ->format , $ this ->context );
125
128
}
126
129
127
130
return $ headers ;
0 commit comments