Skip to content

Commit e99e622

Browse files
committed
feature #26941 [Messenger] Allow to configure the transport (sroze)
This PR was squashed before being merged into the 4.1-dev branch (closes #26941). Discussion ---------- [Messenger] Allow to configure the transport | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | ish | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26900, #26908, #26935 | License | MIT | Doc PR | ø We allow users to configure the encoder/decoder used by the built-in adapter(s). This also adds the support of configuring the default's encoder/decoder format and context. Commits ------- 1a3f0bbb14 [Messenger] Allow to configure the transport
2 parents 512bf78 + 23570b4 commit e99e622

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

DependencyInjection/MessengerPass.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ public function process(ContainerBuilder $container)
5353
$container->removeDefinition('messenger.middleware.debug.logging');
5454
}
5555

56-
if (!$container->has('serializer')) {
57-
$container->removeDefinition('messenger.transport.serialize_message_with_type_in_headers');
58-
$container->removeAlias('messenger.transport.default_encoder');
59-
$container->removeAlias('messenger.transport.default_decoder');
60-
}
61-
6256
$this->registerReceivers($container);
6357
$this->registerSenders($container);
6458
$this->registerHandlers($container);

Tests/Transport/Serialization/SerializerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,21 @@ public function testEncodedIsHavingTheBodyAndTypeHeader()
4444
$this->assertArrayHasKey('type', $encoded['headers']);
4545
$this->assertEquals(DummyMessage::class, $encoded['headers']['type']);
4646
}
47+
48+
public function testUsesTheCustomFormatAndContext()
49+
{
50+
$message = new DummyMessage('Foo');
51+
52+
$serializer = $this->getMockBuilder(SerializerComponent\SerializerInterface::class)->getMock();
53+
$serializer->expects($this->once())->method('serialize')->with($message, 'csv', array('foo' => 'bar'))->willReturn('Yay');
54+
$serializer->expects($this->once())->method('deserialize')->with('Yay', DummyMessage::class, 'csv', array('foo' => 'bar'))->willReturn($message);
55+
56+
$encoder = new Serializer($serializer, 'csv', array('foo' => 'bar'));
57+
58+
$encoded = $encoder->encode($message);
59+
$decoded = $encoder->decode($encoded);
60+
61+
$this->assertSame('Yay', $encoded['body']);
62+
$this->assertSame($message, $decoded);
63+
}
4764
}

Transport/Serialization/Serializer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class Serializer implements DecoderInterface, EncoderInterface
2020
{
2121
private $serializer;
2222
private $format;
23+
private $context;
2324

24-
public function __construct(SerializerInterface $serializer, string $format = 'json')
25+
public function __construct(SerializerInterface $serializer, string $format = 'json', array $context = array())
2526
{
2627
$this->serializer = $serializer;
2728
$this->format = $format;
29+
$this->context = $context;
2830
}
2931

3032
/**
@@ -40,7 +42,7 @@ public function decode(array $encodedMessage)
4042
throw new \InvalidArgumentException('Encoded message does not have a `type` header.');
4143
}
4244

43-
return $this->serializer->deserialize($encodedMessage['body'], $encodedMessage['headers']['type'], $this->format);
45+
return $this->serializer->deserialize($encodedMessage['body'], $encodedMessage['headers']['type'], $this->format, $this->context);
4446
}
4547

4648
/**
@@ -49,7 +51,7 @@ public function decode(array $encodedMessage)
4951
public function encode($message): array
5052
{
5153
return array(
52-
'body' => $this->serializer->serialize($message, $this->format),
54+
'body' => $this->serializer->serialize($message, $this->format, $this->context),
5355
'headers' => array('type' => \get_class($message)),
5456
);
5557
}

0 commit comments

Comments
 (0)