andrewdyer/event-dispatcher

A framework-agnostic library for implementing the observer pattern through named events and registered listeners

Maintainers

Package info

github.com/andrewdyer/event-dispatcher

pkg:composer/andrewdyer/event-dispatcher

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

1.0.1 2026-06-16 22:19 UTC

This package is auto-updated.

Last update: 2026-06-16 22:40:51 UTC


README

A framework-agnostic library for implementing the observer pattern through named events and registered listeners.

Latest Stable Version Total Downloads License PHP Version Require

Introduction

This library lets events be defined as classes and listeners attached to them by name, with the dispatcher notifying every registered listener in the order they were added when an event is triggered. Listeners are kept fully decoupled from the code that raises the event, making it straightforward to extend application behaviour without modifying the originating logic, regardless of the framework in use.

Prerequisites

  • PHP: Version 8.3 or higher is required.
  • Composer: Dependency management tool for PHP.

Installation

composer require andrewdyer/event-dispatcher

Getting Started

1. Define an event

Create a class that implements EventInterface, or extend AbstractEvent. By default, the event name is the short (unqualified) class name, but this can be overridden:

namespace App\Events;

use AndrewDyer\EventDispatcher\Events\AbstractEvent;

class UserRegistered extends AbstractEvent
{
    public function getName(): string
    {
        return 'UserRegistered';
    }
}

2. Create a listener

Create a class that implements ListenerInterface, or extend AbstractListener:

namespace App\Listeners;

use AndrewDyer\EventDispatcher\Events\EventInterface;
use AndrewDyer\EventDispatcher\Listeners\AbstractListener;

class SendRegistrationEmail extends AbstractListener
{
    public function handle(EventInterface $event): void
    {
        // Send welcome email to user...
    }
}

3. Set up the dispatcher

Instantiate EventDispatcher:

use AndrewDyer\EventDispatcher\EventDispatcher;

$dispatcher = new EventDispatcher();

Usage

Registering a listener

Attach a listener to a specific event name via addListener():

use App\Listeners\SendRegistrationEmail;

$dispatcher->addListener('UserRegistered', new SendRegistrationEmail());

Multiple listeners can be registered against the same event name; they are executed in the order they were added.

Dispatching an event

Trigger an event and notify all of its registered listeners via dispatch():

use App\Events\UserRegistered;

$dispatcher->dispatch(new UserRegistered());

License

Licensed under the MIT licence and is free for private or commercial projects.