Skip to content

Commit 2963f3e

Browse files
committed
Merge branch '5.4' into 6.3
* 5.4: [EventDispatcher] docs: document simpler event dispatching
2 parents 5a95212 + f1142c0 commit 2963f3e

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

components/event_dispatcher.rst

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ An :class:`Symfony\\Contracts\\EventDispatcher\\Event` instance is also
6969
created and passed to all of the listeners. As you'll see later, the ``Event``
7070
object itself often contains data about the event being dispatched.
7171

72-
Naming Conventions
73-
..................
74-
75-
The unique event name can be any string, but optionally follows a few
76-
naming conventions:
77-
78-
* Use only lowercase letters, numbers, dots (``.``) and underscores (``_``);
79-
* Prefix names with a namespace followed by a dot (e.g. ``order.*``, ``user.*``);
80-
* End names with a verb that indicates what action has been taken (e.g.
81-
``order.placed``).
82-
8372
Event Names and Event Objects
8473
.............................
8574

@@ -257,7 +246,7 @@ system flexible and decoupled.
257246
Creating an Event Class
258247
.......................
259248

260-
Suppose you want to create a new event - ``order.placed`` - that is dispatched
249+
Suppose you want to create a new event that is dispatched
261250
each time a customer orders a product with your application. When dispatching
262251
this event, you'll pass a custom event instance that has access to the placed
263252
order. Start by creating this custom event class and documenting it::
@@ -268,17 +257,12 @@ order. Start by creating this custom event class and documenting it::
268257
use Symfony\Contracts\EventDispatcher\Event;
269258

270259
/**
271-
* The order.placed event is dispatched each time an order is created
272-
* in the system.
260+
* This event is dispatched each time an order
261+
* is placed in the system.
273262
*/
274-
class OrderPlacedEvent extends Event
263+
final class OrderPlacedEvent extends Event
275264
{
276-
public const NAME = 'order.placed';
277-
278-
public function __construct(
279-
protected Order $order,
280-
) {
281-
}
265+
public function __construct(private Order $order) {}
282266

283267
public function getOrder(): Order
284268
{
@@ -314,10 +298,10 @@ of the event to dispatch::
314298

315299
// creates the OrderPlacedEvent and dispatches it
316300
$event = new OrderPlacedEvent($order);
317-
$dispatcher->dispatch($event, OrderPlacedEvent::NAME);
301+
$dispatcher->dispatch($event);
318302

319303
Notice that the special ``OrderPlacedEvent`` object is created and passed to
320-
the ``dispatch()`` method. Now, any listener to the ``order.placed``
304+
the ``dispatch()`` method. Now, any listener to the ``OrderPlacedEvent::class``
321305
event will receive the ``OrderPlacedEvent``.
322306

323307
.. _event_dispatcher-using-event-subscribers:
@@ -336,7 +320,7 @@ events it should subscribe to. It implements the
336320
interface, which requires a single static method called
337321
:method:`Symfony\\Component\\EventDispatcher\\EventSubscriberInterface::getSubscribedEvents`.
338322
Take the following example of a subscriber that subscribes to the
339-
``kernel.response`` and ``order.placed`` events::
323+
``kernel.response`` and ``OrderPlacedEvent::class`` events::
340324

341325
namespace Acme\Store\Event;
342326

@@ -354,7 +338,7 @@ Take the following example of a subscriber that subscribes to the
354338
['onKernelResponsePre', 10],
355339
['onKernelResponsePost', -10],
356340
],
357-
OrderPlacedEvent::NAME => 'onStoreOrder',
341+
OrderPlacedEvent::class => 'onPlacedOrder',
358342
];
359343
}
360344

@@ -368,8 +352,9 @@ Take the following example of a subscriber that subscribes to the
368352
// ...
369353
}
370354

371-
public function onStoreOrder(OrderPlacedEvent $event): void
355+
public function onPlacedOrder(OrderPlacedEvent $event): void
372356
{
357+
$order = $event->getOrder();
373358
// ...
374359
}
375360
}
@@ -413,14 +398,14 @@ inside a listener via the
413398

414399
use Acme\Store\Event\OrderPlacedEvent;
415400

416-
public function onStoreOrder(OrderPlacedEvent $event): void
401+
public function onPlacedOrder(OrderPlacedEvent $event): void
417402
{
418403
// ...
419404

420405
$event->stopPropagation();
421406
}
422407

423-
Now, any listeners to ``order.placed`` that have not yet been called will
408+
Now, any listeners to ``OrderPlacedEvent::class`` that have not yet been called will
424409
*not* be called.
425410

426411
It is possible to detect if an event was stopped by using the

0 commit comments

Comments
 (0)