PHPackages                             naingaunglwin-dev/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. naingaunglwin-dev/event-dispatcher

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

naingaunglwin-dev/event-dispatcher
==================================

A lightweight and flexible PHP Event Dispatcher package fully compliant with PSR-14

v1.0.0(3mo ago)01MITPHPPHP &gt;=8.4CI passing

Since Jan 26Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/naingaunglwin-dev/event-dispatcher)[ Packagist](https://packagist.org/packages/naingaunglwin-dev/event-dispatcher)[ RSS](/packages/naingaunglwin-dev-event-dispatcher/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Event-Dispatcher
================

[](#event-dispatcher)

[![GitHub CI Status](https://github.com/naingaunglwin-dev/event-dispatcher/actions/workflows/test.yml/badge.svg)](https://github.com/naingaunglwin-dev/event-dispatcher/actions)[![Code Coverage](https://camo.githubusercontent.com/0c967d746eb7f4fe28065a3b94d23faa207cd6f4a968535b20fce0310a87cf89/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d677265656e)](https://github.com/naingaunglwin-dev/dotenv/)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://github.com/naingaunglwin-dev/dotenv/blob/main/LICENSE)

A lightweight PHP Event Dispatcher that follows PSR-14, with optional helpers and facade for ergonomic usage.

Contributing
------------

[](#contributing)

- This is an open-source library, and contributions are welcome.
- If you have any suggestions, bug reports, or feature requests, please open an issue or submit a pull request on the project repository.

Requirement
-----------

[](#requirement)

- **PHP** version 8.4 or newer is required
- composer

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

[](#installation)

```
composer require naingaunglwin-dev/event-dispatcher
```

Features
--------

[](#features)

- PSR-14 compliant core dispatcher
- Stoppable events (`stopPropagation()`)
- One-time listeners (`once()`)
- Payload support with `GenericEvent`
- User-friendly `Support\Event` helper
- Static `Facade\Event` API for Laravel-style usage
- Lightweight and easy to extend

Core Concepts
=============

[](#core-concepts)

This package provides a PSR-14 compliant core and optional helpers.

- Core (PSR-14)
    - `EventDispatcher`
    - `ListenerProvider`
    - `StoppableEvent`
- Extras (Framework-friendly)
    - `GenericEvent` (string-based events with payload)
    - `Support\Event` helper
    - `Facade\Event` (Laravel-style static API)

You may use **only the core**, or opt into the helpers.

Usage
-----

[](#usage)

### 1️⃣ GenericEvent (String-based Events)

[](#1️⃣-genericevent-string-based-events)

Use `GenericEvent` when you want string-based events with a mutable payload.

- Example

```
use Naingaunglwin\EventDispatcher\EventDispatcher;
use Naingaunglwin\EventDispatcher\ListenerProvider;
use Naingaunglwin\EventDispatcher\GenericEvent;

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

$provider->addListener('init', function (GenericEvent $event) {
    $event->setPayload('foo', 'bar');
});

$provider->addListener('init', function (GenericEvent $event) {
    echo $event->getPayload('foo') . PHP_EOL;
});

$dispatcher->dispatch(new GenericEvent('init'));
```

### Description

[](#description)

- GenericEvent allows non-class-based events
- Payload is mutable and shared across listeners
- Fully compatible with stoppable propagation

---

### 2️⃣ Class-based Events (Recommended)

[](#2️⃣-class-based-events-recommended)

Class-based events are the preferred PSR-14 approach.

- Define an Event

```
use Naingaunglwin\EventDispatcher\StoppableEvent;

class UserRegistered extends StoppableEvent
{
    public function __construct(
        public readonly string $username
    ) {}
}
```

- Register Listeners

```
$provider->addListener(UserRegistered::class, function (UserRegistered $event) {
    echo "User registered: {$event->username}" . PHP_EOL;
});
```

- Dispatch the Event

```
$dispatcher->dispatch(new UserRegistered('john'));
```

---

### 3️⃣ Class-based Listeners (`__invoke`)

[](#3️⃣-class-based-listeners-__invoke)

Listeners may be invokable classes

- Listener Class

```
class SendWelcomeEmail
{
    public function __invoke(UserRegistered $event): void
    {
        echo "Sending welcome email to {$event->username}" . PHP_EOL;
    }
}
```

Register the Listener

```
$provider->addListener(UserRegistered::class, new SendWelcomeEmail());
```

---

### 4️⃣ Stoppable Events

[](#4️⃣-stoppable-events)

Listeners can stop further propagation.

- Example

```
$provider->addListener(UserRegistered::class, function (UserRegistered $event) {
    if ($event->username === 'admin') {
        $event->stopPropagation();
    }
});

$provider->addListener(UserRegistered::class, function () {
    echo "This will not run if propagation is stopped";
});
```

---

### 5️⃣ One-time Listeners (once)

[](#5️⃣-one-time-listeners-once)

One-time listeners are removed automatically after execution.

```
$provider->addOnceListener('init', function () {
    echo "This runs only once" . PHP_EOL;
});
```

---

### 6️⃣ Support\\Event Helper (User-friendly API)

[](#6️⃣-supportevent-helper-user-friendly-api)

The Support\\Event helper wraps the core dispatcher for convenience.

- Example

```
use Naingaunglwin\EventDispatcher\Support\Event;
use Naingaunglwin\EventDispatcher\GenericEvent;

$event = new Event();

$event->on('init', function (GenericEvent $event) {
    $event->setPayload('foo', 'bar');
});

$event->once('init', function () {
    echo "Run once" . PHP_EOL;
});

$event->on('init', function (GenericEvent $event) {
    echo $event->getPayload('foo') . PHP_EOL;
});

$event->dispatch(new GenericEvent('init'));
$event->dispatch(new GenericEvent('init'));
```

### Description

[](#description-1)

- Simplifies listener registration
- Manages provider and dispatcher internally
- Still uses PSR-14 compliant core internally

---

### 7️⃣ Facade\\Event (Static API)

[](#7️⃣-facadeevent-static-api)

For maximum ergonomics, use the facade.

Example

```
use Naingaunglwin\EventDispatcher\Support\Facade\Event;
use Naingaunglwin\EventDispatcher\GenericEvent;

Event::on('init', function (GenericEvent $event) {
    $event->setPayload('foo', 'bar');
});

Event::once('init', function () {
    echo "Just once" . PHP_EOL;
});

Event::on('init', function (GenericEvent $event) {
    echo $event->getPayload('foo') . PHP_EOL;
});

Event::dispatch('init');
Event::dispatch('init');
```

**Resetting the Facade**

- Useful for tests or long-running processes:

```
Event::reset();
```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance80

Actively maintained with recent releases

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

103d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/812276ecc49f99db91f7d5f37c2c3ff1a382aeaf431738670d9a60b4104a01fb?d=identicon)[naingaunglwin-dev](/maintainers/naingaunglwin-dev)

---

Top Contributors

[![naingaunglwin-dev](https://avatars.githubusercontent.com/u/126607909?v=4)](https://github.com/naingaunglwin-dev "naingaunglwin-dev (13 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/naingaunglwin-dev-event-dispatcher/health.svg)

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

###  Alternatives

[symfony/event-dispatcher-contracts

Generic abstractions related to dispatching event

3.4k756.5M422](/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)
