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

Appearance settings

Commit 4a027a1

Browse filesBrowse files
committed
Adding a new NonSendableStampInterface to avoid sending certain stamps
Fixes a bug where Symfony serialization of the AmqpReceivedStamp sometimes caused problems.
1 parent ca6266d commit 4a027a1
Copy full SHA for 4a027a1

File tree

Expand file treeCollapse file tree

11 files changed

+101
-6
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+101
-6
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/CHANGELOG.md
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* Added `NonSendableStampInterface` that a stamp can implement if
8+
it should not be sent to a transport. Transport serializers
9+
must now check for these stamps and not encode them.
710
* [BC BREAK] `SendersLocatorInterface` has an additional method:
811
`getSenderByAlias()`.
912
* A new `ListableReceiverInterface` was added, which a receiver
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Stamp;
13+
14+
/**
15+
* A stamp that should not be included with the Envelope if sent to a transport.
16+
*
17+
* @author Ryan Weaver <ryan@symfonycasts.com>
18+
*
19+
* @experimental in 4.3
20+
*/
21+
interface NonSendableStampInterface extends StampInterface
22+
{
23+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/Transport/Serialization/PhpSerializerTest.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
17+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1718
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
1819
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
1920

@@ -63,4 +64,20 @@ public function testDecodingFailsWithBadClass()
6364
'body' => 'O:13:"ReceivedSt0mp":0:{}',
6465
]);
6566
}
67+
68+
public function testEncodedSkipsNonEncodeableStamps()
69+
{
70+
$serializer = new PhpSerializer();
71+
72+
$envelope = new Envelope(new DummyMessage('Hello'), [
73+
new DummyPhpSerializerNonSendableStamp()
74+
]);
75+
76+
$encoded = $serializer->encode($envelope);
77+
$this->assertNotContains('DummyPhpSerializerNonSendableStamp', $encoded['body']);
78+
}
6679
}
80+
81+
class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface
82+
{
83+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
17+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1718
use Symfony\Component\Messenger\Stamp\SerializerStamp;
1819
use Symfony\Component\Messenger\Stamp\ValidationStamp;
1920
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
@@ -193,4 +194,19 @@ public function testDecodingFailsWithBadClass()
193194
'headers' => ['type' => 'NonExistentClass'],
194195
]);
195196
}
197+
198+
public function testEncodedSkipsNonEncodeableStamps()
199+
{
200+
$serializer = new Serializer();
201+
202+
$envelope = new Envelope(new DummyMessage('Hello'), [
203+
new DummySymfonySerializerNonSendableStamp()
204+
]);
205+
206+
$encoded = $serializer->encode($envelope);
207+
$this->assertNotContains('DummySymfonySerializerNonSendableStamp', print_r($encoded['headers'], true));
208+
}
196209
}
210+
class DummySymfonySerializerNonSendableStamp implements NonSendableStampInterface
211+
{
212+
}

‎src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceivedStamp.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceivedStamp.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Component\Messenger\Transport\AmqpExt;
1313

14-
use Symfony\Component\Messenger\Stamp\StampInterface;
14+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1515

1616
/**
1717
* Stamp applied when a message is received from Amqp.
1818
*
1919
* @experimental in 4.3
2020
*/
21-
class AmqpReceivedStamp implements StampInterface
21+
class AmqpReceivedStamp implements NonSendableStampInterface
2222
{
2323
private $amqpEnvelope;
2424
private $queueName;

‎src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineReceivedStamp.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Doctrine/DoctrineReceivedStamp.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Component\Messenger\Transport\Doctrine;
1313

14-
use Symfony\Component\Messenger\Stamp\StampInterface;
14+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1515

1616
/**
1717
* @author Vincent Touzet <vincent.touzet@gmail.com>
1818
*
1919
* @experimental in 4.3
2020
*/
21-
class DoctrineReceivedStamp implements StampInterface
21+
class DoctrineReceivedStamp implements NonSendableStampInterface
2222
{
2323
private $id;
2424

‎src/Symfony/Component/Messenger/Transport/RedisExt/RedisReceivedStamp.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/RedisExt/RedisReceivedStamp.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
namespace Symfony\Component\Messenger\Transport\RedisExt;
1313

14-
use Symfony\Component\Messenger\Stamp\StampInterface;
14+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1515

1616
/**
1717
* @author Alexander Schranz <alexander@sulu.io>
1818
*
1919
* @experimental in 4.3
2020
*/
21-
class RedisReceivedStamp implements StampInterface
21+
class RedisReceivedStamp implements NonSendableStampInterface
2222
{
2323
private $id;
2424

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
class PhpSerializer implements SerializerInterface
2323
{
24+
use RemoveNonSendableStampsTrait;
25+
2426
/**
2527
* {@inheritdoc}
2628
*/
@@ -40,6 +42,8 @@ public function decode(array $encodedEnvelope): Envelope
4042
*/
4143
public function encode(Envelope $envelope): array
4244
{
45+
$envelope = $this->removeNonSendableStamps($envelope);
46+
4347
$body = addslashes(serialize($envelope));
4448

4549
return [
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Transport\Serialization;
4+
5+
6+
use Symfony\Component\Messenger\Envelope;
7+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
8+
9+
trait RemoveNonSendableStampsTrait
10+
{
11+
private function removeNonSendableStamps(Envelope $originalEnvelope)
12+
{
13+
$envelope = new Envelope($originalEnvelope->getMessage());
14+
foreach ($originalEnvelope->all() as $class => $stamps) {
15+
if (\is_subclass_of($class, NonSendableStampInterface::class)) {
16+
continue;
17+
}
18+
19+
$envelope = $envelope->with(...$stamps);
20+
}
21+
22+
return $envelope;
23+
}
24+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Messenger\Envelope;
1515
use Symfony\Component\Messenger\Exception\LogicException;
1616
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
17+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1718
use Symfony\Component\Messenger\Stamp\SerializerStamp;
1819
use Symfony\Component\Messenger\Stamp\StampInterface;
1920
use Symfony\Component\Serializer\Encoder\JsonEncoder;
@@ -31,6 +32,8 @@
3132
*/
3233
class Serializer implements SerializerInterface
3334
{
35+
use RemoveNonSendableStampsTrait;
36+
3437
private const STAMP_HEADER_PREFIX = 'X-Message-Stamp-';
3538

3639
private $serializer;
@@ -98,6 +101,8 @@ public function encode(Envelope $envelope): array
98101
$context = $serializerStamp->getContext() + $context;
99102
}
100103

104+
$envelope = $this->removeNonSendableStamps($envelope);
105+
101106
$headers = ['type' => \get_class($envelope->getMessage())] + $this->encodeStamps($envelope);
102107

103108
return [

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Serialization/SerializerInterface.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function decode(array $encodedEnvelope): Envelope;
3939
* Encodes an envelope content (message & stamps) to a common format understandable by transports.
4040
* The encoded array should only contain scalars and arrays.
4141
*
42+
* Stamps that implement NonSendableStampInterface should
43+
* not be encoded.
44+
*
4245
* The most common keys of the encoded array are:
4346
* - `body` (string) - the message body
4447
* - `headers` (string<string>) - a key/value pair of headers

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.