Skip to content

[Messenger] Add HeaderStamp to add custom headers #34481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG-5.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CHANGELOG for 5.1.x
===================

This changelog references the relevant changes (bug and security fixes) done
in 5.1 minor versions.

To get the diff for a specific change, go to https://github.com/symfony/symfony/commit/XXX where XXX is the change hash
To get the diff between two versions, go to https://github.com/symfony/symfony/compare/v5.1.0...v5.1.1

* 5.1.0-BETA1

* feature #34481 [Messenger] Add HeaderStamp to add custom headers in the encoded message (alanpoulain)
39 changes: 39 additions & 0 deletions src/Symfony/Component/Messenger/Stamp/HeaderStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Stamp;

/**
* Stamp to add a custom header to be used by the transport.
*
* @author Alan Poulain <contact@alanpoulain.eu>
*/
final class HeaderStamp implements StampInterface
{
private $headerName;
private $headerValue;

public function __construct(string $headerName, string $headerValue)
{
$this->headerName = $headerName;
$this->headerValue = $headerValue;
}

public function getHeaderName(): string
{
return $this->headerName;
}

public function getHeaderValue(): string
{
return $this->headerValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\HeaderStamp;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Messenger\Stamp\SerializerStamp;
use Symfony\Component\Messenger\Stamp\ValidationStamp;
Expand Down Expand Up @@ -106,6 +107,23 @@ public function testEncodedWithSymfonySerializerForStamps()
$this->assertArrayHasKey('X-Message-Stamp-'.ValidationStamp::class, $encoded['headers']);
}

public function testEncodedWithHeaderStamps()
{
$serializer = new Serializer();

$envelope = (new Envelope(new DummyMessage('test')))
->with(new HeaderStamp('X-Custom-Header', 'foo'))
->with(new HeaderStamp('X-Custom-Header-Bis', 'bar'));

$encoded = $serializer->encode($envelope);

$this->assertArrayHasKey('headers', $encoded);
$this->assertArrayHasKey('X-Custom-Header', $encoded['headers']);
$this->assertSame($encoded['headers']['X-Custom-Header'], 'foo');
$this->assertArrayHasKey('X-Custom-Header-Bis', $encoded['headers']);
$this->assertSame($encoded['headers']['X-Custom-Header-Bis'], 'bar');
}

public function testDecodeWithSymfonySerializerStamp()
{
$serializer = new Serializer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Stamp\HeaderStamp;
use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Messenger\Stamp\SerializerStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
Expand Down Expand Up @@ -136,6 +137,14 @@ private function encodeStamps(Envelope $envelope): array

$headers = [];
foreach ($allStamps as $class => $stamps) {
if (HeaderStamp::class === $class) {
foreach ($stamps as $stamp) {
$headers[$stamp->getHeaderName()] = $stamp->getHeaderValue();
}

continue;
}

$headers[self::STAMP_HEADER_PREFIX.$class] = $this->serializer->serialize($stamps, $this->format, $this->context);
}

Expand Down