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

Commit 2e50e03

Browse filesBrowse files
Introduce SelfStampableInterface
1 parent d90416f commit 2e50e03
Copy full SHA for 2e50e03

File tree

4 files changed

+54
-0
lines changed
Filter options

4 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Message;
4+
5+
use Symfony\Component\Messenger\Stamp\StampInterface;
6+
7+
interface SelfStampableInterface
8+
{
9+
/**
10+
* @return array<StampInterface>
11+
*/
12+
public function getStamps(): array;
13+
}

‎src/Symfony/Component/Messenger/MessageBus.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/MessageBus.php
+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Messenger;
1313

14+
use Symfony\Component\Messenger\Message\SelfStampableInterface;
1415
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1516
use Symfony\Component\Messenger\Middleware\StackMiddleware;
1617

@@ -54,6 +55,10 @@ public function getIterator(): \Traversable
5455

5556
public function dispatch(object $message, array $stamps = []): Envelope
5657
{
58+
if ($message instanceof SelfStampableInterface) {
59+
$stamps = array_merge($message->getStamps(), $stamps);
60+
}
61+
5762
$envelope = Envelope::wrap($message, $stamps);
5863
$middlewareIterator = $this->middlewareAggregate->getIterator();
5964

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\Messenger\Tests\Fixtures;
4+
5+
use Symfony\Component\Messenger\Message\SelfStampableInterface;
6+
use Symfony\Component\Messenger\Stamp\DelayStamp;
7+
8+
class SelfStampableDummyMessage implements DummyMessageInterface, SelfStampableInterface
9+
{
10+
private string $message;
11+
12+
public function __construct(string $message)
13+
{
14+
$this->message = $message;
15+
}
16+
17+
public function getMessage(): string
18+
{
19+
return $this->message;
20+
}
21+
22+
public function getStamps(): array
23+
{
24+
return [new DelayStamp(1)];
25+
}
26+
}

‎src/Symfony/Component/Messenger/Tests/MessageBusTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Tests/MessageBusTest.php
+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2323
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
2424
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
25+
use Symfony\Component\Messenger\Tests\Fixtures\SelfStambableDummyMessage;
26+
use Symfony\Component\Messenger\Tests\Fixtures\SelfStampableDummyMessage;
2527

2628
class MessageBusTest extends TestCase
2729
{
@@ -133,6 +135,14 @@ public function testItAddsTheStampsToEnvelope()
133135
$this->assertCount(2, $finalEnvelope->all());
134136
}
135137

138+
public function testSelfStampableMessage()
139+
{
140+
$finalEnvelope = (new MessageBus())->dispatch(new SelfStampableDummyMessage(''), [new DelayStamp(5), new BusNameStamp('bar')]);
141+
$this->assertCount(2, $finalEnvelope->all());
142+
$this->assertCount(2, $finalEnvelope->all()[DelayStamp::class]);
143+
$this->assertSame(5, $finalEnvelope->last(DelayStamp::class)->getDelay());
144+
}
145+
136146
public static function provideConstructorDataStucture(): iterable
137147
{
138148
yield 'iterator' => [new \ArrayObject([

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.