PHPackages                             alex-patterson-webdev/event-dispatcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. alex-patterson-webdev/event-dispatcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

alex-patterson-webdev/event-dispatcher
======================================

Simple implementation of the PSR-14 event dispatcher

3.0.0(3y ago)1523[1 PRs](https://github.com/alex-patterson-webdev/event-dispatcher/pulls)2MITPHPPHP &gt;=8.1

Since Apr 8Pushed 1y ago1 watchersCompare

[ Source](https://github.com/alex-patterson-webdev/event-dispatcher)[ Packagist](https://packagist.org/packages/alex-patterson-webdev/event-dispatcher)[ RSS](/packages/alex-patterson-webdev-event-dispatcher/feed)WikiDiscussions master Synced yesterday

READMEChangelog (4)Dependencies (8)Versions (7)Used By (2)

[![github workflow](https://github.com/alex-patterson-webdev/event-dispatcher/actions/workflows/workflow.yml/badge.svg)](https://github.com/alex-patterson-webdev/event-dispatcher/actions/workflows/workflow.yml/badge.svg)[![codecov](https://camo.githubusercontent.com/cd9c26b8ae69a21ddefa62887823e3b2d9c56e8f0308f39f054baacc5d7ac7f9/68747470733a2f2f636f6465636f762e696f2f67682f616c65782d706174746572736f6e2d7765626465762f6576656e742d646973706174636865722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/alex-patterson-webdev/event-dispatcher)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5299ac4261228e566db520e0de53b8c3f7dae228a065f0970735772e878a3b68/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c65782d706174746572736f6e2d7765626465762f6576656e742d646973706174636865722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alex-patterson-webdev/event-dispatcher/?branch=master)

Arp\\EventDispatcher
====================

[](#arpeventdispatcher)

About
-----

[](#about)

An implementation of the [PSR-14 Event Dispatcher](https://www.php-fig.org/psr/psr-14/).

Installation
------------

[](#installation)

Installation via [composer](https://getcomposer.org).

```
require alex-patterson-webdev/event-dispatcher ^3

```

Usage
-----

[](#usage)

### Dispatching events

[](#dispatching-events)

The `Arp\EventDispatcher\EventDispatcher` class is responsible for executing the event listeners for any given event instance. The event dispatcher requires a `Arp\EventDispatcher\Listener\ListenerProvider` instance as a single dependency. The `ListenerProvider` contains all the event listeners that can be dispatched.

```
use Arp\EventDispatcher\EventDispatcher;
use Arp\EventDispatcher\Listener\ListenerProvider;

$listenerProvider = new ListenerProvider();
$dispatcher = new EventDispatcher($listenerProvider);

```

The call to `EventDispatcher::dispatch()` will loop through the registered event listeners from the provider. Any listeners that match `$event` will be executed using the configured priority order.

```
$dispatcher->dispatch(new \My\Event\Foo());

```

Any object can be used as an event; by default an internal `EventNameResolver` instance will return the fully qualified class name of the object to use as the event name. See the 'Event Name Resolver' section for more configuration options.

### Event Listener Registration

[](#event-listener-registration)

The `Arp\EventDispatcher\Listener\ListenerProvider` is an implementation of the `Psr\EventDispatcher\ListenerProviderInterface`. The class has been designed to store and 'provide' an `iterable` collection of event listeners for a given event object. The `ListenerProvider` also implements interface `Arp\EventDispatcher\Listener\AddListenerAwareInterface` which exposes public methods which allow for the registration of event listeners.

We can register any PHP `callable` data type directly using the `ListenerProvider::addListenerForEvent()` method.

For example :

```
use Arp\EventDispatcher\Listener\ListenerProvider;
use My\Event\Foo;

$listenerProvider = new ListenerProvider();

$event = new Foo();
$listener = static function (Foo $event) {
    echo 'The My\Event\Foo event was dispatched' . PHP_EOL;
};

$listenerProvider->addListenerForEvent($event, $listener);

```

We can also attach an array of listeners with the `addListenersForEvent()` method.

```
$listeners = [
    new class() {
        public function __invoke(Foo $event): void {
            echo 'Listener 1' . PHP_EOL;
        }
    },
    static function (Foo $event) {
        echo 'Listener 2' . PHP_EOL;
    },
];

$listenerProvider->addListenersForEvent($event, $listeners);

```

We are able to then retrieve these event listeners by calling `$listenerProvider->getListenersForEvent($event);`. The result is an instance of `Arp\EventDispatcher\Listener\ListenerCollectionInterface`, which contains the event listeners that were added.

```
/** @var ListenerCollectionInterface $listenerCollection */
$listenerCollection = $listenerProvider->getListenersForEvent($event);

```

### Adding Listeners via the Event Dispatcher

[](#adding-listeners-via-the-event-dispatcher)

For convenience, the `EventDispatcher` class also implements `Arp\EventDispatcher\Listener\AddListenerAwareInterface`. This provides public methods to add event listeners to collections of the listener provider *after* it has been passed to the `EventDispatcher`.

Internally calls to `addListenerForEvent()` and `addListenersForEvent()` will proxy to the internal listener provider.

```
use Arp\EventDispatcher\EventDispatcher;
use Arp\EventDispatcher\Listener\ListenerProvider;

$dispatcher = new EventDispatcher(new ListenerProvider());

$listener1 = static function (Foo $event) {
    echo 'Event Listener 1' . PHP_EOL;
};
$listener2 = static function (Foo $event) {
    echo 'Event Listener 2' . PHP_EOL;
};

$dispatcher->addListenerForEvent($event, $listener1);
$dispatcher->addListenerForEvent($event, $listener2);

$dispatcher->dispatch(new My\Event\Foo());

```

### Immutable Event Dispatcher

[](#immutable-event-dispatcher)

If you do not want the collection of event listeners to be modified after being passed to the `EventDispatcher` you can use the provided `Arp\EventDispatcher\ImmutableEventDispatcher`. This event dispatcher implementation does not expose any methods that can change the initial listener provider.

Listener Priority
-----------------

[](#listener-priority)

Internally the `ListenerProvider` will keep an iterable priority queue of all the listeners for each event you provide.

The `$priority` argument allows the ordering of each listener. Higher priority listeners will execute before lower priority listeners. If two or more listeners share the same priority, they will respect the order in which they were added.

```
use My\Event\Foo;
use Arp\EventDispatcher\Listener\ListenerProvider;

$event = new Foo();

$listener1 = static function (Foo $event) {
    echo 'Listener 1' . PHP_EOL;
};
$listener2 = static function (Foo $event) {
    echo 'Listener 2' . PHP_EOL;
};

$listenerProvider->addListenerForEvent($event, $listener1, -100);
$listenerProvider->addListenerForEvent($event, $listener2, 100);

$eventDispatcher->dispatch($event);

// Listener 2
// Listener 1

```

The Event Name Resolver
-----------------------

[](#the-event-name-resolver)

By default, the `EventNameResolver` will use the fully qualified class name of the event object as the name of the event that will be dispatched. There may however be times when you would like to provide the name of the event as a property of the event object. This can be achieved by implementing `Arp\EventDispacther\Resolver\EventNameAwareInterface` on your event class or using the default `Arp\EventDispacther\NamedEvent`.

When an object that implements `EventNameAwareInterface` is passed to the `EventNameResolver` the provider will return the value of `getEventName()`.

```
use Arp\EventDispatcher\Event\NamedEvent;

// Dispatch the 'foo' event
$eventDispatcher->dispatch(new NamedEvent('foo'));

```

Event Propagation
-----------------

[](#event-propagation)

In accordance with the PSR, if provided with a `Psr\EventDispatcher\StoppableEventInterface` event instance the dispatcher will respect the result of the call `isPropagationStopped()`. If set to `true` the event dispatcher will be prevented from executing any further listeners.

Unit Tests
----------

[](#unit-tests)

PHP Unit test using PHPUnit.

```
php vendor/bin/phpunit

```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~281 days

Total

5

Last Release

1098d ago

Major Versions

0.1.1 → 1.0.02020-07-18

1.0.0 → 2.0.02022-03-18

2.0.0 → 3.0.02023-05-08

PHP version history (4 changes)0.1.0PHP &gt;=7.2

1.0.0PHP &gt;=7.3

2.0.0PHP &gt;=7.4 || &gt;=8.0

3.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/8ffd0597cab058e2b719b482a502868f91eb143581b159835d7a97c5da302f8f?d=identicon)[alex-patterson-webdev](/maintainers/alex-patterson-webdev)

---

Top Contributors

[![alex-patterson-webdev](https://avatars.githubusercontent.com/u/1563851?v=4)](https://github.com/alex-patterson-webdev "alex-patterson-webdev (125 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/alex-patterson-webdev-event-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/alex-patterson-webdev-event-dispatcher/health.svg)](https://phpackages.com/packages/alex-patterson-webdev-event-dispatcher)
```

###  Alternatives

[symfony/event-dispatcher-contracts

Generic abstractions related to dispatching event

3.4k756.5M423](/packages/symfony-event-dispatcher-contracts)[league/event

Event package

1.6k141.6M184](/packages/league-event)[phpro/soap-client

A general purpose SoapClient library

8885.6M46](/packages/phpro-soap-client)[yohang/finite

A simple PHP Finite State Machine

1.3k3.5M10](/packages/yohang-finite)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.4k423.9k30](/packages/mcp-sdk)[cognesy/instructor-php

The complete AI toolkit for PHP: unified LLM API, structured outputs, agents, and coding agent control

310107.9k1](/packages/cognesy-instructor-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
