Skip to content

[Messenger] Introduce SelfStampableInterface #54366

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

Open
wants to merge 2 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Introduce SelfStampableInterface
  • Loading branch information
VincentLanglet committed Mar 19, 2025
commit 70d6d27eaa4fcf181364228264d38d31be74a19a
22 changes: 22 additions & 0 deletions src/Symfony/Component/Messenger/Message/SelfStampableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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\Message;

use Symfony\Component\Messenger\Stamp\StampInterface;

interface SelfStampableInterface
{
/**
* @return array<StampInterface>
*/
public function getStamps(): array;
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger;

use Symfony\Component\Messenger\Message\SelfStampableInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackMiddleware;

Expand Down Expand Up @@ -53,6 +54,10 @@ public function getIterator(): \Traversable

public function dispatch(object $message, array $stamps = []): Envelope
{
if ($message instanceof SelfStampableInterface) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find a better place to automatically add the stamp since it's the place where

$envelope = Envelope::wrap($message, $stamps);

is done.

And then return $middlewareIterator->current()->handle($envelope, $stack); is done.

But if I introduce a SelfStampableMiddleware to automatically add the stamp, I cannot be sure:

  • The middleware is used
  • The middleware is the first one

So the same issue would kinda occurs no ?

$stamps = array_merge($message->getStamps(), $stamps);
}

$envelope = Envelope::wrap($message, $stamps);
$middlewareIterator = $this->middlewareAggregate->getIterator();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Symfony\Component\Messenger\Tests\Fixtures;

use Symfony\Component\Messenger\Message\SelfStampableInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;

class SelfStampableDummyMessage implements DummyMessageInterface, SelfStampableInterface
{
public function __construct(private string $message)
{
}

public function getMessage(): string
{
return $this->message;
}

public function getStamps(): array
{
return [new DelayStamp(1)];
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/Messenger/Tests/MessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\SelfStampableDummyMessage;

class MessageBusTest extends TestCase
{
Expand Down Expand Up @@ -133,6 +134,14 @@ public function testItAddsTheStampsToEnvelope()
$this->assertCount(2, $finalEnvelope->all());
}

public function testSelfStampableMessage()
{
$finalEnvelope = (new MessageBus())->dispatch(new SelfStampableDummyMessage(''), [new DelayStamp(5), new BusNameStamp('bar')]);
$this->assertCount(2, $finalEnvelope->all());
$this->assertCount(2, $finalEnvelope->all()[DelayStamp::class]);
$this->assertSame(5, $finalEnvelope->last(DelayStamp::class)->getDelay());
}

public static function provideConstructorDataStucture(): iterable
{
yield 'iterator' => [new \ArrayObject([
Expand Down