Skip to content

Commit 3baef87

Browse files
committed
minor #10899 drop not needed configuration block (xabbuh)
This PR was merged into the 4.2 branch. Discussion ---------- drop not needed configuration block <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- d758375 drop not needed configuration block
2 parents 60e9d91 + d758375 commit 3baef87

File tree

1 file changed

+56
-68
lines changed

1 file changed

+56
-68
lines changed

messenger/handler_results.rst

Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ Getting Results from your Handler
66

77
When a message is handled, the :class:`Symfony\\Component\\Messenger\\Middleware\\HandleMessageMiddleware`
88
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)::
1010

11-
.. configuration-block::
11+
use Symfony\Component\Messenger\MessageBusInterface;
12+
use Symfony\Component\Messenger\Stamp\HandledStamp;
1213

13-
.. code-block:: php
14+
$envelope = $messageBus->dispatch(SomeMessage());
1415

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();
1719

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);
2622

2723
A :class:`Symfony\\Component\\Messenger\\HandleTrait` also exists in order to ease
2824
leveraging a Messenger bus for synchronous needs.
@@ -41,77 +37,69 @@ As queries are usually synchronous and expected to be handled once,
4137
getting the result from the handler is a common need.
4238

4339
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;
4544

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;
4749

48-
.. code-block:: php
50+
class ListItems
51+
{
52+
use HandleTrait;
53+
54+
public function __construct(MessageBusInterface $messageBus)
55+
{
56+
$this->messageBus = $messageBus;
57+
}
4958

50-
// src/Action/ListItems.php
51-
namespace App\Action;
59+
public function __invoke()
60+
{
61+
$result = $this->query(new ListItemsQuery(/* ... */));
5262

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+
}
5766

58-
class ListItems
67+
// Creating such a method is optional, but allows type-hinting the result
68+
private function query(ListItemsQuery $query): ListItemsResult
5969
{
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);
8071
}
72+
}
8173

8274
Hence, you can use the trait to create command & query bus classes.
8375
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``::
8577

86-
.. configuration-block::
78+
// src/MessageBus/QueryBus.php
79+
namespace App\MessageBus;
8780

88-
.. code-block:: php
81+
use Symfony\Component\Messenger\Envelope;
82+
use Symfony\Component\Messenger\HandleTrait;
83+
use Symfony\Component\Messenger\MessageBusInterface;
8984

90-
// src/MessageBus/QueryBus.php
91-
namespace App\MessageBus;
85+
class QueryBus
86+
{
87+
use HandleTrait;
9288

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+
}
9693

97-
class QueryBus
94+
/**
95+
* @param object|Envelope $query
96+
*
97+
* @return mixed The handler returned value
98+
*/
99+
public function query($query)
98100
{
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);
115102
}
103+
}
116104

117105
.. _`article about CQRS`: https://martinfowler.com/bliki/CQRS.html

0 commit comments

Comments
 (0)