zorachka / event-dispatcher
Event Dispatcher
Requires
- php: ^8.1
- psr/event-dispatcher: ^1.0
- webmozart/assert: ^1.11
- zorachka/container: ^3.1
README
Zorachka 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 zorachka/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 Zorachka\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 Zorachka\EventDispatcher\ImmutablePrioritizedListenerProvider; use Zorachka\EventDispatcher\ListenerPriority; use Zorachka\EventDispatcher\PrioritizedListenerProvider; use Zorachka\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 Zorachka\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 Zorachka\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 Zorachka\EventDispatcher\EventDispatcherConfig; use Zorachka\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.