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 6545805

Browse filesBrowse files
committed
feature #31471 [Messenger] Add "non sendable" stamps (weaverryan)
This PR was merged into the 4.3 branch. Discussion ---------- [Messenger] Add "non sendable" stamps | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #31460 | License | MIT | Doc PR | not needed Fixes a bug where Symfony serialization of the AmqpReceivedStamp sometimes caused problems. It's still a mystery why the `AmqpReceivedStamp` caused a segfault *sometimes* when going through the Symfony serializer or the `VarDumper`. But, that stamp really didn't need to be sent on redelivery anyways. I don't love making the removal the responsibility of the serializers, but it didn't work well anywhere else. Cheers! Commits ------- 34e7781 Adding a new NonSendableStampInterface to avoid sending certain stamps
2 parents 6fb05dc + 34e7781 commit 6545805
Copy full SHA for 6545805

File tree

12 files changed

+128
-6
lines changed
Filter options

12 files changed

+128
-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
* Removed argument `?bool &$handle = false` from `SendersLocatorInterface::getSenders`

‎src/Symfony/Component/Messenger/Envelope.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Envelope.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ public function withoutAll(string $stampFqcn): self
8080
return $cloned;
8181
}
8282

83+
/**
84+
* Removes all stamps that implement the given type.
85+
*/
86+
public function withoutStampsOfType(string $type): self
87+
{
88+
$cloned = clone $this;
89+
90+
foreach ($cloned->stamps as $class => $stamps) {
91+
if ($class === $type || \is_subclass_of($class, $type)) {
92+
unset($cloned->stamps[$class]);
93+
}
94+
}
95+
96+
return $cloned;
97+
}
98+
8399
public function last(string $stampFqcn): ?StampInterface
84100
{
85101
return isset($this->stamps[$stampFqcn]) ? end($this->stamps[$stampFqcn]) : null;
+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/EnvelopeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/EnvelopeTest.php
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Stamp\DelayStamp;
1717
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
18+
use Symfony\Component\Messenger\Stamp\StampInterface;
1819
use Symfony\Component\Messenger\Stamp\ValidationStamp;
1920
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2021

@@ -50,6 +51,30 @@ public function testWithoutAll()
5051
$this->assertCount(1, $envelope->all(DelayStamp::class));
5152
}
5253

54+
public function testWithoutStampsOfType()
55+
{
56+
$envelope = new Envelope(new DummyMessage('dummy'), [
57+
new ReceivedStamp('transport1'),
58+
new DelayStamp(5000),
59+
new DummyExtendsDelayStamp(5000),
60+
new DummyImplementsFooBarStampInterface(),
61+
]);
62+
63+
$envelope2 = $envelope->withoutStampsOfType(DummyNothingImplementsMeStampInterface::class);
64+
$this->assertEquals($envelope, $envelope2);
65+
66+
$envelope3 = $envelope2->withoutStampsOfType(ReceivedStamp::class);
67+
$this->assertEmpty($envelope3->all(ReceivedStamp::class));
68+
69+
$envelope4 = $envelope3->withoutStampsOfType(DelayStamp::class);
70+
$this->assertEmpty($envelope4->all(DelayStamp::class));
71+
$this->assertEmpty($envelope4->all(DummyExtendsDelayStamp::class));
72+
73+
$envelope5 = $envelope4->withoutStampsOfType(DummyFooBarStampInterface::class);
74+
$this->assertEmpty($envelope5->all(DummyImplementsFooBarStampInterface::class));
75+
$this->assertEmpty($envelope5->all());
76+
}
77+
5378
public function testLast()
5479
{
5580
$receivedStamp = new ReceivedStamp('transport');
@@ -92,3 +117,16 @@ public function testWrapWithEnvelope()
92117
$this->assertCount(1, $envelope->all(ReceivedStamp::class));
93118
}
94119
}
120+
121+
class DummyExtendsDelayStamp extends DelayStamp
122+
{
123+
}
124+
interface DummyFooBarStampInterface extends StampInterface
125+
{
126+
}
127+
interface DummyNothingImplementsMeStampInterface extends StampInterface
128+
{
129+
}
130+
class DummyImplementsFooBarStampInterface implements DummyFooBarStampInterface
131+
{
132+
}

‎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+
}
79+
}
80+
81+
class DummyPhpSerializerNonSendableStamp implements NonSendableStampInterface
82+
{
6683
}

‎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+
}
209+
}
210+
class DummySymfonySerializerNonSendableStamp implements NonSendableStampInterface
211+
{
196212
}

‎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
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Messenger\Envelope;
1515
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
16+
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1617

1718
/**
1819
* @author Ryan Weaver<ryan@symfonycasts.com>
@@ -40,6 +41,8 @@ public function decode(array $encodedEnvelope): Envelope
4041
*/
4142
public function encode(Envelope $envelope): array
4243
{
44+
$envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);
45+
4346
$body = addslashes(serialize($envelope));
4447

4548
return [

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php
+3Lines changed: 3 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;
@@ -98,6 +99,8 @@ public function encode(Envelope $envelope): array
9899
$context = $serializerStamp->getContext() + $context;
99100
}
100101

102+
$envelope = $envelope->withoutStampsOfType(NonSendableStampInterface::class);
103+
101104
$headers = ['type' => \get_class($envelope->getMessage())] + $this->encodeStamps($envelope);
102105

103106
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.