andrewdyer / event-dispatcher
A simple event dispatcher that you can fit into the framework of your choice
Requires
- php: ^8.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpunit/phpunit: ^10.5
README
Event Dispatcher
A simple event dispatcher that you can fit into the framework of your choice.
⚖️ License
Licensed under the MIT license and is free for private or commercial projects.
✨ Introduction
Event Dispatcher is a lightweight, framework-agnostic library for implementing the observer pattern in PHP. It enables you to define events and attach listeners that react when those events are triggered - keeping your code clean, modular, and easy to extend.
📋 Prerequisites
Before you begin, ensure you have met the following requirements:
- PHP: Version 8.3 or higher.
- Composer: A dependency manager for PHP, used to install packages and autoload your code.
📦 Installation
Install the package via Composer:
composer require andrewdyer/event-dispatcher
🚀 Getting Started
1. Define Your Events
Create a class that implements AndrewDyer\EventDispatcher\Events\EventInterface
(or extend AbstractEvent
).
By default, the event name is the class name, but you can override it:
namespace App\Events; use AndrewDyer\EventDispatcher\Events\AbstractEvent; class UserRegistered extends AbstractEvent { public function getName(): string { return 'UserRegistered'; } }
2. Create Your Listeners
Listeners must implement AndrewDyer\EventDispatcher\Listeners\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... } }
📖 Usage
Initialize the Dispatcher
Create a new instance of the event dispatcher:
use AndrewDyer\EventDispatcher\EventDispatcher; $dispatcher = new EventDispatcher();
Register a Listener
Attach a listener class to respond to a specific event:
use App\Listeners\SendRegistrationEmail; $dispatcher->addListener('UserRegistered', new SendRegistrationEmail());
💡 Tip: You can register multiple listeners for the same event name, and they will be executed in the order they were added.
Dispatch an Event
Trigger the event and notify all registered listeners:
use App\Events\UserRegistered; $dispatcher->dispatch(new UserRegistered());
🤝 Contributing
Found a bug or want to improve this package? Feel free to open a pull request or submit an issue.