-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Messenger] Introduce DefaultStampsProviderInterface
#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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Messenger/Message/DefaultStampsProviderInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?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 DefaultStampsProviderInterface | ||
| { | ||
| /** | ||
| * 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 getDefaultStamps(): array; | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Messenger/Middleware/AddDefaultStampsMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\DefaultStampsProviderInterface; | ||
|
|
||
| /** | ||
| * Automatically add stamps from the DefaultStampsProviderInterface. | ||
| */ | ||
| class AddDefaultStampsMiddleware implements MiddlewareInterface | ||
| { | ||
| public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
| { | ||
| $message = $envelope->getMessage(); | ||
| if ($message instanceof DefaultStampsProviderInterface) { | ||
| foreach ($message->getDefaultStamps() as $stamp) { | ||
| if (null === $envelope->last($stamp::class)) { | ||
| $envelope = $envelope->with($stamp); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $stack->next()->handle($envelope, $stack); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/Messenger/Tests/Fixtures/DefaultStampsProviderDummyMessage.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\DefaultStampsProviderInterface; | ||
| use Symfony\Component\Messenger\Stamp\DelayStamp; | ||
|
|
||
| class DefaultStampsProviderDummyMessage implements DummyMessageInterface, DefaultStampsProviderInterface | ||
| { | ||
| public function __construct(private string $message) | ||
| { | ||
| } | ||
|
|
||
| public function getMessage(): string | ||
| { | ||
| return $this->message; | ||
| } | ||
|
|
||
| public function getDefaultStamps(): array | ||
| { | ||
| return [new DelayStamp(1)]; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Messenger/Tests/Middleware/AddDefaultStampsMiddlewareTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\AddDefaultStampsMiddleware; | ||
| use Symfony\Component\Messenger\Stamp\DelayStamp; | ||
| use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; | ||
| use Symfony\Component\Messenger\Tests\Fixtures\DefaultStampsProviderDummyMessage; | ||
|
|
||
| final class AddDefaultStampsMiddlewareTest extends MiddlewareTestCase | ||
| { | ||
| public function testSelfStampableStampsMiddleware() | ||
| { | ||
| $message = new DefaultStampsProviderDummyMessage(''); | ||
| $envelope = new Envelope($message); | ||
|
|
||
| $decorator = new AddDefaultStampsMiddleware(); | ||
|
|
||
| $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 DefaultStampsProviderDummyMessage(''); | ||
| $envelope = new Envelope($message, [new DelayStamp(5)]); | ||
|
|
||
| $decorator = new AddDefaultStampsMiddleware(); | ||
|
|
||
| $envelope = $decorator->handle($envelope, $this->getStackMock(true)); | ||
|
|
||
| $delayStamp = $envelope->last(DelayStamp::class); | ||
| $this->assertNotNull($delayStamp); | ||
| $this->assertSame(5, $delayStamp->getDelay()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is needed for the test PHP 8.4 which is running on Symfony 6.4