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

Skip to content

Commit 0fc495d

Browse files
committed
Uses Symfony Serializer by default for envelope items
1 parent ff1727e commit 0fc495d

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,18 @@ public function testUsesTheCustomFormatAndContext()
8080
$this->assertSame($message, $decoded->getMessage());
8181
}
8282

83-
public function testEncodedWithSerializationConfiguration()
83+
public function testEncodedWithSerializationConfigurationUsingPhpSerialize()
8484
{
8585
$serializer = new Serializer(
86-
new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder()))
86+
new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())),
87+
'json',
88+
array(),
89+
true
8790
);
8891

8992
$envelope = Envelope::wrap(new DummyMessage('Hello'))
90-
->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo'))))
91-
->with(new ValidationConfiguration(array('foo', 'bar')))
93+
->with($serializerConfiguration = new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo'))))
94+
->with($validationConfiguration = new ValidationConfiguration(array('foo', 'bar')))
9295
;
9396

9497
$encoded = $serializer->encode($envelope);
@@ -99,5 +102,37 @@ public function testEncodedWithSerializationConfiguration()
99102
$this->assertEquals(DummyMessage::class, $encoded['headers']['type']);
100103
$this->assertArrayHasKey('X-Message-Envelope-Items', $encoded['headers']);
101104
$this->assertSame('a:2:{s:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration";C:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration":59:{a:1:{s:7:"context";a:1:{s:6:"groups";a:1:{i:0;s:3:"foo";}}}}s:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration";C:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration":82:{a:2:{s:6:"groups";a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}s:17:"is_group_sequence";b:0;}}}', $encoded['headers']['X-Message-Envelope-Items']);
105+
106+
$decoded = $serializer->decode($encoded);
107+
108+
$this->assertEquals($serializerConfiguration, $decoded->get(SerializerConfiguration::class));
109+
$this->assertEquals($validationConfiguration, $decoded->get(ValidationConfiguration::class));
110+
}
111+
112+
public function testEncodedWithSymfonySerializerForItems()
113+
{
114+
$serializer = new Serializer(
115+
new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())),
116+
'json',
117+
array()
118+
);
119+
120+
$envelope = Envelope::wrap(new DummyMessage('Hello'))
121+
->with($serializerConfiguration = new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo'))))
122+
->with($validationConfiguration = new ValidationConfiguration(array('foo', 'bar')))
123+
;
124+
125+
$encoded = $serializer->encode($envelope);
126+
127+
$this->assertArrayHasKey('body', $encoded);
128+
$this->assertArrayHasKey('headers', $encoded);
129+
$this->assertArrayHasKey('type', $encoded['headers']);
130+
$this->assertArrayHasKey('X-Message-Envelope-Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration', $encoded['headers']);
131+
$this->assertArrayHasKey('X-Message-Envelope-Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration', $encoded['headers']);
132+
133+
$decoded = $serializer->decode($encoded);
134+
135+
$this->assertEquals($serializerConfiguration, $decoded->get(SerializerConfiguration::class));
136+
$this->assertEquals($validationConfiguration, $decoded->get(ValidationConfiguration::class));
102137
}
103138
}

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ class Serializer implements DecoderInterface, EncoderInterface
2222
private $serializer;
2323
private $format;
2424
private $context;
25+
private $envelopeWithSerialize;
2526

26-
public function __construct(SerializerInterface $serializer, string $format = 'json', array $context = array())
27+
public function __construct(SerializerInterface $serializer, string $format = 'json', array $context = array(), bool $envelopeWithSerialize = false)
2728
{
2829
$this->serializer = $serializer;
2930
$this->format = $format;
3031
$this->context = $context;
32+
$this->envelopeWithSerialize = $envelopeWithSerialize;
3133
}
3234

3335
/**
@@ -43,7 +45,7 @@ public function decode(array $encodedEnvelope): Envelope
4345
throw new \InvalidArgumentException('Encoded envelope does not have a `type` header.');
4446
}
4547

46-
$envelopeItems = isset($encodedEnvelope['headers']['X-Message-Envelope-Items']) ? unserialize($encodedEnvelope['headers']['X-Message-Envelope-Items']) : array();
48+
$envelopeItems = $this->decodeEnvelopeItems($encodedEnvelope);
4749

4850
$context = $this->context;
4951
/** @var SerializerConfiguration|null $serializerConfig */
@@ -67,14 +69,49 @@ public function encode(Envelope $envelope): array
6769
$context = $serializerConfig->getContext() + $context;
6870
}
6971

70-
$headers = array('type' => \get_class($envelope->getMessage()));
71-
if ($configurations = $envelope->all()) {
72-
$headers['X-Message-Envelope-Items'] = serialize($configurations);
73-
}
72+
$headers = array('type' => \get_class($envelope->getMessage())) + $this->encodeEnvelopeItems($envelope);
7473

7574
return array(
7675
'body' => $this->serializer->serialize($envelope->getMessage(), $this->format, $context),
7776
'headers' => $headers,
7877
);
7978
}
79+
80+
private function decodeEnvelopeItems($encodedEnvelope)
81+
{
82+
if ($this->envelopeWithSerialize) {
83+
return isset($encodedEnvelope['headers']['X-Message-Envelope-Items']) ? unserialize($encodedEnvelope['headers']['X-Message-Envelope-Items']) : array();
84+
}
85+
86+
$items = array();
87+
foreach ($encodedEnvelope['headers'] as $name => $value) {
88+
if (0 !== strpos($name, $prefix = 'X-Message-Envelope-')) {
89+
continue;
90+
}
91+
92+
$items[] = $this->serializer->deserialize($value, substr($name, strlen($prefix)), $this->format, $this->context);
93+
}
94+
95+
return $items;
96+
}
97+
98+
private function encodeEnvelopeItems(Envelope $envelope)
99+
{
100+
if (!$configurations = $envelope->all()) {
101+
return array();
102+
}
103+
104+
if ($this->envelopeWithSerialize) {
105+
return array(
106+
'X-Message-Envelope-Items' => serialize($configurations),
107+
);
108+
}
109+
110+
$headers = array();
111+
foreach ($configurations as $configuration) {
112+
$headers['X-Message-Envelope-'.get_class($configuration)] = $this->serializer->serialize($configuration, $this->format, $this->context);
113+
}
114+
115+
return $headers;
116+
}
80117
}

0 commit comments

Comments
 (0)