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

Skip to content

Commit 39bd4e3

Browse files
committed
Add HeaderStamp
1 parent 4e5b153 commit 39bd4e3

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

CHANGELOG-5.1.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CHANGELOG for 5.1.x
2+
===================
3+
4+
This changelog references the relevant changes (bug and security fixes) done
5+
in 5.1 minor versions.
6+
7+
To get the diff for a specific change, go to https://github.com/symfony/symfony/commit/XXX where XXX is the change hash
8+
To get the diff between two versions, go to https://github.com/symfony/symfony/compare/v5.1.0...v5.1.1
9+
10+
* 5.1.0-BETA1
11+
12+
* feature #34481 [Messenger] Add HeaderStamp to add custom headers in the encoded message (alanpoulain)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
* Stamp to add a custom header to be used by the transport.
16+
*
17+
* @author Alan Poulain <[email protected]>
18+
*/
19+
final class HeaderStamp implements StampInterface
20+
{
21+
private $headerName;
22+
private $headerValue;
23+
24+
public function __construct(string $headerName, string $headerValue)
25+
{
26+
$this->headerName = $headerName;
27+
$this->headerValue = $headerValue;
28+
}
29+
30+
public function getHeaderName(): string
31+
{
32+
return $this->headerName;
33+
}
34+
35+
public function getHeaderValue(): string
36+
{
37+
return $this->headerValue;
38+
}
39+
}

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

Lines changed: 18 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\HeaderStamp;
1718
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1819
use Symfony\Component\Messenger\Stamp\SerializerStamp;
1920
use Symfony\Component\Messenger\Stamp\ValidationStamp;
@@ -106,6 +107,23 @@ public function testEncodedWithSymfonySerializerForStamps()
106107
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']);
107108
}
108109

110+
public function testEncodedWithHeaderStamps()
111+
{
112+
$serializer = new Serializer();
113+
114+
$envelope = (new Envelope(new DummyMessage('test')))
115+
->with(new HeaderStamp('X-Custom-Header', 'foo'))
116+
->with(new HeaderStamp('X-Custom-Header-Bis', 'bar'));
117+
118+
$encoded = $serializer->encode($envelope);
119+
120+
$this->assertArrayHasKey('headers', $encoded);
121+
$this->assertArrayHasKey('X-Custom-Header', $encoded['headers']);
122+
$this->assertSame($encoded['headers']['X-Custom-Header'], 'foo');
123+
$this->assertArrayHasKey('X-Custom-Header-Bis', $encoded['headers']);
124+
$this->assertSame($encoded['headers']['X-Custom-Header-Bis'], 'bar');
125+
}
126+
109127
public function testDecodeWithSymfonySerializerStamp()
110128
{
111129
$serializer = new Serializer(

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

Lines changed: 9 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\HeaderStamp;
1718
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
1819
use Symfony\Component\Messenger\Stamp\SerializerStamp;
1920
use Symfony\Component\Messenger\Stamp\StampInterface;
@@ -136,6 +137,14 @@ private function encodeStamps(Envelope $envelope): array
136137

137138
$headers = [];
138139
foreach ($allStamps as $class => $stamps) {
140+
if (HeaderStamp::class === $class) {
141+
foreach ($stamps as $stamp) {
142+
$headers[$stamp->getHeaderName()] = $stamp->getHeaderValue();
143+
}
144+
145+
continue;
146+
}
147+
139148
$headers[self::STAMP_HEADER_PREFIX.$class] = $this->serializer->serialize($stamps, $this->format, $this->context);
140149
}
141150

0 commit comments

Comments
 (0)