ghostwriter / event-dispatcher
Event Dispatcher implementation.
Fund package maintenance!
ghostwriter
Installs: 2 251 618
Dependents: 13
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.3
- ghostwriter/container: ^4.0.2
Requires (Dev)
- ghostwriter/coding-standard: dev-main
- 6.0.x-dev
- 5.1.x-dev
- 5.0.x-dev
- 5.0.2
- 5.0.1
- 5.0.0
- 4.0.0
- 3.0.0
- 2.0.0
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
- dev-chore/update/dot-gitattributes/2ebf2fd
- dev-chore/update/readme/bb95d86
- dev-chore/automation/e94aa84
- dev-chore/update/dot-gitattributes/d332cf6
- dev-chore/psalm/configuration
This package is auto-updated.
Last update: 2025-01-28 18:43:10 UTC
README
Provides an Event Dispatcher implementation for PHP.
Installation
You can install the package via composer:
composer require ghostwriter/event-dispatcher
Star ⭐️ this repo if you find it useful
You can also star (🌟) this repo to find it easier later.
Usage
Registering and dispatching an Event Listener.
use Ghostwriter\EventDispatcher\EventDispatcher; use Ghostwriter\EventDispatcher\ListenerProvider; // Create an event class final class ExampleEvent { } // Create an Event Listener final class ExampleEventListener { public function __invoke(ExampleEvent $event): void { // Handle the event, e.g., print the event class name // echo $event::class; } } // Create a ListenerProvider $provider = ListenerProvider::new(); // or new ListenerProvider() // Bind the Listener to the Event $provider->bind(ExampleEvent::class, ExampleEventListener::class); // Create an EventDispatcher $dispatcher = EventDispatcher::new($provider); // or new EventDispatcher($provider) // Dispatch the Event. $event = $dispatcher->dispatch(new ExampleEvent()); // Assert the Event is the same as the dispatched Event assert($event instanceof ExampleEvent);
Event Subscriber
Registering an Event Subscriber.
use Ghostwriter\EventDispatcher\Interface\ListenerProviderInterface; use Ghostwriter\EventDispatcher\Interface\SubscriberInterface; use Override; final class EventSubscriber implements SubscriberInterface { /** * @throws Throwable */ #[Override] public function __invoke(ListenerProviderInterface $provider): void { // InvokableListener '::__invoke' $provider->bind( TestEvent::class, TestEventListener::class, ); } } // Create a ListenerProvider $provider = ListenerProvider::new(); // or new ListenerProvider() // Subscribe the EventSubscriber $provider->subscribe(EventSubscriber::class); // Create an EventDispatcher $dispatcher = EventDispatcher::new($provider); // or new EventDispatcher($provider) // Dispatch the Event. $event = $dispatcher->dispatch(new TestEvent()); // Assert the Event is the same as the dispatched Event assert($event instanceof TestEvent);
Changelog
Please see CHANGELOG.md for more information what has changed recently.
Security
If you discover any security related issues, please email nathanael.esayeas@protonmail.com
or create a Security Advisory instead of using the issue tracker.
License
The BSD-3-Clause. Please see License File for more information.