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

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

lubobill1990/event-dispatcher
=============================

Yii Event Dispatcher

012PHP

Since Jun 14Pushed 5y agoCompare

[ Source](https://github.com/lubobill1990/event-dispatcher)[ Packagist](https://packagist.org/packages/lubobill1990/event-dispatcher)[ RSS](/packages/lubobill1990-event-dispatcher/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

 [ ![](https://avatars0.githubusercontent.com/u/993323) ](https://github.com/yiisoft)

Yii Event Dispatcher
====================

[](#yii-event-dispatcher)

[PSR-14](http://www.php-fig.org/psr/psr-14/) compatible event dispatcher provides an ability to dispatch events and listen to events dispatched.

[![Latest Stable Version](https://camo.githubusercontent.com/80d62776c7bc1af643db4dbed541f589b6861b247b2855ee4346a5cfbec50159/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f6576656e742d646973706174636865722f762f737461626c652e706e67)](https://packagist.org/packages/yiisoft/event-dispatcher)[![Total Downloads](https://camo.githubusercontent.com/02d553ab56076b28972c0051522e8568ccbb2f1e639b9d091310f1dede5b632c/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f6576656e742d646973706174636865722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yiisoft/event-dispatcher)[![Build Status](https://github.com/yiisoft/event-dispatcher/workflows/build/badge.svg)](https://github.com/yiisoft/event-dispatcher/actions?query=workflow%3Abuild)[![Code Coverage](https://camo.githubusercontent.com/cb0f8d7bb1b10377b0e98ecf46f1c8014852d4d551fa608fa89772f981ed3f3f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f796969736f66742f6576656e742d646973706174636865722f6261646765732f636f7665726167652e706e67)](https://scrutinizer-ci.com/g/yiisoft/event-dispatcher/)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/64a06e3d0b748f74c6fb07bbca93887e17777be76f6bbecc7b6ee42252d7326e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f796969736f66742f6576656e742d646973706174636865722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/yiisoft/event-dispatcher/?branch=master)[![Mutation testing badge](https://camo.githubusercontent.com/1596d100e415c6035b1932b4e97bbb407fde4b235e78baf7ddbec908b3b7b27b/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d68747470733a2f2f62616467652d6170692e737472796b65722d6d757461746f722e696f2f6769746875622e636f6d2f796969736f66742f6576656e742d646973706174636865722f6d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/event-dispatcher/master)[![static analysis](https://github.com/yiisoft/event-dispatcher/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/event-dispatcher/actions?query=workflow%3A%22static+analysis%22)

### Features

[](#features)

- [PSR-14](http://www.php-fig.org/psr/psr-14/) compatible.
- Simple and lightweight.
- Encourages designing event hierarchy.
- Can combine multiple event listener providers.

### General usage

[](#general-usage)

The library consists of two parts: event dispatcher and event listener provider. Provider's job is to register listeners for a certain event type. Dispatcher's job is to take an event, get listeners for it from a provider and call them sequentially.

```
// add some listeners
$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // do something with document
    });

$provider = new Yiisoft\EventDispatcher\Provider\Provider($listeners);
$dispatcher = new Yiisoft\EventDispatcher\Dispatcher\Dispatcher($provider);
```

The event dispatching may look like:

```
class DocumentProcessor
{
    public function process(Document $document)
    {
        // process the document
        $dispatcher->dispatch(new AfterDocumentProcessed($document));
    }
}
```

### Stoppable events

[](#stoppable-events)

Event could be made stoppable by implementing `Psr\EventDispatcher\StoppableEventInterface`:

```
class BusyEvent implements Psr\EventDispatcher\StoppableEventInterface
{
    // ...

    public function isPropagationStopped(): bool
    {
        return true;
    }
}
```

This way we can ensure that only first event listener will be able to handle the event. Another option is to allow stopping propagation in one of the listeners by providing corresponding event method.

### Events hierarchy

[](#events-hierarchy)

Events do not have any name or wildcard matching on purpose. Event class names and class/interface hierarchy and composition could be used to achieve great flexibility:

```
interface DocumentEvent
{
}

class BeforeDocumentProcessed implements DocumentEvent
{
}

class AfterDocumentProcessed implements DocumentEvent
{
}
```

With the interface above listening to all document-related events could be done as:

```
$listeners->add(function (DocumentEvent $event) {
    // log events here
});
```

### Combining multiple listener providers

[](#combining-multiple-listener-providers)

In case you want to combine multiple listener providers, you can use `CompositeProvider`:

```
$compositeProvider = new Yiisoft\EventDispatcher\Provider\CompositeProvider();
$provider = new Yiisoft\EventDispatcher\Provider\Provider();
$compositeProvider->add($provider);
$compositeProvider->add(new class implements ListenerProviderInterface {
    public function getListenersForEvent(object $event): iterable
    {
        yield function ($event) {
            // handle
        };
    }
});

$dispatcher = new Yiisoft\EventDispatcher\Dispatcher\Dispatcher($compositeProvider);
```

### Register listeners with concrete event names

[](#register-listeners-with-concrete-event-names)

You may use a more simple listener provider, which allows you to specify which event they can provide.

It can be useful in some specific cases, for instance if one of your listeners does not need the event object passed as a parameter (can happen if the listener only needs to run at a specific stage during runtime, but does not need event data).

In that case, it is advised to use the aggregate (see above) if you need features from both providers included in this library.

```
$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(static function () {
    // this function does not need an event object as argument
}, SomeEvent::class);
```

### Unit testing

[](#unit-testing)

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

```
./vendor/bin/phpunit
```

### Mutation testing

[](#mutation-testing)

The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:

```
./vendor/bin/infection
```

### Static analysis

[](#static-analysis)

The code is statically analyzed with [Phan](https://github.com/phan/phan/wiki). To run static analysis:

```
./vendor/bin/phan
```

### Credits

[](#credits)

- Larry Garfield (@crell) for initial implementation of deriving callable parameter type.

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 64.5% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/641540?v=4)[Bo Lu](/maintainers/lubobill1990)[@lubobill1990](https://github.com/lubobill1990)

---

Top Contributors

[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (40 commits)")[![romm](https://avatars.githubusercontent.com/u/6342901?v=4)](https://github.com/romm "romm (4 commits)")[![xepozz](https://avatars.githubusercontent.com/u/6815714?v=4)](https://github.com/xepozz "xepozz (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![yiiliveext](https://avatars.githubusercontent.com/u/37578608?v=4)](https://github.com/yiiliveext "yiiliveext (3 commits)")[![terabytesoftw](https://avatars.githubusercontent.com/u/42547589?v=4)](https://github.com/terabytesoftw "terabytesoftw (3 commits)")[![lubobill1990](https://avatars.githubusercontent.com/u/641540?v=4)](https://github.com/lubobill1990 "lubobill1990 (2 commits)")[![romkatsu](https://avatars.githubusercontent.com/u/1677515?v=4)](https://github.com/romkatsu "romkatsu (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![hiqsol](https://avatars.githubusercontent.com/u/11820365?v=4)](https://github.com/hiqsol "hiqsol (1 commits)")[![azjezz](https://avatars.githubusercontent.com/u/29315886?v=4)](https://github.com/azjezz "azjezz (1 commits)")

### Embed Badge

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

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

###  Alternatives

[dragonmantank/cron-expression

CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due

4.7k510.8M734](/packages/dragonmantank-cron-expression)[spatie/laravel-enum

Laravel Enum support

3685.8M35](/packages/spatie-laravel-enum)[bref/extra-php-extensions

Extra PHP extensions for your lambda application.

2264.5M10](/packages/bref-extra-php-extensions)[owenmelbz/nova-radio-field

A Laravel Nova radio button field type with the ability to toggle other field visibility.

34529.1k10](/packages/owenmelbz-nova-radio-field)[laradevsbd/zkteco-sdk

ZKLibrary is PHP Library for ZK Time &amp; Attendance Devices

489.8k](/packages/laradevsbd-zkteco-sdk)

PHPackages © 2026

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