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

[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
Loading
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
Prev Previous commit
Rework
  • Loading branch information
VincentLanglet committed Mar 19, 2025
commit 8f0da1ca98d70389e52c650eb519d0a45c931b59
Original file line number Diff line number Diff line change
Expand Up @@ -2300,6 +2300,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder

$defaultMiddleware = [
'before' => [
['id' => 'add_self_stampable_stamps_middleware'],
['id' => 'add_bus_name_stamp_middleware'],
['id' => 'reject_redelivered_message_middleware'],
['id' => 'dispatch_after_current_bus'],
Expand All @@ -2325,7 +2326,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
$defaultMiddleware['after'][1]['arguments'] = [$bus['default_middleware']['allow_no_handlers']];

// argument to add_bus_name_stamp_middleware
$defaultMiddleware['before'][0]['arguments'] = [$busId];
$defaultMiddleware['before'][1]['arguments'] = [$busId];

$middleware = array_merge($defaultMiddleware['before'], $middleware, $defaultMiddleware['after']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Messenger\EventListener\StopWorkerOnRestartSignalListener;
use Symfony\Component\Messenger\Handler\RedispatchMessageHandler;
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
use Symfony\Component\Messenger\Middleware\AddSelfStampableStampsMiddleware;
use Symfony\Component\Messenger\Middleware\DeduplicateMiddleware;
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
use Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware;
Expand Down Expand Up @@ -92,6 +93,8 @@
service('lock.factory'),
])

->set('messenger.middleware.add_self_stampable_stamps_middleware', AddSelfStampableStampsMiddleware::class)

->set('messenger.middleware.add_bus_name_stamp_middleware', AddBusNameStampMiddleware::class)
->abstract()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,7 @@ public function testMessengerWithMultipleBusesWithoutDeduplicateMiddleware()
$this->assertTrue($container->has('messenger.bus.commands'));
$this->assertSame([], $container->getDefinition('messenger.bus.commands')->getArgument(0));
$this->assertEquals([
['id' => 'add_self_stampable_stamps_middleware'],
['id' => 'add_bus_name_stamp_middleware', 'arguments' => ['messenger.bus.commands']],
['id' => 'reject_redelivered_message_middleware'],
['id' => 'dispatch_after_current_bus'],
Expand Down Expand Up @@ -1104,6 +1105,7 @@ public function testMessengerWithMultipleBusesWithDeduplicateMiddleware()
$this->assertTrue($container->has('messenger.bus.commands'));
$this->assertSame([], $container->getDefinition('messenger.bus.commands')->getArgument(0));
$this->assertEquals([
['id' => 'add_self_stampable_stamps_middleware'],
['id' => 'add_bus_name_stamp_middleware', 'arguments' => ['messenger.bus.commands']],
['id' => 'reject_redelivered_message_middleware'],
['id' => 'dispatch_after_current_bus'],
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `CloseableTransportInterface` to allow closing the transport
* Add `SentForRetryStamp` that identifies whether a failed message was sent for retry
* Add `Symfony\Component\Messenger\Middleware\DeduplicateMiddleware` and `Symfony\Component\Messenger\Stamp\DeduplicateStamp`
* Add `Symfony\Component\Messenger\Middleware\AddSelfStampableStampsMiddleware` and `Symfony\Component\Messenger\Message\SelfStampableInterface`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
interface SelfStampableInterface
{
/**
* List of stamps which will be automatically added to the envelope,
* if there is no other stamp of the same class already set.
*
* @return array<StampInterface>
*/
public function getStamps(): array;
public function getDefaultStamps(): array;
}
5 changes: 0 additions & 5 deletions 5 src/Symfony/Component/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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 @@ -54,10 +53,6 @@ public function getIterator(): \Traversable

public function dispatch(object $message, array $stamps = []): Envelope
{
if ($message instanceof SelfStampableInterface) {
$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,35 @@
<?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\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Message\SelfStampableInterface;

/**
* Automatically add stamps from the SelfStampableInterface.
*/
class AddSelfStampableStampsMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$message = $envelope->getMessage();
if ($message instanceof SelfStampableInterface) {
foreach ($message->getDefaultStamps() as $stamp) {
if (null === $envelope->last($stamp::class)) {
$envelope = $envelope->with($stamp);
}
}
}

return $stack->next()->handle($envelope, $stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getMessage(): string
return $this->message;
}

public function getStamps(): array
public function getDefaultStamps(): array
{
return [new DelayStamp(1)];
}
Expand Down
9 changes: 0 additions & 9 deletions 9 src/Symfony/Component/Messenger/Tests/MessageBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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 @@ -134,14 +133,6 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Tests\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\AddSelfStampableStampsMiddleware;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\SelfStampableDummyMessage;

final class AddSelfStampableStampsMiddlewareTest extends MiddlewareTestCase
{
public function testSelfStampableStampsMiddleware()
{
$message = new SelfStampableDummyMessage('');
$envelope = new Envelope($message);

$decorator = new AddSelfStampableStampsMiddleware();

$envelope = $decorator->handle($envelope, $this->getStackMock(true));

$delayStamp = $envelope->last(DelayStamp::class);
$this->assertNotNull($delayStamp);
$this->assertSame(1, $delayStamp->getDelay());
}

public function testSelfStampableStampsMiddlewareIfStampExists()
{
$message = new SelfStampableDummyMessage('');
$envelope = new Envelope($message, [new DelayStamp(5)]);

$decorator = new AddSelfStampableStampsMiddleware();

$envelope = $decorator->handle($envelope, $this->getStackMock(true));

$delayStamp = $envelope->last(DelayStamp::class);
$this->assertNotNull($delayStamp);
$this->assertSame(5, $delayStamp->getDelay());
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.