@@ -6,23 +6,19 @@ Getting Results from your Handler
6
6
7
7
When a message is handled, the :class: `Symfony\\ Component\\ Messenger\\ Middleware\\ HandleMessageMiddleware `
8
8
adds a :class: `Symfony\\ Component\\ Messenger\\ Stamp\\ HandledStamp ` for each object that handled the message.
9
- You can use this to get the value returned by the handler(s):
9
+ You can use this to get the value returned by the handler(s)::
10
10
11
- .. configuration-block ::
11
+ use Symfony\Component\Messenger\MessageBusInterface;
12
+ use Symfony\Component\Messenger\Stamp\HandledStamp;
12
13
13
- .. code-block :: php
14
+ $envelope = $messageBus->dispatch(SomeMessage());
14
15
15
- use Symfony\Component\Messenger\MessageBusInterface;
16
- use Symfony\Component\Messenger\Stamp\HandledStamp;
16
+ // get the value that was returned by the last message handler
17
+ $handledStamp = $envelope->last(HandledStamp::class);
18
+ $handledStamp->getResult();
17
19
18
- $envelope = $messageBus->dispatch(SomeMessage());
19
-
20
- // get the value that was returned by the last message handler
21
- $handledStamp = $envelope->last(HandledStamp::class);
22
- $handledStamp->getResult();
23
-
24
- // or get info about all of handlers
25
- $handledStamps = $envelope->all(HandledStamp::class);
20
+ // or get info about all of handlers
21
+ $handledStamps = $envelope->all(HandledStamp::class);
26
22
27
23
A :class: `Symfony\\ Component\\ Messenger\\ HandleTrait ` also exists in order to ease
28
24
leveraging a Messenger bus for synchronous needs.
@@ -41,77 +37,69 @@ As queries are usually synchronous and expected to be handled once,
41
37
getting the result from the handler is a common need.
42
38
43
39
To make this easy, you can leverage the ``HandleTrait `` in any class that has
44
- a ``$messageBus `` property:
40
+ a ``$messageBus `` property::
41
+
42
+ // src/Action/ListItems.php
43
+ namespace App\Action;
45
44
46
- .. configuration-block ::
45
+ use App\Message\ListItemsQuery;
46
+ use App\MessageHandler\ListItemsQueryResult;
47
+ use Symfony\Component\Messenger\HandleTrait;
48
+ use Symfony\Component\Messenger\MessageBusInterface;
47
49
48
- .. code-block :: php
50
+ class ListItems
51
+ {
52
+ use HandleTrait;
53
+
54
+ public function __construct(MessageBusInterface $messageBus)
55
+ {
56
+ $this->messageBus = $messageBus;
57
+ }
49
58
50
- // src/Action/ListItems.php
51
- namespace App\Action;
59
+ public function __invoke()
60
+ {
61
+ $result = $this->query(new ListItemsQuery(/* ... */));
52
62
53
- use App\Message\ListItemsQuery;
54
- use App\MessageHandler\ListItemsQueryResult;
55
- use Symfony\Component\Messenger\HandleTrait;
56
- use Symfony\Component\Messenger\MessageBusInterface;
63
+ // Do something with the result
64
+ // ...
65
+ }
57
66
58
- class ListItems
67
+ // Creating such a method is optional, but allows type-hinting the result
68
+ private function query(ListItemsQuery $query): ListItemsResult
59
69
{
60
- use HandleTrait;
61
-
62
- public function __construct(MessageBusInterface $messageBus)
63
- {
64
- $this->messageBus = $messageBus;
65
- }
66
-
67
- public function __invoke()
68
- {
69
- $result = $this->query(new ListItemsQuery(/* ... */));
70
-
71
- // Do something with the result
72
- // ...
73
- }
74
-
75
- // Creating such a method is optional, but allows type-hinting the result
76
- private function query(ListItemsQuery $query): ListItemsResult
77
- {
78
- return $this->handle($query);
79
- }
70
+ return $this->handle($query);
80
71
}
72
+ }
81
73
82
74
Hence, you can use the trait to create command & query bus classes.
83
75
For example, you could create a special ``QueryBus `` class and inject it
84
- wherever you need a query bus behavior instead of the ``MessageBusInterface ``:
76
+ wherever you need a query bus behavior instead of the ``MessageBusInterface ``::
85
77
86
- .. configuration-block ::
78
+ // src/MessageBus/QueryBus.php
79
+ namespace App\MessageBus;
87
80
88
- .. code-block :: php
81
+ use Symfony\Component\Messenger\Envelope;
82
+ use Symfony\Component\Messenger\HandleTrait;
83
+ use Symfony\Component\Messenger\MessageBusInterface;
89
84
90
- // src/MessageBus/QueryBus.php
91
- namespace App\MessageBus;
85
+ class QueryBus
86
+ {
87
+ use HandleTrait;
92
88
93
- use Symfony\Component\Messenger\Envelope;
94
- use Symfony\Component\Messenger\HandleTrait;
95
- use Symfony\Component\Messenger\MessageBusInterface;
89
+ public function __construct(MessageBusInterface $messageBus)
90
+ {
91
+ $this->messageBus = $messageBus;
92
+ }
96
93
97
- class QueryBus
94
+ /**
95
+ * @param object|Envelope $query
96
+ *
97
+ * @return mixed The handler returned value
98
+ */
99
+ public function query($query)
98
100
{
99
- use HandleTrait;
100
-
101
- public function __construct(MessageBusInterface $messageBus)
102
- {
103
- $this->messageBus = $messageBus;
104
- }
105
-
106
- /**
107
- * @param object|Envelope $query
108
- *
109
- * @return mixed The handler returned value
110
- */
111
- public function query($query)
112
- {
113
- return $this->handle($query);
114
- }
101
+ return $this->handle($query);
115
102
}
103
+ }
116
104
117
105
.. _`article about CQRS` : https://martinfowler.com/bliki/CQRS.html
0 commit comments