super-kernel / event-dispatcher
PSR-14 event dispatching with native Attribute listener discovery for SuperKernel.
Requires
- php: >=8.4.27 <8.5
- psr/container: ^2.0.2
- psr/event-dispatcher: ^1.0
- super-kernel/annotator: 1.0.0-alpha.2
- super-kernel/attribute: 1.0.0-alpha.2
- super-kernel/contract: 1.0.0-alpha.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95.27
- phpunit/phpunit: ^13.4.5
Suggests
None
Provides
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-11 02:23:37 UTC
README
English | 中文文档
SuperKernel Event Dispatcher
super-kernel/event-dispatcher is the PSR-14 event dispatcher of SuperKernel: it finds the classes declared with #[Listener], creates them through the container, and calls the ones that match an event synchronously, in priority order.
Installation
composer require super-kernel/event-dispatcher
The package provides psr/event-dispatcher-implementation 1.0. It creates listeners through a PSR-11 container, super-kernel/di in a SuperKernel application, and finds them in the metadata that skernel build generates, so a project needs SKERNEL to build it.
What it provides
Psr\EventDispatcher\EventDispatcherInterface, which dispatches any event object and returns it.- Listeners declared with
#[Listener]andListenerInterface, ordered by priority. - Matching by class: a listener of a class or interface also receives its subclasses and implementations.
- Stopping propagation through PSR-14's
StoppableEventInterface. Psr\EventDispatcher\ListenerProviderInterface, which lists the listeners of an event.
It does not:
- dispatch asynchronously or defer events;
- collect listener return values;
- register or remove listeners at run time;
- adapt Symfony's event dispatcher;
super-kernel/frameworkdoes.
Quick start
Declare an event. Save it as src/Account/UserRegistered.php:
<?php declare(strict_types=1); namespace Src\Account; final readonly class UserRegistered { public function __construct(public string $email) { } }
Declare a listener of the event. Save it as src/Account/SendWelcomeMail.php:
<?php declare(strict_types=1); namespace Src\Account; use SuperKernel\Attribute\Listener; use SuperKernel\Contract\ListenerInterface; #[Listener] final readonly class SendWelcomeMail implements ListenerInterface { public function listen(): array { return [ UserRegistered::class, ]; } /** * @param UserRegistered $event */ public function process(object $event): void { echo "Welcome mail sent to {$event->email}", PHP_EOL; } }
Dispatch the event from a script. Save it as bin/register.php:
<?php declare(strict_types=1); use Psr\EventDispatcher\EventDispatcherInterface; use Src\Account\UserRegistered; use SuperKernel\Context\ApplicationContext; require dirname(__DIR__) . '/vendor/autoload.php'; $events = ApplicationContext::getContainer()->get(EventDispatcherInterface::class); $events->dispatch(new UserRegistered('ada@example.com'));
Build the project and run the script:
skernel build php bin/register.php
It prints Welcome mail sent to ada@example.com.
Public surface
| Name | Kind | Purpose |
|---|---|---|
SuperKernel\Attribute\Listener |
Attribute | Registers the annotated class as a listener; priority orders it, higher first. |
SuperKernel\Contract\ListenerInterface |
Interface | Implemented by a listener: listen() names its events, process() handles one. |
Psr\EventDispatcher\EventDispatcherInterface |
Interface | Provided by the container; dispatches an event to its listeners. |
Psr\EventDispatcher\ListenerProviderInterface |
Interface | Provided by the container; returns the listeners of an event in order. |
Behavior to know
- Listeners run synchronously, higher priorities first and equal priorities in the order the build found them;
dispatch()returns the same event object. - A listener's exception propagates unchanged out of
dispatch(), and later listeners do not run. - Every listener is created when the dispatcher is first needed. A listener that does not implement
ListenerInterface, or whoselisten()does not return a list of existing class or interface names, raisesInvalidArgumentExceptionthen, so no event can be dispatched andskernel buildfails. - Listeners are found in the build metadata: run
skernel buildagain after adding, removing, or changing a listener, or start withskernel run, which builds first.
Documentation
The developer documentation describes declaring listeners, matching several events, stopping propagation, the listener provider, and the error messages: SuperKernel Event Dispatcher.
License
Released under the MIT License. See LICENSE.