Skip to content

Commit

Permalink
Documentation (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes authored Sep 30, 2023
1 parent 8a153b1 commit 48f7021
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/components/event-dispatcher/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

use SonsOfPHP\Component\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();

// If you have a custom ListenerProviderInterface you can inject it into the
// EventDispatcher
//$dispatcher = new EventDispatcher($provider);

$dispatcher->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`.

0 comments on commit 48f7021

Please sign in to comment.