PHPackages                             jalle19/laravel-deferred-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. jalle19/laravel-deferred-event-dispatcher

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

jalle19/laravel-deferred-event-dispatcher
=========================================

A deferring event dispatcher for the Laravel and Lumen frameworks

1.1.0(5y ago)140.5k1MITPHPPHP &gt;=7.1CI failing

Since Feb 3Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Jalle19/laravel-deferred-event-dispatcher)[ Packagist](https://packagist.org/packages/jalle19/laravel-deferred-event-dispatcher)[ RSS](/packages/jalle19-laravel-deferred-event-dispatcher/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

laravel-deferred-event-dispatcher
=================================

[](#laravel-deferred-event-dispatcher)

[![Build Status](https://camo.githubusercontent.com/3cd132fa1a489890453ed55b3604e0e10c2ee5b662df96a331dd56c810b8ea2d/68747470733a2f2f7472617669732d63692e6f72672f4a616c6c6531392f6c61726176656c2d64656665727265642d6576656e742d646973706174636865722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Jalle19/laravel-deferred-event-dispatcher)[![Coverage Status](https://camo.githubusercontent.com/b94e7094f07958227585440707f32efee430f1fdceb568f076992842e05509da/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4a616c6c6531392f6c61726176656c2d64656665727265642d6576656e742d646973706174636865722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Jalle19/laravel-deferred-event-dispatcher?branch=master)

A deferring event dispatcher for the Laravel and Lumen frameworks.

Motivation
----------

[](#motivation)

Events are a powerful feature of any framework. It allows you to decouple your code so that the logic that triggers an event doesn't have to know what should happen in reaction to that event.

While the default implementation in Laravel/Lumen is perfectly adequate in many cases, it is fairly naive, which can lead to subpar performance. Consider the following scenario:

1. You have your own `EventServiceProvider` where you have registered a bunch of event subscribers.
2. You register this service provider in your bootstrap file

Under the hood, whenever an `EventServiceProvider` is registered, the `EventDispatcher` will also be constructed. The event dispatcher in turn will go through all the event handlers you've defined and construct each of them so that it knows which listeners to trigger in the case of an event.

While this doesn't sound that bad, it means that even for the simplest of requests (e.g. a route that only prints "OK", e.g. a health check route) will cause all your event subscribers to be constructed, which can pull in a lot of code that you don't need to fulfill the request (e.g. Doctrine and other "heavy" services").

This wouldn't be so bad if it weren't for the fact that the majority of your requests probably won't be triggering any events, so constructing and registering all your event handlers is pointless. This event dispatcher implementation aims to solve that problem.

### Deferring the resolving of listeners and subscribers

[](#deferring-the-resolving-of-listeners-and-subscribers)

A simple yet fairly effective solution is to defer the resolving of event handlers until an event is actually dispatched. This assures we don't do any unnecessary work in requests that don't trigger any events. But there's a problem...

A lot of services use events under the hood:

- Some service providers will dispatch events before and after they've booted to allow other service providers to hook into these and perform optional bootstraping logic
- The cache repository will dispatch `Illuminate/Cache/Events/CacheHit` and `Illuminate/Cache/Events/CacheMissed`events every time you attempt to retrieve something from your cache. Depending on your application, this is something that could happen during practically every request.

Even if we deferr the resolving of event handlers until an event is dispatched, chances are there is at least one event dispatched on every request anyway, defeating the whole point.

### Deferring specific events

[](#deferring-specific-events)

Chances are you don't care about whether a cache operation resulted in a hit or a miss, or whether a service provider has been booted or not. Ignoring these events would defer the resolving of event handlers even further, possibly for the duration of the request, which finally means we've accomplished our goal of not constructing all event handlers needlessly.

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

[](#requirements)

- Laravel/Lumen &gt;= 5.4
- PHP &gt;= 7.1

Usage
-----

[](#usage)

1. Install the library using Composer:

```
composer require jalle19/laravel-deferred-event-dispatcher
```

2. Swap the default event dispatcher for this one:

```
// The event dispatcher must be a singleton
$app->singleton(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, function () use ($app) {
    return new \Jalle19\Laravel\Events\DeferredEventDispatcher($app, [
        // Cache events
        Illuminate\Cache\Events\CacheHit::class,
        Illuminate\Cache\Events\CacheMissed::class,
    ]);
});

// Swap the default implementation for this one. Some classes type-hint the interface, others simply use "events"
$app->alias(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, 'events');
$app->alias(\Jalle19\Laravel\Events\DeferredEventDispatcher::class, Illuminate\Contracts\Events\Dispatcher::class);
```

In this example we've decided to defer resolving of event handlers whenever a cache event is dispatched.

3. Make sure your event service provider is registered somewhere after these lines

That's it, your event handlers will now be resolved only when an event that you haven't explicitly deferred is dispatched!

License
-------

[](#license)

MIT

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

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 ~943 days

Total

2

Last Release

2077d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d7e28ec3bf2ff3ffcdd8c947f54d53d4dae86939bea2a7316cfde0b10410e9ee?d=identicon)[Jalle19](/maintainers/Jalle19)

---

Top Contributors

[![Jalle19](https://avatars.githubusercontent.com/u/1106133?v=4)](https://github.com/Jalle19 "Jalle19 (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jalle19-laravel-deferred-event-dispatcher/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3274.9M308](/packages/psalm-plugin-laravel)[laragear/preload

Effortlessly make a Preload script for your Laravel application.

119363.5k](/packages/laragear-preload)[flarum/core

Delightfully simple forum software.

211.3M1.9k](/packages/flarum-core)[guava/calendar

Adds support for vkurko/calendar to Filament PHP.

298241.0k3](/packages/guava-calendar)[tonysm/rich-text-laravel

Integrates Trix content with Laravel

46577.8k1](/packages/tonysm-rich-text-laravel)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

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