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

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

solophp/event-dispatcher
========================

Minimal, PSR-14 compatible event dispatcher with priorities and stoppable propagation.

v1.2.0(2mo ago)015MITPHPPHP ^8.3

Since Aug 10Pushed 2mo agoCompare

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

READMEChangelog (3)Dependencies (9)Versions (4)Used By (0)

Solo Event Dispatcher
=====================

[](#solo-event-dispatcher)

[![Latest Version on Packagist](https://camo.githubusercontent.com/511edc9066fe659c7776802496b674c2bacd87f893c26281c3249c59528b8b3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6c6f7068702f6576656e742d646973706174636865722e737667)](https://packagist.org/packages/solophp/event-dispatcher)[![License](https://camo.githubusercontent.com/4a321ea44c37e1e33038f86911feb4b8a6e07c6a4fcc65b13cdeb8afb3b97a25/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736f6c6f7068702f6576656e742d646973706174636865722e737667)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/c660ed11f5b2ff08f521191c2a47470667557258fccb8b76f021c5ae335fcad4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f736f6c6f7068702f6576656e742d646973706174636865722e737667)](https://packagist.org/packages/solophp/event-dispatcher)![Code Coverage](https://camo.githubusercontent.com/b3545ae1bcdb4ea486f71f87b43001e82dd21933bc8035d44601706c851265da/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d627269676874677265656e2e737667)

Minimal, PSR-14 compatible event dispatcher with priorities, stoppable propagation, and optional error logging.

Requirements
------------

[](#requirements)

- PHP 8.3+

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

[](#installation)

```
composer require solophp/event-dispatcher
```

Usage
-----

[](#usage)

### Adding Listeners Directly

[](#adding-listeners-directly)

```
use Solo\EventDispatcher\{EventDispatcher, ListenerProvider};

$provider = new ListenerProvider();

$provider->addListener(
    UserRegistered::class,
    fn (UserRegistered $e) => print "Welcome, {$e->username}!\n",
    priority: 10
);

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

### Using Subscribers

[](#using-subscribers)

```
use Solo\EventDispatcher\EventSubscriberInterface;

final class WelcomeEmailSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            UserRegistered::class => ['onUserRegistered', 10],
        ];
    }

    public function onUserRegistered(UserRegistered $event): void
    {
        echo "Welcome, {$event->username}!" . PHP_EOL;
    }
}

$provider = new ListenerProvider();
$provider->addSubscriber(new WelcomeEmailSubscriber());

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

### Subscriber Configuration Formats

[](#subscriber-configuration-formats)

```
return [
    EventClass::class => 'methodName',
    EventClass::class => ['methodName', 10],                         // with priority
    EventClass::class => [['firstMethod', 20], ['secondMethod', 0]], // multiple handlers
];
```

### Using the Factory

[](#using-the-factory)

```
use Solo\EventDispatcher\EventDispatcherFactory;

$dispatcher = EventDispatcherFactory::create(
    listeners: [
        UserRegistered::class => fn (UserRegistered $e) => print "Welcome!\n",
    ],
    subscribers: [
        new WelcomeEmailSubscriber(),
        WelcomeEmailSubscriber::class,           // or class-string
        fn () => new WelcomeEmailSubscriber(),   // or factory callable
    ],
);
```

### Error Logging

[](#error-logging)

By default, if a listener throws an exception, it bubbles up to the caller. Pass a PSR-3 `LoggerInterface` to catch errors, log them, and continue executing the remaining listeners:

```
use Solo\EventDispatcher\{EventDispatcher, ListenerProvider};
use Psr\Log\LoggerInterface;

$dispatcher = new EventDispatcher($provider, $logger);

// or via factory
$dispatcher = EventDispatcherFactory::create(
    listeners: [...],
    subscribers: [...],
    logger: $logger,
);
```

When a listener fails, the following context is logged at `error` level:

KeyDescription`exception`Exception class name`message`Exception message`event`Event class name`listener`Listener description (e.g. `Closure`, `MyClass::onEvent`)`file`File where the exception was thrown`line`Line number### Stoppable Events

[](#stoppable-events)

```
use Solo\EventDispatcher\AbstractStoppableEvent;

final class OrderPlaced extends AbstractStoppableEvent
{
    public function __construct(public int $orderId) {}
}

$provider->addListener(OrderPlaced::class, function (OrderPlaced $e) {
    if ($e->orderId < 0) {
        $e->stopPropagation(); // subsequent listeners won't be called
    }
}, priority: 100);

$provider->addListener(OrderPlaced::class, function (OrderPlaced $e) {
    // this won't run if propagation was stopped
});
```

### Checking for Listeners

[](#checking-for-listeners)

```
// checks for listeners including parent classes and interfaces
if ($provider->hasListenersFor(UserRegistered::class)) {
    $dispatcher->dispatch(new UserRegistered('john'));
}
```

Testing
-------

[](#testing)

```
composer test        # cs-check + analyze + phpunit
composer cs-check    # PHPCS (PSR-12)
composer cs-fix      # PHPCBF auto-fix
composer analyze     # PHPStan (level 8)
```

License
-------

[](#license)

MIT

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance87

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

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

Every ~104 days

Total

3

Last Release

64d ago

PHP version history (2 changes)v1.0.0PHP ^8.1

v1.1.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f29817cec408d033cd4441c8f760e3ae40248dc0f66856a09080d282aee6959?d=identicon)[Vitaliy Olos](/maintainers/Vitaliy%20Olos)

---

Top Contributors

[![SoloPHP](https://avatars.githubusercontent.com/u/175482616?v=4)](https://github.com/SoloPHP "SoloPHP (3 commits)")

---

Tags

eventpsr-14event dispatcherdispatcherevent listener

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[phly/phly-event-dispatcher

Experimental event dispatcher for PSR-14

26209.9k4](/packages/phly-phly-event-dispatcher)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)

PHPackages © 2026

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