Skip to content

Commit c6baf1b

Browse files
committed
feature #42124 [Messenger] Add $stamps parameter to HandleTrait::handle (alexander-schranz)
This PR was merged into the 7.3 branch. Discussion ---------- [Messenger] Add `$stamps` parameter to `HandleTrait::handle` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | -- | License | MIT | Doc PR | TODO Like the `MessageBusInterface::dispatch` it would be great to use stamps. We are using example a custom middleware to control if a flush should be done. ```php $this->handle(new CreateUserMessage($data), [new DoctrineFlushStamp()]); ``` Also a custom middleware for locking: ```php $this->handle(new OurMessage(), [new LockStamp('lock-key', 300.0, true)])); ``` Commits ------- 1742f67 Add stamps to handle trait
2 parents 764fe52 + 1742f67 commit c6baf1b

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add `SentForRetryStamp` that identifies whether a failed message was sent for retry
99
* Add `Symfony\Component\Messenger\Middleware\DeduplicateMiddleware` and `Symfony\Component\Messenger\Stamp\DeduplicateStamp`
1010
* Add `--class-filter` option to the `messenger:failed:remove` command
11+
* Add `$stamps` parameter to `HandleTrait::handle`
1112

1213
7.2
1314
---

src/Symfony/Component/Messenger/HandleTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Messenger\Exception\LogicException;
1515
use Symfony\Component\Messenger\Stamp\HandledStamp;
16+
use Symfony\Component\Messenger\Stamp\StampInterface;
1617

1718
/**
1819
* Leverages a message bus to expect a single, synchronous message handling and return its result.
@@ -29,15 +30,16 @@ trait HandleTrait
2930
* This behavior is useful for both synchronous command & query buses,
3031
* the last one usually returning the handler result.
3132
*
32-
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
33+
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
34+
* @param StampInterface[] $stamps Stamps to be set on the Envelope which are used to control middleware behavior
3335
*/
34-
private function handle(object $message): mixed
36+
private function handle(object $message, array $stamps = []): mixed
3537
{
3638
if (!isset($this->messageBus)) {
3739
throw new LogicException(\sprintf('You must provide a "%s" instance in the "%s::$messageBus" property, but that property has not been initialized yet.', MessageBusInterface::class, static::class));
3840
}
3941

40-
$envelope = $this->messageBus->dispatch($message);
42+
$envelope = $this->messageBus->dispatch($message, $stamps);
4143
/** @var HandledStamp[] $handledStamps */
4244
$handledStamps = $envelope->all(HandledStamp::class);
4345

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface MessageBusInterface
2323
* Dispatches the given message.
2424
*
2525
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
26-
* @param StampInterface[] $stamps
26+
* @param StampInterface[] $stamps Stamps set on the Envelope which are used to control middleware behavior
2727
*
2828
* @throws ExceptionInterface
2929
*/

src/Symfony/Component/Messenger/Tests/HandleTraitTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Messenger\MessageBus;
1919
use Symfony\Component\Messenger\MessageBusInterface;
2020
use Symfony\Component\Messenger\Stamp\HandledStamp;
21+
use Symfony\Component\Messenger\Stamp\StampInterface;
2122
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2223

2324
class HandleTraitTest extends TestCase
@@ -56,6 +57,20 @@ public function testHandleAcceptsEnvelopes()
5657
$this->assertSame('result', $queryBus->query($envelope));
5758
}
5859

60+
public function testHandleWithStamps()
61+
{
62+
$bus = $this->createMock(MessageBus::class);
63+
$queryBus = new TestQueryBus($bus);
64+
$stamp = $this->createMock(StampInterface::class);
65+
66+
$query = new DummyMessage('Hello');
67+
$bus->expects($this->once())->method('dispatch')->with($query, [$stamp])->willReturn(
68+
new Envelope($query, [new HandledStamp('result', 'DummyHandler::__invoke')])
69+
);
70+
71+
$queryBus->query($query, [$stamp]);
72+
}
73+
5974
public function testHandleThrowsOnNoHandledStamp()
6075
{
6176
$this->expectException(LogicException::class);
@@ -96,8 +111,8 @@ public function __construct(?MessageBusInterface $messageBus)
96111
}
97112
}
98113

99-
public function query($query): string
114+
public function query($query, array $stamps = []): string
100115
{
101-
return $this->handle($query);
116+
return $this->handle($query, $stamps);
102117
}
103118
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy