From 48f7021f9fcb41d9dd03f8ad463904af5124f739 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Sat, 30 Sep 2023 12:35:08 -0400 Subject: [PATCH] Documentation (#18) --- docs/components/event-dispatcher/index.md | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/docs/components/event-dispatcher/index.md b/docs/components/event-dispatcher/index.md index 57ab0d04..138b87e5 100644 --- a/docs/components/event-dispatcher/index.md +++ b/docs/components/event-dispatcher/index.md @@ -2,8 +2,56 @@ title: Event Dispatcher - Overview --- +# Event Dispatcher + +Event Dispatcher will allow you to attach listeners and notify those listeners +so they can handle the event that you dispatch. The Event Dispatcher is PSR-14 +Compatible with extra features. + +## Features + +* Event Names +* Event Subscribers +* Listener Priorities + + ## Installation ```shell composer require sonsofphp/event-dispatcher ``` + +## Usage + +```php +addListener($event::class, function ($event, $eventName, $dispatcher) {}); +$dispatcher->addListener('event.name', function ($event, $eventName, $dispatcher) {}); +$dispatcher->addSubscriber($subscriber); + +$dispatcher->dispatch($event); // PSR-14 +$dispatcher->dispatch($event, 'event.name'); // Custom Event Name +``` + +### Event Subscribers + +Must implement `EventSubscriberInterface`. + +### Listener Priorities + +```php +$dispatcher->addListener('event.name', function () {}, $priority); +``` + +The priority will default to `0`. Lower numbers are higher priority. Higher +numbers will be handled later. For example, a listener with a priority of `-1` +will be handled before a listener of priority `1`.