Skip to content

Navigation Menu

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] Add auto stamp attribute and middleware #50812

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.3
Choose a base branch
Loading
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\Messenger\EventListener\StopWorkerOnSignalsListener;
use Symfony\Component\Messenger\Handler\RedispatchMessageHandler;
use Symfony\Component\Messenger\Middleware\AddBusNameStampMiddleware;
use Symfony\Component\Messenger\Middleware\AutoStampMiddleware;
use Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware;
use Symfony\Component\Messenger\Middleware\FailedMessageProcessingMiddleware;
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
Expand Down Expand Up @@ -112,6 +113,8 @@
service('router'),
])

->set('messenger.middleware.auto_stamp_middleware', AutoStampMiddleware::class)

// Discovery
->set('messenger.receiver_locator', ServiceLocator::class)
->args([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AmazonSqsFifoStamp implements NonSendableStampInterface
{
private ?string $messageGroupId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AmazonSqsReceivedStamp implements NonSendableStampInterface
{
private string $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AmazonSqsXrayTraceHeaderStamp implements NonSendableStampInterface
{
private string $traceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* Stamp applied when a message is received from Amqp.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AmqpReceivedStamp implements NonSendableStampInterface
{
private \AMQPEnvelope $amqpEnvelope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @author Guillaume Gammelin <ggammelin@gmail.com>
* @author Samuel Roze <samuel.roze@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AmqpStamp implements NonSendableStampInterface
{
private ?string $routingKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @author Antonio Pauletich <antonio.pauletich95@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class BeanstalkdReceivedStamp implements NonSendableStampInterface
{
private string $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @author Vincent Touzet <vincent.touzet@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class DoctrineReceivedStamp implements NonSendableStampInterface
{
private string $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @author Alexander Schranz <alexander@sulu.io>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class RedisReceivedStamp implements NonSendableStampInterface
{
private string $id;
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 @@ -16,6 +16,7 @@ CHANGELOG
SIGTERM by default
* Add `RedispatchMessage` and `RedispatchMessageHandler`
* Add optional parameter `$isSameDatabase` to `DoctrineTransport::configureSchema()`
* Add `AutoStampMiddleware` that register envelope stamps from message attributes

6.2
---
Expand Down
35 changes: 35 additions & 0 deletions 35 src/Symfony/Component/Messenger/Middleware/AutoStampMiddleware.php
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\Stamp\StampInterface;

/**
* Middleware that add stamps configured on message.
*
* @author Kerian Montes <kerianmontes@gmail.com>
*/
class AutoStampMiddleware implements MiddlewareInterface
{
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
$class = new \ReflectionClass($envelope->getMessage());
do {
foreach ($class->getAttributes(StampInterface::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$envelope = $envelope->with($attribute->newInstance());
}
} while ($class = $class->getParentClass());

return $stack->next()->handle($envelope, $stack);
}
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/AckStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* Marker stamp for messages that can be ack/nack'ed.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class AckStamp implements NonSendableStampInterface
{
private $ack;
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/BusNameStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class BusNameStamp implements StampInterface
{
private string $busName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* A marker that this message was consumed by a worker process.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class ConsumedByWorkerStamp implements NonSendableStampInterface
{
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/DelayStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* Apply this stamp to delay delivery of your message on a transport.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class DelayStamp implements StampInterface
{
private int $delay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class DispatchAfterCurrentBusStamp implements NonSendableStampInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/**
* Stamp applied when a messages fails due to an exception in the handler.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class ErrorDetailsStamp implements StampInterface
{
private string $exceptionClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* Marker telling that any batch handlers bound to the envelope should be flushed.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class FlushBatchHandlersStamp implements NonSendableStampInterface
{
private $force;
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/HandledStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class HandledStamp implements StampInterface
{
private mixed $result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @author Jáchym Toušek <enumag@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class HandlerArgumentsStamp implements NonSendableStampInterface
{
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class MessageDecodingFailedStamp implements StampInterface
{
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/NoAutoAckStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* Marker telling that ack should not be done automatically for this message.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class NoAutoAckStamp implements NonSendableStampInterface
{
private $handlerDescriptor;
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/ReceivedStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
* @author Samuel Roze <samuel.roze@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class ReceivedStamp implements NonSendableStampInterface
{
private string $transportName;
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/RedeliveryStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* Stamp applied when a messages needs to be redelivered.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class RedeliveryStamp implements StampInterface
{
private int $retryCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @author Jérémy Derussé <jeremy@derusse.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class RouterContextStamp implements StampInterface
{
private string $baseUrl;
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/SentStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class SentStamp implements NonSendableStampInterface
{
private string $senderClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class SentToFailureTransportStamp implements StampInterface
{
private string $originalReceiverName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Stamp;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class SerializedMessageStamp implements NonSendableStampInterface
{
public function __construct(private string $serializedMessage)
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/SerializerStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class SerializerStamp implements StampInterface
{
private array $context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class TransportMessageIdStamp implements StampInterface
{
private mixed $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* Stamp used to override the transport names specified in the Messenger routing configuration file.
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class TransportNamesStamp implements StampInterface
{
private array $transportNames;
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Messenger/Stamp/ValidationStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class ValidationStamp implements StampInterface
{
private array|GroupSequence $groups;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Messenger\Tests\Fixtures;

use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\ValidationStamp;

#[DelayStamp(123)]
#[ValidationStamp(['Default', 'Extra'])]
class AutoStampedMessage
{
}
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\AutoStampMiddleware;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\ValidationStamp;
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
use Symfony\Component\Messenger\Tests\Fixtures\AutoStampedMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

class AutoStampMiddlewareTest extends MiddlewareTestCase
{
public function testHandleWithoutAutoStampAndNextMiddleware()
{
$message = new DummyMessage('Hey');
$envelope = new Envelope($message);

(new AutoStampMiddleware())->handle($envelope, $this->getStackMock());
$this->assertCount(0, $envelope->all());
}

public function testHandleWithAutoStampAndNextMiddleware()
{
$message = new AutoStampedMessage();
$envelope = new Envelope($message);

$handledEnvelope = (new AutoStampMiddleware())->handle($envelope, $this->getStackMock());
$this->assertCount(2, $handledEnvelope->all());

$delayStamp = $handledEnvelope->last(DelayStamp::class);
$this->assertInstanceOf(DelayStamp::class, $delayStamp);
$this->assertSame(123, $delayStamp->getDelay());

$validationStamp = $handledEnvelope->last(ValidationStamp::class);
$this->assertInstanceOf(ValidationStamp::class, $validationStamp);
$this->assertSame(['Default', 'Extra'], $validationStamp->getGroups());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/**
* @experimental
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
final class ScheduledStamp implements NonSendableStampInterface
{
public function __construct(public readonly MessageContext $messageContext)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.