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

Commit574c4ea

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

File tree

6 files changed

+206
-1
lines changed

6 files changed

+206
-1
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.',__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()".',get_debug_type($envelope->getMessage()),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".',get_debug_type($envelope->getMessage()),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: 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\TestQueryBus::$messageBus" property, but that property has not been initialized yet.');
30+
$queryBus =newTestQueryBus(null);
31+
$query =newDummyMessage('Hello');
32+
33+
$queryBus->query($query);
34+
}
35+
36+
publicfunctiontestHandleReturnsHandledStampResult()
37+
{
38+
$bus =$this->createMock(MessageBus::class);
39+
$queryBus =newTestQueryBus($bus);
40+
41+
$query =newDummyMessage('Hello');
42+
$bus->expects($this->once())->method('dispatch')->willReturn(
43+
newEnvelope($query, [newHandledStamp('result','DummyHandler::__invoke')])
44+
);
45+
46+
$this->assertSame('result',$queryBus->query($query));
47+
}
48+
49+
publicfunctiontestHandleAcceptsEnvelopes()
50+
{
51+
$bus =$this->createMock(MessageBus::class);
52+
$queryBus =newTestQueryBus($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',$queryBus->query($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\TestQueryBus::handle()".');
64+
$bus =$this->createMock(MessageBus::class);
65+
$queryBus =newTestQueryBus($bus);
66+
67+
$query =newDummyMessage('Hello');
68+
$bus->expects($this->once())->method('dispatch')->willReturn(newEnvelope($query));
69+
70+
$queryBus->query($query);
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\TestQueryBus::handle()", got 2: "FirstDummyHandler::__invoke", "SecondDummyHandler::__invoke".');
77+
$bus =$this->createMock(MessageBus::class);
78+
$queryBus =newTestQueryBus($bus);
79+
80+
$query =newDummyMessage('Hello');
81+
$bus->expects($this->once())->method('dispatch')->willThrowException(
82+
newHandlerFailedException(
83+
newEnvelope($query, [newHandledStamp('first_result','FirstDummyHandler::__invoke')]),
84+
['SecondDummyHandler::__invoke' =>new \RuntimeException('SecondDummyHandler failed.')]
85+
)
86+
);
87+
88+
$queryBus->query($query);
89+
}
90+
91+
publicfunctiontestHandleThrowsWrappedException()
92+
{
93+
$bus =$this->createMock(MessageBus::class);
94+
$queryBus =newTestQueryBus($bus);
95+
96+
$query =newDummyMessage('Hello');
97+
$wrappedException =new \RuntimeException('Handler failed.');
98+
$bus->expects($this->once())->method('dispatch')->willThrowException(
99+
newHandlerFailedException(
100+
newEnvelope($query),
101+
['DummyHandler::__invoke' =>new \RuntimeException('Handler failed.')]
102+
)
103+
);
104+
105+
$this->expectException($wrappedException::class);
106+
$this->expectExceptionMessage($wrappedException->getMessage());
107+
108+
$queryBus->query($query);
109+
}
110+
}
111+
112+
class TestQueryBus
113+
{
114+
use SingleHandlingTrait;
115+
116+
publicfunction__construct(?MessageBusInterface$messageBus)
117+
{
118+
if ($messageBus) {
119+
$this->messageBus =$messageBus;
120+
}
121+
}
122+
123+
publicfunctionquery($query):string
124+
{
125+
return$this->handle($query);
126+
}
127+
}

‎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