antidot-fw / event-dispatcher
Antidot 框架 PSR-14 事件分发器库
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 事件分发器。
安装
使用 Composer
composer require antidot-fw/event-dispatcher
使用 Laminas 配置聚合器
它会自动安装库
使用工厂
配置
<?php /** @var \Psr\Container\ContainerInterface $container */ $container->set('config', [ 'app-events' => [ 'event-listeners' => [ // SomeEvent::class => [ 'some.event' => [ SomeEventListener::class, SomeEventOtherListener::class, ] ] ] ]);
工厂
<?php use Antidot\Event\Container\EventDispatcherFactory; use Psr\EventDispatcher\EventDispatcherInterface; $factory = new EventDispatcherFactory(); $eventDispatcher = $factory->__invoke($container); $container->set(EventDispatcherInterface::class, $eventDispatcher);
异步事件分发器工厂
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);
用法
发送事件
<?php use Psr\EventDispatcher\EventDispatcherInterface; /** @var \Psr\Container\ContainerInterface $container */ $eventDispatcher = $container->get(EventDispatcherInterface::class); $eventDispatcher->dispatch(SomeEvent::occur());
异步模式发送事件
<?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()