ttpryg / event-dispatcher
PSR-14 Compliant Event Dispatcher & Listener Provider standalone PHP 8.1+ library
1.0.0
2026-09-21 14:33 UTC
Requires
- php: ^8.1
- psr/event-dispatcher: ^1.0
Requires (Dev)
- laravel/pint: ^1.20
- phpunit/phpunit: ^10.0
- rector/rector: ^2.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-21 14:35:29 UTC
README
A lightweight, PSR-14 compliant Event Dispatcher & Listener Provider for PHP 8.1+.
๐ Key Features
- PSR-14 Compliant: Fully compatible with PSR-14 contracts (
EventDispatcherInterface,ListenerProviderInterface,StoppableEventInterface). - Framework Agnostic: Clean, decoupled design that works in any PHP application or framework.
- Stoppable Events Support: Safely halt propagation chains when
isPropagationStopped()returns true. - Strictly Typed: Built with modern PHP 8.1+ features (readonly properties, strict types).
๐ฆ Installation
Install the package via Composer:
composer require ttpryg/event-dispatcher
Requirements: PHP 8.1 or higher.
๐ Quick Start
1. Define an Event
namespace App\Event; use Psr\EventDispatcher\StoppableEventInterface; class UserRegisteredEvent implements StoppableEventInterface { private bool $stopped = false; public function __construct( public readonly string $userId, public readonly string $email ) {} public function isPropagationStopped(): bool { return $this->stopped; } public function stopPropagation(): void { $this->stopped = true; } }
2. Register Listeners & Dispatch
use Ttpryg\EventDispatcher\Provider\ListenerProvider; use Ttpryg\EventDispatcher\Dispatcher\EventDispatcher; use App\Event\UserRegisteredEvent; // 1. Initialize Listener Provider $provider = new ListenerProvider(); // Register listener (Closure or callable) $provider->addListener(UserRegisteredEvent::class, function (UserRegisteredEvent $event) { echo "Welcome email sent to: " . $event->email; }); // 2. Initialize Dispatcher & Dispatch Event $dispatcher = new EventDispatcher($provider); $dispatcher->dispatch(new UserRegisteredEvent("usr_101", "user@example.com"));
๐ณ Architecture
event-dispatcher/
โโโ src/
โ โโโ Dispatcher/
โ โ โโโ EventDispatcher.php
โ โโโ Provider/
โ โ โโโ ListenerProvider.php
โ โโโ Exceptions/
โ โโโ InvalidListenerException.php
โโโ tests/
โ โโโ EventDispatcherTest.php
โ โโโ ListenerProviderTest.php
โโโ composer.json
๐งช Testing
Run the test suite using PHPUnit:
composer test # or ./vendor/bin/phpunit
๐ License
This project is open-sourced software licensed under the MIT License.