systream/event-dispatcher
Composer 安装命令:
composer require systream/event-dispatcher
包简介
Observer for events
README 文档
README
Observer for events
Usage Examples
Installation
You can install this package via packagist.org with composer.
composer require systream/event-dispatcher
composer.json:
"require": { "systream/event-dispatcher": "1.*" }
This library requires php 5.6 or higher, but also works on php 5.4.
Subscribe to event
$eventDispatcher = new EventDispatcher(); $subscription = new EventDispatcher\Subscription('nameOfEvent', function (EventInterface $event) { // do stuff here }); $eventDispatcher->subscribe($subscription);
Fire an event
$eventDispatcher = new EventDispatcher(); $eventDispatcher->emit(new EventDispatcher\Event('nameOfEvent'));
Stop Propagation
$eventDispatcher = new EventDispatcher(); $subscription = new EventDispatcher\Subscription('fooBar', function (EventDispatcher\EventInterface $event) { $event->stopPropagation(); }); $eventDispatcher->subscribe($subscription); $subscription2 = new EventDispatcher\Subscription('fooBar', function (EventDispatcher\EventInterface $event) { // do stuff here }); $eventDispatcher->subscribe($subscription2); $eventDispatcher->emit(new EventDispatcher\Event('fooBar'));
In this case the second subscription won't be invoked.
Custom events
The only thing you need to do is that implementing the EventInterface
class MyCustomEvent extends AbstractEvent implements EventInterface { /** * @var string */ protected $key = 'order.save'; /** * @var Order */ protected $order; /** * @param string $key */ public function __construct(Order $order) { $this->order = $order; } /** * @return Order */ public function getOrder() { return $this->order; } } ... $eventDispatcher = new EventDispatcher(); $subscription = new EventDispatcher\Subscription('nameOfEvent', function (OrderEvent $event) { $order = $orderEvent->getOrder(); // do stuff }); $eventDispatcher->subscribe($subscription);
Custom Subscriptions
The only thing you need to do is that implementing the SubscriptionInterface
Test
统计信息
- 总下载量: 728
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-16