Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit48a75e3

Browse files
committed
[Messenger] DeprecateHandleTrait in favor of a newSingleHandlingTrait
1 parentc4e97eb commit48a75e3

File tree

10 files changed

+280
-2
lines changed

10 files changed

+280
-2
lines changed

‎UPGRADE-7.1.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
UPGRADE FROM 7.0 to 7.1
22
=======================
33

4+
Messenger
5+
---------
6+
7+
* Deprecate`HandleTrait`, use`SingleHandlingTrait` instead
8+
49
Workflow
510
--------
611

‎src/Symfony/Component/Messenger/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add option`redis_sentinel` as an alias for`sentinel_master`
88
* Add`--all` option to the`messenger:consume` command
9+
* Deprecate`HandleTrait`, use`SingleHandlingTrait` instead
910

1011
7.0
1112
---

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
useSymfony\Component\Messenger\Exception\LogicException;
1515
useSymfony\Component\Messenger\Stamp\HandledStamp;
1616

17+
trigger_deprecation('symfony/messenger','7.1','The "%s" class is deprecated, use "%s" instead.', HandleTrait::class, SingleHandlingTrait::class);
18+
1719
/**
1820
* Leverages a message bus to expect a single, synchronous message handling and return its result.
1921
*
2022
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
23+
*
24+
* @deprecated since Symfony 7.1, use SingleHandlingTrait instead.
2125
*/
2226
trait HandleTrait
2327
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger;
13+
14+
useSymfony\Component\Messenger\Exception\HandlerFailedException;
15+
useSymfony\Component\Messenger\Exception\LogicException;
16+
useSymfony\Component\Messenger\Stamp\HandledStamp;
17+
18+
trait SingleHandlingTrait
19+
{
20+
privateMessageBusInterface$messageBus;
21+
22+
/**
23+
* Dispatches the given message, expecting to be handled by a single handler
24+
* and returns the result from the handler returned value.
25+
* This behavior is useful for both synchronous command & query buses,
26+
* the last one usually returning the handler result.
27+
*
28+
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
29+
*/
30+
privatefunctionhandle(object$message):mixed
31+
{
32+
if (!isset($this->messageBus)) {
33+
thrownewLogicException(sprintf('You must provide a "%s" instance in the "%s::$messageBus" property, but that property has not been initialized yet.', MessageBusInterface::class,static::class));
34+
}
35+
36+
$exceptions = [];
37+
38+
try {
39+
$envelope =$this->messageBus->dispatch($message);
40+
}catch (HandlerFailedException$exception) {
41+
$envelope =$exception->getEnvelope();
42+
$exceptions =$exception->getWrappedExceptions();
43+
}
44+
45+
/** @var HandledStamp[] $handledStamps */
46+
$handledStamps =$envelope->all(HandledStamp::class);
47+
48+
$handlers =array_merge(
49+
array_map(staticfn (HandledStamp$stamp) =>$stamp->getHandlerName(),$handledStamps),
50+
array_keys($exceptions),
51+
);
52+
53+
if (!$handlers) {
54+
thrownewLogicException(sprintf('Message of type "%s" was handled zero times. Exactly one handler is expected when using "%s::%s()".',$envelope->getMessage()::class,static::class,__FUNCTION__));
55+
}
56+
57+
if (\count($handlers) >1) {
58+
thrownewLogicException(sprintf('Message of type "%s" was handled multiple times. Only one handler is expected when using "%s::%s()", got %d: "%s".',$envelope->getMessage()::class,static::class,__FUNCTION__,\count($handlers),implode('", "',$handlers)));
59+
}
60+
61+
if ($exceptions) {
62+
throwreset($exceptions);
63+
}
64+
65+
return$handledStamps[0]->getResult();
66+
}
67+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Tests\Fixtures;
13+
14+
useSymfony\Component\Messenger\MessageBusInterface;
15+
useSymfony\Component\Messenger\SingleHandlingTrait;
16+
17+
/**
18+
* @see \Symfony\Component\Messenger\Tests\TraceableMessageBusTest::testItTracesDispatchWhenSingleHandlingTraitIsUsed
19+
*/
20+
class TestTracesWithSingleHandlingTraitAction
21+
{
22+
use SingleHandlingTrait;
23+
24+
publicfunction__construct(MessageBusInterface$messageBus)
25+
{
26+
$this->messageBus =$messageBus;
27+
}
28+
29+
publicfunction__invoke($message)
30+
{
31+
$this->handle($message);
32+
}
33+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
useSymfony\Component\Messenger\Stamp\HandledStamp;
2121
useSymfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2222

23+
/**
24+
* @group legacy
25+
*/
2326
class HandleTraitTestextends TestCase
2427
{
2528
publicfunctiontestItThrowsOnNoMessageBusInstance()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Messenger\Tests;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Messenger\Envelope;
16+
useSymfony\Component\Messenger\Exception\HandlerFailedException;
17+
useSymfony\Component\Messenger\Exception\LogicException;
18+
useSymfony\Component\Messenger\MessageBus;
19+
useSymfony\Component\Messenger\MessageBusInterface;
20+
useSymfony\Component\Messenger\SingleHandlingTrait;
21+
useSymfony\Component\Messenger\Stamp\HandledStamp;
22+
useSymfony\Component\Messenger\Tests\Fixtures\DummyMessage;
23+
24+
class SingleHandlingTraitTestextends TestCase
25+
{
26+
publicfunctiontestItThrowsOnNoMessageBusInstance()
27+
{
28+
$this->expectException(LogicException::class);
29+
$this->expectExceptionMessage('You must provide a "Symfony\Component\Messenger\MessageBusInterface" instance in the "Symfony\Component\Messenger\Tests\SingleHandlerBus::$messageBus" property, but that property has not been initialized yet.');
30+
$singleHandlerBus =newSingleHandlerBus(null);
31+
$message =newDummyMessage('Hello');
32+
33+
$singleHandlerBus->dispatch($message);
34+
}
35+
36+
publicfunctiontestHandleReturnsHandledStampResult()
37+
{
38+
$bus =$this->createMock(MessageBus::class);
39+
$singleHandlerBus =newSingleHandlerBus($bus);
40+
41+
$message =newDummyMessage('Hello');
42+
$bus->expects($this->once())->method('dispatch')->willReturn(
43+
newEnvelope($message, [newHandledStamp('result','DummyHandler::__invoke')])
44+
);
45+
46+
$this->assertSame('result',$singleHandlerBus->dispatch($message));
47+
}
48+
49+
publicfunctiontestHandleAcceptsEnvelopes()
50+
{
51+
$bus =$this->createMock(MessageBus::class);
52+
$singleHandlerBus =newSingleHandlerBus($bus);
53+
54+
$envelope =newEnvelope(newDummyMessage('Hello'), [newHandledStamp('result','DummyHandler::__invoke')]);
55+
$bus->expects($this->once())->method('dispatch')->willReturn($envelope);
56+
57+
$this->assertSame('result',$singleHandlerBus->dispatch($envelope));
58+
}
59+
60+
publicfunctiontestHandleThrowsOnNoHandledStamp()
61+
{
62+
$this->expectException(LogicException::class);
63+
$this->expectExceptionMessage('Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" was handled zero times. Exactly one handler is expected when using "Symfony\Component\Messenger\Tests\SingleHandlerBus::handle()".');
64+
$bus =$this->createMock(MessageBus::class);
65+
$singleHandlerBus =newSingleHandlerBus($bus);
66+
67+
$message =newDummyMessage('Hello');
68+
$bus->expects($this->once())->method('dispatch')->willReturn(newEnvelope($message));
69+
70+
$singleHandlerBus->dispatch($message);
71+
}
72+
73+
publicfunctiontestHandleThrowsOnMultipleHandledStamps()
74+
{
75+
$this->expectException(LogicException::class);
76+
$this->expectExceptionMessage('Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" was handled multiple times. Only one handler is expected when using "Symfony\Component\Messenger\Tests\SingleHandlerBus::handle()", got 2: "FirstDummyHandler::__invoke", "SecondDummyHandler::__invoke".');
77+
$bus =$this->createMock(MessageBus::class);
78+
$singleHandlerBus =newSingleHandlerBus($bus);
79+
80+
$message =newDummyMessage('Hello');
81+
$bus->expects($this->once())->method('dispatch')->willThrowException(
82+
newHandlerFailedException(
83+
newEnvelope($message, [newHandledStamp('first_result','FirstDummyHandler::__invoke')]),
84+
['SecondDummyHandler::__invoke' =>new \RuntimeException('SecondDummyHandler failed.')]
85+
)
86+
);
87+
88+
$singleHandlerBus->dispatch($message);
89+
}
90+
91+
publicfunctiontestHandleThrowsWrappedException()
92+
{
93+
$bus =$this->createMock(MessageBus::class);
94+
$singleHandlerBus =newSingleHandlerBus($bus);
95+
96+
$message =newDummyMessage('Hello');
97+
$wrappedException =new \RuntimeException('Handler failed.');
98+
$bus->expects($this->once())->method('dispatch')->willThrowException(
99+
newHandlerFailedException(
100+
newEnvelope($message),
101+
['DummyHandler::__invoke' =>new \RuntimeException('Handler failed.')]
102+
)
103+
);
104+
105+
$this->expectException($wrappedException::class);
106+
$this->expectExceptionMessage($wrappedException->getMessage());
107+
108+
$singleHandlerBus->dispatch($message);
109+
}
110+
}
111+
112+
class SingleHandlerBus
113+
{
114+
use SingleHandlingTrait;
115+
116+
publicfunction__construct(?MessageBusInterface$messageBus)
117+
{
118+
if ($messageBus) {
119+
$this->messageBus =$messageBus;
120+
}
121+
}
122+
123+
publicfunctiondispatch($message):string
124+
{
125+
return$this->handle($message);
126+
}
127+
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@
1212
namespaceSymfony\Component\Messenger\Tests;
1313

1414
usePHPUnit\Framework\TestCase;
15+
useSymfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
useSymfony\Component\Messenger\Envelope;
1617
useSymfony\Component\Messenger\MessageBusInterface;
1718
useSymfony\Component\Messenger\Stamp\DelayStamp;
1819
useSymfony\Component\Messenger\Stamp\HandledStamp;
1920
useSymfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
2021
useSymfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2122
useSymfony\Component\Messenger\Tests\Fixtures\TestTracesWithHandleTraitAction;
23+
useSymfony\Component\Messenger\Tests\Fixtures\TestTracesWithSingleHandlingTraitAction;
2224
useSymfony\Component\Messenger\TraceableMessageBus;
2325

2426
class TraceableMessageBusTestextends TestCase
2527
{
28+
use ExpectDeprecationTrait;
29+
30+
/**
31+
* @group legacy
32+
*/
2633
publicfunctiontestItTracesDispatch()
2734
{
35+
$this->expectDeprecation('Since symfony/messenger 7.1: The "Symfony\Component\Messenger\HandleTrait" class is deprecated, use "Symfony\Component\Messenger\SingleHandlingTrait" instead.');
36+
2837
$message =newDummyMessage('Hello');
2938

3039
$stamp =newDelayStamp(5);
@@ -49,6 +58,9 @@ public function testItTracesDispatch()
4958
],$actualTracedMessage);
5059
}
5160

61+
/**
62+
* @group legacy
63+
*/
5264
publicfunctiontestItTracesDispatchWhenHandleTraitIsUsed()
5365
{
5466
$message =newDummyMessage('Hello');
@@ -73,6 +85,30 @@ public function testItTracesDispatchWhenHandleTraitIsUsed()
7385
],$actualTracedMessage);
7486
}
7587

88+
publicfunctiontestItTracesDispatchWhenSingleHandlingTraitIsUsed()
89+
{
90+
$message =newDummyMessage('Hello');
91+
92+
$bus =$this->createMock(MessageBusInterface::class);
93+
$bus->expects($this->once())->method('dispatch')->with($message)->willReturn((newEnvelope($message))->with($stamp =newHandledStamp('result','handlerName')));
94+
95+
$traceableBus =newTraceableMessageBus($bus);
96+
(newTestTracesWithSingleHandlingTraitAction($traceableBus))($message);
97+
$this->assertCount(1,$tracedMessages =$traceableBus->getDispatchedMessages());
98+
$actualTracedMessage =$tracedMessages[0];
99+
unset($actualTracedMessage['callTime']);// don't check, too variable
100+
$this->assertEquals([
101+
'message' =>$message,
102+
'stamps' => [],
103+
'stamps_after_dispatch' => [$stamp],
104+
'caller' => [
105+
'name' =>'TestTracesWithSingleHandlingTraitAction.php',
106+
'file' => (new \ReflectionClass(TestTracesWithSingleHandlingTraitAction::class))->getFileName(),
107+
'line' => (new \ReflectionMethod(TestTracesWithSingleHandlingTraitAction::class,'__invoke'))->getStartLine() +2,
108+
],
109+
],$actualTracedMessage);
110+
}
111+
76112
publicfunctiontestItTracesDispatchWithEnvelope()
77113
{
78114
$message =newDummyMessage('Hello');

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ private function getCaller(): array
6363
$line =$trace[1]['line'] ??null;
6464

6565
$handleTraitFile = (new \ReflectionClass(HandleTrait::class))->getFileName();
66+
$singleHandlingTraitFile = (new \ReflectionClass(SingleHandlingTrait::class))->getFileName();
6667
$found =false;
6768
for ($i =1;$i <8; ++$i) {
68-
if (isset($trace[$i]['file'],$trace[$i +1]['file'],$trace[$i +1]['line']) &&$trace[$i]['file'] ===$handleTraitFile) {
69+
if (isset($trace[$i]['file'],$trace[$i +1]['file'],$trace[$i +1]['line']) &&($trace[$i]['file'] ===$singleHandlingTraitFile ||$trace[$i]['file'] ===$handleTraitFile)) {
6970
$file =$trace[$i +1]['file'];
7071
$line =$trace[$i +1]['line'];
7172
$found =true;

‎src/Symfony/Component/Messenger/composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php":">=8.2",
2020
"psr/log":"^1|^2|^3",
21-
"symfony/clock":"^6.4|^7.0"
21+
"symfony/clock":"^6.4|^7.0",
22+
"symfony/deprecation-contracts":"^2.5|^3"
2223
},
2324
"require-dev": {
2425
"psr/cache":"^1.0|^2.0|^3.0",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp