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

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

kelvinmo/f3-event-dispatcher
============================

A PSR-14 compliant event dispatcher for the Fat-Free Framework

v1.0.0(1y ago)14.3k↓73.6%[1 PRs](https://github.com/kelvinmo/f3-event-dispatcher/pulls)GPL-3.0-or-laterPHPPHP ^7.2 || ^8.0CI passing

Since Jul 11Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/kelvinmo/f3-event-dispatcher)[ Packagist](https://packagist.org/packages/kelvinmo/f3-event-dispatcher)[ RSS](/packages/kelvinmo-f3-event-dispatcher/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (4)Versions (9)Used By (0)

Event Dispatcher for the Fat-Free Framework
===========================================

[](#event-dispatcher-for-the-fat-free-framework)

This is a simple [PSR-14](https://www.php-fig.org/psr/psr-14/) compliant event dispatcher and listener provider library for the [Fat-Free Framework](https://fatfreeframework.com/).

[![Latest Stable Version](https://camo.githubusercontent.com/0aebb6c3aac2d6ea5e00242a392bfd26262ab0c1d37539ad8e9cc3336a57f74f/68747470733a2f2f706f7365722e707567782e6f72672f6b656c76696e6d6f2f66332d6576656e742d646973706174636865722f762f737461626c65)](https://packagist.org/packages/kelvinmo/f3-event-dispatcher)[![build](https://github.com/kelvinmo/f3-event-dispatcher/workflows/CI/badge.svg)](https://github.com/kelvinmo/f3-event-dispatcher/actions?query=workflow%3ACI)

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

[](#requirements)

- PHP 7.2 or later
- Fat-Free Framework 3.5 or later

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

[](#installation)

You can install via [Composer](http://getcomposer.org/).

```
composer require kelvinmo/f3-event-dispatcher
```

Usage
-----

[](#usage)

### Listener Provider

[](#listener-provider)

The listener provider is implemented by the `\Listeners` class. `Listeners`is a subclass of Fat-Free's `\Prefab` class.

```
$listeners = \Listeners::instance();
```

To add a listener, call the `on()` method. The name of the event is specified in the first parameter and the listener in the second parameter.

As required by [PSR-14](https://www.php-fig.org/psr/psr-14/), if the name of the event is the name of a class, then the listener will also be triggered for all subclasses of that event class.

The listener can be a PHP callable, or a string that can be resolved by Fat-Free's [`call()`](https://fatfreeframework.com/3.7/base#call) method

```
// Object method
$listeners->on(FooEvent::class, 'Bar->listener');

// Static method
$listeners->on(FooEvent::class, 'Bar::listener');

// PHP callable
$listeners->on(FooEvent::class, [ $object, 'listener' ]);

// Closure
$listeners->on(FooEvent::class, function($event) {
    // listener
});
```

The `on()` method also takes a third, optional parameter, specifying the priority which the listeners should be called. Listeners are called from the highest priority to the lowest.

```
// Baz->listener is called first, then Bar->listener
$listeners->on(FooEvent::class, 'Bar->listener', 10);
$listeners->on(FooEvent::class, 'Baz->listener', 20);
```

You can use `Listeners` with any [PSR-14](https://www.php-fig.org/psr/psr-14/)-compliant event dispatcher.

#### Generic Events

[](#generic-events)

Sometimes it is too cumbersome to create a new event class for every single event. You can use *generic events* to group a set of related events into a single class.

A generic event implements `GenericEventInterface` and provides the name of the event through the `getEventName()` method.

```
class BarEvent implements GenericEventInterface {
    private $eventName;

    public function __construct($eventName) {
        $this->eventName = $eventName;
    }

    public function getEventName() {
        return $this->eventName;
    }
}

$listeners->on('foo', 'Baz->listener');
$event = new BarEvent('foo');
```

#### Adding Listeners via Reflection

[](#adding-listeners-via-reflection)

You can also add listeners using reflection by the `map()` method. This method takes a name or an instantiated object of a class. A listener will be added based on a method of this class if:

1. It is a `public` method (whether or not it is also `static`)
2. The name of the method starts with `on`
3. The method takes exactly one parameter, and the parameter is type-hinted with an event class.

The name of the event the method will listen to depends on the name of the method. If the name of the method is the same as the short name of the type hint of the parameter, then the name of the event is the fully qualified name of the parameter type. Otherwise, the name of the event is the method name converted to snake case, in which case the event type must be a generic event (i.e. implements `GenericEventInterface`).

```
class TestListener {
    // Will be mapped to FooEvent (with namespace)
    public function onFooEvent(FooEvent $event) {
    }

    // Will be mapped to custom_event
    // (BarEvent must implement GenericEventInterface)
    public function onCustomEvent(BarEvent $event) {
    }
}

$listeners = \Listeners::instance();
$listeners->map(TestListener::class);
```

### Event Dispatcher

[](#event-dispatcher)

The event dispatcher is implemented by the `\Events` class. `Events`is a subclass of Fat-Free's `\Prefab` class.

```
$dispatcher = \Events::instance();
```

By default, `Events` uses the `Listeners` listener provider included in this library. To use a different listener provider, pass the provider as an argument in the constructor.

```
use League\Event\PrioritizedListenerRegistry;

$listenerProvider = new PrioritizedListenerRegistry();
$dispatcher = \Events::instance($listenerProvider);
```

To use the event dispatcher, call the standard PSR-14 `dispatch()` method:

```
$dispatcher->dispatch(new FooEvent());
```

Licence
-------

[](#licence)

GPL 3 or later

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance58

Moderate activity, may be stable

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 98.3% 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 ~261 days

Recently: every ~327 days

Total

6

Last Release

512d ago

Major Versions

v0.1.4 → v1.0.02025-02-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/467c1767396cee916f7dfb3a04f0dcef1714b72940a27fee0b58f96a8aa3b608?d=identicon)[kelvinmo](/maintainers/kelvinmo)

---

Top Contributors

[![kelvinmo](https://avatars.githubusercontent.com/u/1594601?v=4)](https://github.com/kelvinmo "kelvinmo (57 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

eventevents

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[doctrine/event-manager

The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.

6.0k526.1M158](/packages/doctrine-event-manager)[laminas/laminas-eventmanager

Trigger and listen to events within a PHP application

1.0k72.5M255](/packages/laminas-laminas-eventmanager)[tormjens/eventy

The WordPress filter/action system in Laravel

439951.1k24](/packages/tormjens-eventy)[fig/event-dispatcher-util

Useful utility classes and traits for implementing the PSR events standard

27473.2k21](/packages/fig-event-dispatcher-util)[zumba/symbiosis

Symbiosis, event structure for bootstrapping plugins.

1361.5k1](/packages/zumba-symbiosis)[codefog/contao-events_subscriptions

events\_subscriptions extension for Contao Open Source CMS

142.8k](/packages/codefog-contao-events-subscriptions)

PHPackages © 2026

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