messy-one / mediator
简单的事件中介,用于事件委托。
1.3.0
2016-01-02 09:38 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- phpunit/phpunit: @stable
README
中介
简单的PHP事件中介。如果需要一个集中处理事件的地方,可以使用它。
如何使用
常规用法
// create an instance
$mediator = new Mediator();
// create an class implementing the EventData interface
class ConcreteEventData implements EventData
{
/** @var string */
private $foo;
/**
* @param string $foo
*/
public function __construct($foo)
{
$this->foo = $foo;
}
/**
* @return string
*/
public function getFoo()
{
return $this->foo;
}
}
// attach an event
$mediator->attach('unique:event', function ($event, ConcreteEventData $data) {
// do whatever you have to do with $data->getFoo()
});
// somewhere else in the code you can trigger the event and send the data to the callback function
$mediator->trigger('unique:event', new ConcreteEventData('foo'));
附加信息
由于事件存储在静态变量中,您可能需要删除它们(例如,用于单元测试)。
Mediator::reset();
待办事项
- 添加分组监听:监听 "main_event:*"。