From 9b575ab263ffd6f5c4ea99e360fcab033bd01599 Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Sun, 26 Aug 2018 14:13:20 +0100 Subject: [PATCH] Uses Symfony Serializer by default for envelope items --- .../Serialization/SerializerTest.php | 20 +++++++---- .../Transport/Serialization/Serializer.php | 35 ++++++++++++++++--- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php index ee8ed66a9e145..6475c65f7778e 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php @@ -80,15 +80,17 @@ public function testUsesTheCustomFormatAndContext() $this->assertSame($message, $decoded->getMessage()); } - public function testEncodedWithSerializationConfiguration() + public function testEncodedWithSymfonySerializerForItems() { $serializer = new Serializer( - new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) + new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())), + 'json', + array() ); $envelope = Envelope::wrap(new DummyMessage('Hello')) - ->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo')))) - ->with(new ValidationConfiguration(array('foo', 'bar'))) + ->with($serializerConfiguration = new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo')))) + ->with($validationConfiguration = new ValidationConfiguration(array('foo', 'bar'))) ; $encoded = $serializer->encode($envelope); @@ -96,8 +98,12 @@ public function testEncodedWithSerializationConfiguration() $this->assertArrayHasKey('body', $encoded); $this->assertArrayHasKey('headers', $encoded); $this->assertArrayHasKey('type', $encoded['headers']); - $this->assertEquals(DummyMessage::class, $encoded['headers']['type']); - $this->assertArrayHasKey('X-Message-Envelope-Items', $encoded['headers']); - $this->assertSame('a:2:{s:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration";O:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration":1:{s:84:"'."\0".'Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration'."\0".'context";a:1:{s:6:"groups";a:1:{i:0;s:3:"foo";}}}s:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration";O:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration":1:{s:84:"'."\0".'Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration'."\0".'groups";a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}}}', $encoded['headers']['X-Message-Envelope-Items']); + $this->assertArrayHasKey('X-Message-Envelope-Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration', $encoded['headers']); + $this->assertArrayHasKey('X-Message-Envelope-Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration', $encoded['headers']); + + $decoded = $serializer->decode($encoded); + + $this->assertEquals($serializerConfiguration, $decoded->get(SerializerConfiguration::class)); + $this->assertEquals($validationConfiguration, $decoded->get(ValidationConfiguration::class)); } } diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php index cfb30090e5dfa..45665473f741c 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php @@ -43,7 +43,7 @@ public function decode(array $encodedEnvelope): Envelope throw new \InvalidArgumentException('Encoded envelope does not have a `type` header.'); } - $envelopeItems = isset($encodedEnvelope['headers']['X-Message-Envelope-Items']) ? unserialize($encodedEnvelope['headers']['X-Message-Envelope-Items']) : array(); + $envelopeItems = $this->decodeEnvelopeItems($encodedEnvelope); $context = $this->context; /** @var SerializerConfiguration|null $serializerConfig */ @@ -67,14 +67,39 @@ public function encode(Envelope $envelope): array $context = $serializerConfig->getContext() + $context; } - $headers = array('type' => \get_class($envelope->getMessage())); - if ($configurations = $envelope->all()) { - $headers['X-Message-Envelope-Items'] = serialize($configurations); - } + $headers = array('type' => \get_class($envelope->getMessage())) + $this->encodeEnvelopeItems($envelope); return array( 'body' => $this->serializer->serialize($envelope->getMessage(), $this->format, $context), 'headers' => $headers, ); } + + private function decodeEnvelopeItems($encodedEnvelope) + { + $items = array(); + foreach ($encodedEnvelope['headers'] as $name => $value) { + if (0 !== strpos($name, $prefix = 'X-Message-Envelope-')) { + continue; + } + + $items[] = $this->serializer->deserialize($value, substr($name, \strlen($prefix)), $this->format, $this->context); + } + + return $items; + } + + private function encodeEnvelopeItems(Envelope $envelope) + { + if (!$configurations = $envelope->all()) { + return array(); + } + + $headers = array(); + foreach ($configurations as $configuration) { + $headers['X-Message-Envelope-'.\get_class($configuration)] = $this->serializer->serialize($configuration, $this->format, $this->context); + } + + return $headers; + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy