antidot-fw / event-dispatcher
Antidot Framework PSR-14 event dispatcher library
Fund package maintenance!
kpicaza
2.1.0
2021-12-06 12:10 UTC
Requires
- php: ^7.4|^8.0
- psr/event-dispatcher: ^1.0
Requires (Dev)
- infection/infection: ^0.21.0
- phpro/grumphp: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.0 || ^9.0
- react/event-loop: ^1.2
- squizlabs/php_codesniffer: ^3.4
- symfony/var-dumper: ^4.2 || ^5.0
- vimeo/psalm: ^4.4
Suggests
- react/event-loop: If you want to run Async Event Dispatcher implementation.
README
Psr 14 Event dispatcher implementation.
Installation
Using composer
composer require antidot-fw/event-dispatcher
Using Laminas config Aggregator
it install the library automatically
Using factory:
Config
<?php /** @var \Psr\Container\ContainerInterface $container */ $container->set('config', [ 'app-events' => [ 'event-listeners' => [ // SomeEvent::class => [ 'some.event' => [ SomeEventListener::class, SomeEventOtherListener::class, ] ] ] ]);
factory
<?php use Antidot\Event\Container\EventDispatcherFactory; use Psr\EventDispatcher\EventDispatcherInterface; $factory = new EventDispatcherFactory(); $eventDispatcher = $factory->__invoke($container); $container->set(EventDispatcherInterface::class, $eventDispatcher);
Async Event Dispatcher Factory
composer require react/event-loop
<?php use Antidot\Event\Container\AsyncEventDispatcherFactory; use Psr\EventDispatcher\EventDispatcherInterface; $factory = new AsyncEventDispatcherFactory(); $eventDispatcher = $factory->__invoke($container); $container->set(EventDispatcherInterface::class, $eventDispatcher);
Usage
Send events
<?php use Psr\EventDispatcher\EventDispatcherInterface; /** @var \Psr\Container\ContainerInterface $container */ $eventDispatcher = $container->get(EventDispatcherInterface::class); $eventDispatcher->dispatch(SomeEvent::occur());
Send events Async mode
<?php use Psr\EventDispatcher\EventDispatcherInterface; use React\EventLoop\Loop; /** @var \Psr\Container\ContainerInterface $container */ $eventDispatcher = $container->get(EventDispatcherInterface::class); $eventDispatcher->dispatch(SomeEvent::occur()); Loop::run()