abdulklarapl / eventdispatcher
简单轻量级的事件调度器。让我们使用abdulklarapl的组件构建自己的系统吧!
dev-master
2013-07-04 00:15 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 05:24:47 UTC
README
安装
- 通过git克隆:
git clone git@github.com:abdulklarapl/EventDispatcher.git
- 通过composer安装,将以下代码添加到您的composer.json的require部分:
"abdulklarapl/eventdispatcher": "dev-master"
关于
Abdulklarapl的EventDispatcher是一个简单轻量级的事件调度器。让我们使用abdulklarapl的组件构建自己的系统吧!
示例
首先,创建您的订阅者
<?php
namespace Acme\EventSubscriber;
use Abdulklarapl\Components\EventDispatcher\Subscriber\SubscriberInterface;
use Abdulklarapl\Components\EventDispatcher\Event\Event;
class SampleSubscriber implements SubscriberInterface
{
public function getSubscribedEvents()
{
return array(
"foo.bar" => 'fooAction'
);
}
/**
* method that it's called on 'event.foo'
*
* @param Event $event
*/
public function fooAction(Event $event)
{
// I just handled the event!
}
}
然后,创建EventDispatcher的新实例
$subscriber = new SampleSubscriber();
$dispatcher = new Dispatcher();
$dispatcher->addSubscriber($subscriber);
$dispatcher->fire('foo.bar');
停止传播
如果您不想在您的订阅调用之后传播事件,您可以修改事件
public function fooAction(Event $event)
{
$event->stopPropagation();
}