a50 / event-dispatcher
Event Dispatcher
Requires
- php: ^8.1
- a50/container: dev-main
- innmind/immutable: ^5.0
- psr/event-dispatcher: ^1.0
- webmozart/assert: ^1.11
This package is auto-updated.
Last update: 2024-12-10 05:42:21 UTC
README
A50 Event Dispatcher
This is PSR-14 Event Dispatcher pretty simple implementation and framework-agnostic solution.
Why another one?
Because:
- you want to follow standard and don't worry about implementation. You can change this provider to another one anytime;
- you don't need complicated and overhead solution;
- you want easy configure and use event dispatcher.
Requirements
- The minimum PHP version is 8.0.
Installation
You can install the package via composer:
composer require a50/event-dispatcher
Usage
For standalone usage example below. In your entity you need to register event:
<?php declare(strict_types=1); namespace YourProject\Domain; use A50\EventDispatcher\EventRecordingCapabilities; final class Post { use EventRecordingCapabilities; // use trait to register and release events private function __construct(private PostId $id) { } public static function create(PostId $id): self { $self = new self($id); $self->registerThat(PostWasCreated::withId($id)); // register event return $self; } }
Configure Psr\EventDispatcher\EventDispatcherInterface
:
<?php declare(strict_types=1); require __DIR__ . 'vendor/autoload.php'; use A50\EventDispatcher\ImmutablePrioritizedListenerProvider; use A50\EventDispatcher\ListenerPriority; use A50\EventDispatcher\PrioritizedListenerProvider; use A50\EventDispatcher\SyncEventDispatcher; use YourProject\Domain\Post; use YourProject\Domain\PostId; use YourProject\Domain\PostWasCreated; use YourProject\Application\SendEmailToModerator; use YourProject\Domain\UserWasRegistered; use YourProject\Application\SendWelcomeEmail; $registry = new ImmutablePrioritizedListenerProvider([ new PrioritizedListenerProvider(PostWasCreated::class, [ ListenerPriority::NORMAL => new SendEmailToModerator(), ]), new PrioritizedListenerProvider(UserWasRegistered::class, [ ListenerPriority::NORMAL => new SendWelcomeEmail(), ]), ]); $dispatcher = new SyncEventDispatcher($registry); // And in your application use case: $post = Post::create( PostId::fromString('00000000-0000-0000-0000-000000000000') ); foreach ($post->releaseEvents() as $event) { $dispatcher->dispatch($event); }
Of course that is better to use DI and you can take
definitions from A50\EventDispatcher\EventDispatcherServiceProvider
class.
After that in your application you can easily inject Psr\EventDispatcher\EventDispatcherInterface
.
Also, you can configure listeners for events pass them into config:
use A50\EventDispatcher\EventDispatcherConfig; use YourProject\Domain\UserWasRegistered; use YourProject\Application\SendEmailToModerator; $config = EventDispatcherConfig::withDefaults() ->addEventListener(UserWasRegistered::class, SendEmailToModerator::class) ->listeners();
And even set priority (ListenerPriority::NORMAL
by default):
use A50\EventDispatcher\EventDispatcherConfig; use A50\EventDispatcher\ListenerPriority; use YourProject\Application\SendEmailToModerator; use YourProject\Domain\UserWasRegistered; $config = EventDispatcherConfig::withDefaults() ->addEventListener(UserWasRegistered::class, SendEmailToModerator::class, ListenerPriority::HIGH) ->listeners();
Testing
make test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.