Skip to content

Commit b7be4a5

Browse files
committed
Merge branch '4.2'
* 4.2: Update micro_kernel_trait.rst Recommend registering the error handler in prod validator: comparison constraints value Minor fixes in the Doctrine listeners article [Messenger] Added a trait for synchronous query & command buses
2 parents 4b549cc + 60e9d91 commit b7be4a5

File tree

12 files changed

+135
-15
lines changed

12 files changed

+135
-15
lines changed

components/debug.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ tools.
3737

3838
.. caution::
3939

40-
You should never enable the debug tools in a production environment as
41-
they might disclose sensitive information to the user.
40+
You should never enable the debug tools, except for the error handler, in a
41+
production environment as they might disclose sensitive information to the user.
4242

4343
Enabling the Error Handler
4444
--------------------------
@@ -52,6 +52,9 @@ fatal errors)::
5252

5353
ErrorHandler::register();
5454

55+
This error handler is enabled by default in the production environment when the
56+
application uses the FrameworkBundle because it generates better error logs.
57+
5558
Enabling the Exception Handler
5659
------------------------------
5760

configuration/micro_kernel_trait.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Next, create an ``index.php`` file that defines the kernel class and executes it
5555
{
5656
// kernel is a service that points to this class
5757
// optional 3rd argument is the route name
58-
$routes->add('/random/{limit}', 'Kernel::randomNumber');
58+
$routes->add('/random/{limit}', 'kernel::randomNumber');
5959
}
6060

6161
public function randomNumber($limit)

doctrine/event_listeners_subscribers.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ entity), you should check for the entity's class type in your method
129129

130130
In Doctrine 2.4, a feature called Entity Listeners was introduced.
131131
It is a lifecycle listener class used for an entity. You can read
132-
about it in `the Doctrine Documentation`_.
132+
about it in `the DoctrineBundle documentation`_.
133133

134134
Creating the Subscriber Class
135135
-----------------------------
@@ -199,9 +199,6 @@ fired.
199199
That's why it is preferable to use entity listeners instead of subscribers
200200
whenever possible.
201201

202-
.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
203-
.. _`the Doctrine Documentation`: https://symfony.com/doc/current/bundles/DoctrineBundle/entity-listeners.html
204-
205202
Priorities for Event Listeners
206203
------------------------------
207204

@@ -256,3 +253,6 @@ numbers mean that listeners are invoked earlier.
256253
->autowire(MyLowPriorityListener::class)
257254
->addTag('doctrine.event_listener', ['event' => 'postPersist', 'priority' => 1])
258255
;
256+
257+
.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
258+
.. _`the DoctrineBundle documentation`: https://symfony.com/doc/current/bundles/DoctrineBundle/entity-listeners.html

messenger/handler_results.rst

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
.. index::
2+
single: Messenger; Getting results / Working with command & query buses
3+
4+
Getting Results from your Handler
5+
---------------------------------
6+
7+
When a message is handled, the :class:`Symfony\\Component\\Messenger\\Middleware\\HandleMessageMiddleware`
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):
10+
11+
.. configuration-block::
12+
13+
.. code-block:: php
14+
15+
use Symfony\Component\Messenger\MessageBusInterface;
16+
use Symfony\Component\Messenger\Stamp\HandledStamp;
17+
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);
26+
27+
A :class:`Symfony\\Component\\Messenger\\HandleTrait` also exists in order to ease
28+
leveraging a Messenger bus for synchronous needs.
29+
The :method:`Symfony\\Component\\Messenger\\HandleTrait::handle` method ensures
30+
there is exactly one handler registered and returns its result.
31+
32+
Working with Command & Query Buses
33+
----------------------------------
34+
35+
The Messenger component can be used in CQRS architectures where command & query
36+
buses are central pieces of the application. Read Martin Fowler's
37+
`article about CQRS`_ to learn more and
38+
:doc:`how to configure multiple buses </messenger/multiple_buses>`.
39+
40+
As queries are usually synchronous and expected to be handled once,
41+
getting the result from the handler is a common need.
42+
43+
To make this easy, you can leverage the ``HandleTrait`` in any class that has
44+
a ``$messageBus`` property:
45+
46+
.. configuration-block::
47+
48+
.. code-block:: php
49+
50+
// src/Action/ListItems.php
51+
namespace App\Action;
52+
53+
use App\Message\ListItemsQuery;
54+
use App\MessageHandler\ListItemsQueryResult;
55+
use Symfony\Component\Messenger\HandleTrait;
56+
use Symfony\Component\Messenger\MessageBusInterface;
57+
58+
class ListItems
59+
{
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+
}
80+
}
81+
82+
Hence, you can use the trait to create command & query bus classes.
83+
For example, you could create a special ``QueryBus`` class and inject it
84+
wherever you need a query bus behavior instead of the ``MessageBusInterface``:
85+
86+
.. configuration-block::
87+
88+
.. code-block:: php
89+
90+
// src/MessageBus/QueryBus.php
91+
namespace App\MessageBus;
92+
93+
use Symfony\Component\Messenger\Envelope;
94+
use Symfony\Component\Messenger\HandleTrait;
95+
use Symfony\Component\Messenger\MessageBusInterface;
96+
97+
class QueryBus
98+
{
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+
}
115+
}
116+
117+
.. _`article about CQRS`: https://martinfowler.com/bliki/CQRS.html

reference/constraints/EqualTo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ and that the ``age`` is ``20``, you could do the following:
7575
<class name="App\Entity\Person">
7676
<property name="firstName">
7777
<constraint name="EqualTo">
78-
<value>Mary</value>
78+
Mary
7979
</constraint>
8080
</property>
8181
<property name="age">

reference/constraints/GreaterThan.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The following constraints ensure that:
7474
<class name="App\Entity\Person">
7575
<property name="siblings">
7676
<constraint name="GreaterThan">
77-
<value>5</value>
77+
5
7878
</constraint>
7979
</property>
8080
<property name="age">

reference/constraints/GreaterThanOrEqual.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The following constraints ensure that:
7272
<class name="App\Entity\Person">
7373
<property name="siblings">
7474
<constraint name="GreaterThanOrEqual">
75-
<value>5</value>
75+
5
7676
</constraint>
7777
</property>
7878
<property name="age">

reference/constraints/IdenticalTo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The following constraints ensure that:
7878
<class name="App\Entity\Person">
7979
<property name="firstName">
8080
<constraint name="IdenticalTo">
81-
<value>Mary</value>
81+
Mary
8282
</constraint>
8383
</property>
8484
<property name="age">

reference/constraints/LessThan.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ The following constraints ensure that:
7474
<class name="App\Entity\Person">
7575
<property name="siblings">
7676
<constraint name="LessThan">
77-
<value>5</value>
77+
5
7878
</constraint>
7979
</property>
8080
<property name="age">

reference/constraints/LessThanOrEqual.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ The following constraints ensure that:
7272
<class name="App\Entity\Person">
7373
<property name="siblings">
7474
<constraint name="LessThanOrEqual">
75-
<value>5</value>
75+
5
7676
</constraint>
7777
</property>
7878
<property name="age">

reference/constraints/NotEqualTo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ the following:
7777
<class name="App\Entity\Person">
7878
<property name="firstName">
7979
<constraint name="NotEqualTo">
80-
<value>Mary</value>
80+
Mary
8181
</constraint>
8282
</property>
8383
<property name="age">

reference/constraints/NotIdenticalTo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The following constraints ensure that:
7878
<class name="App\Entity\Person">
7979
<property name="firstName">
8080
<constraint name="NotIdenticalTo">
81-
<value>Mary</value>
81+
Mary
8282
</constraint>
8383
</property>
8484
<property name="age">

0 commit comments

Comments
 (0)