Event management

The Swoft 2 event was more clearly and rigorously planned. Basic event registration and trigger management are provided.

Function Description

  • Implement the Psr 14 - Event dispatcher
  • Support for fast registration events via annotations, event groups
  • Support for setting event priority
  • Support for monitoring wildcard events

As a core component of Swoft, event management is automatically enabled

installation

 composer require swoft/event 

Events in swoft

The Swoft 2 event was more clearly and rigorously planned. Provides a wealth of events for developers to use.

In swowt we divide the events into three main categories:

  • Swoole server callback event
  • Swoft server events, based on swoole callback processing, extend some of the available events to enhance customization
  • Custom event management and use within the application level

Related introduction

Some extended descriptions about custom events

Event grouping

Except for some special events, in an application, most of the events are related, so we can group the events for easy identification and management.

  • Event grouping It is recommended to group related events in the name design.

E.g:

 swoft.server.*
swoft.process.*
swoft.pool.*

swoft.http.request.before
swoft.http.request.after

swoft.db.query.start
swoft.db.query.after

swoft.redis.start
swoft.redis.after

swoft.ws.start
swoft.ws.after

swoft.tcp.start
swoft.tcp.after

swoft.udp.start
swoft.udp.after 

Event wildcard *

Support for using the event wildcard * to listen to a group of related events in two ways.

  1. * Global event wildcards. Add a listener ( $em->attach('*', 'global_listener') ) directly to * , and all triggered events will be received by this listener.
  2. {prefix}.* specifies the listening of group events. Eg $em->attach('db.*', 'db_listener') , at this point all triggered events with db. prefix (eg db.query db.connect ) will be received by this listener.

Of course, if you stop the event's propagation $event->stopPropagation(true); before the event reaches the listener, it will not be received by the subsequent listeners.

More introduction

For more information on custom events, see https://github.com/inhere/php-event-manager/blob/master/README.md

Git warehouse

Participate in the contribution

Welcome to participate in the contribution, you can

  • Fork our development warehouse swewt/component
  • Modify the code and then initiate the PR
  • About PR launch of Notes
/docs/2.x/en/event/index.html
progress-bar