slince/event-dispatcher
最新稳定版本:2.0.0
Composer 安装命令:
composer require slince/event-dispatcher
包简介
Event dispatcher package
README 文档
README
Installation
Install via composer.
$ composer require slince/event-dispatcher
Usage
Creates a event dispatcher
$dispatcher = new Slince\EventDispatcher\Dispatcher();
Adds a listener for the specified event
There are two types of listeners: callable and Slince\EventDispatcher\Listener
Slince\EventDispatcher\Listener
use Slince\EventDispatcher\ListenerInterface; class FooListener implements ListenerInterface { public function handle(Event $event) { //do something } } $dispatcher->addListener('foo-event-name', new FooListener());
callable
$dispatcher->addListener('foo-event-name', function(Event $event){ //do something });
Add a subscriber
use Slince\EventDispatcher\SubscriberInterface; class FooSubscriber implements SubscriberInterface { public static function getSubscribedEvents(Event $event) { return [ 'foo' => 'onFoo', 'bar' => 'onBar' ]; } public function onFoo(Event $event) { //do something } public function onBar(Event $event) { //do something } } $dispatcher->addSubscriber(new FooSubscriber());
Dispatches the event to the registered listeners
Just provides the event name.
$dispatcher->dispatch('foo-event-name');
You can also dispatch with an event instance.
$dispatcher->dispatch(new Event('foo-event-name'));
Propagation
You can call stopPropagation to stop event propagation on the event instance.
$dispatcher->addListener('foo-event-name', function(Event $event){ $event->stopPropagation(); }); $emitter->addListener('foo-event-name', function ($event) { // This will not be triggered }); $dispatcher->dispatch('foo-event-name');
Checks whether propagation is stopped
$event = new Event('foo-event-name'); $dispatcher->dispatch($event); $event->isPropagationStopped();
License
The MIT license. See MIT
统计信息
- 总下载量: 5.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-21