Handling Events
Events PHP

Registering Class Based Event Handler

In order to handle events published within a system we can use #[EventHandler] with combination of specific class.
1
class Notifier
2
{
3
#[EventHandler]
4
public function notify(AppointmentWasCreatedEvent $event) : void
5
{
6
// do something with $event
7
}
8
}
Copied!
In order to listen for event we need to mark method as EventHandler. This work exactly the same as External Command Handlers.
Whenever AppointmentWasCreatedEvent will be published our notify method will be called,

Event Handling Interfaces and Abstract Classes

1
#[EventHandler]
2
public function notify(AppointmentRelatedEvent $event) : void
3
{
4
// do something with $event
5
}
Copied!
We also can tell Ecotone that we want to handle all events implementing specific Interface or extending specific abstract class. In that case all we need to do it is to type hint for this interface or abstract class.

Event Handling Union Classes

1
#[EventHandler]
2
public function notify(AppointmentWasScheduled|AppointmentWasCancelled $event) : void
3
{
4
// do something with $event
5
}
Copied!
Ecotone provides possibility to listen for multiple events with same method, based on Union Types.

Handling all published events

1
#[EventHandler]
2
public function storeLog(object $event) : void
3
{
4
// do something with $event
5
}
Copied!
If we will type hint for object then this Event Handler will be called for any event published in the system.

Registering Name Based Events

There may be a situations when events will arrive from different systems. We may not have access to specific class or we may not want to share events a cross applications. In that case we can listen for events with specific name.
1
class EmailService
2
{
3
#[EventHandler("billing.customer_was_invoiced")]
4
public function notifyAboutGeneratedInvoiced(array $event) : void
5
{
6
// do something with $event
7
}
8
}
Copied!
This Event Handler listen for billing.customer_was_invoiced.
How to publish named events, you may see in Dispatching Events section
1
class EmailService
2
{
3
#[EventHandler("billing.customer_was_invoiced")]
4
public function notifyAboutGeneratedInvoiced(CustomerWasInvoiced $event) : void
5
{
6
// do something with $event
7
}
8
}
Copied!
In above case EventHandler will be registered only for event routed by billing.customer_was_invoiced. If you want to register it for class based routing also, then you should do it like below:
1
class EmailService
2
{
3
#[EventHandler("billing.customer_was_invoiced")]
4
#[EventHandler()]
5
public function notifyAboutGeneratedInvoiced(CustomerWasInvoiced $event) : void
6
{
7
// do something with $event
8
}
9
}
Copied!

Handling group of named events

In order to handle more than one named event, we can make use of regex like star *
1
#[EventHandler("shop.order.*")
2
public function notifyAboutGeneratedInvoiced(array $event) : void
3
{
4
// do something with $event
5
}
Copied!
This Event Handler will listen for all events beginning with shop.order prefix. So shop.order.was_started shop.order.was_finished are going to be handled by this Endpoint.
You may put the * expression anywhere you like
  • shop.*.processed
  • *
If we put only the star, then it will handle all named events.
Last modified 6mo ago