gandung/event-dispatcher

一个简单的PHP事件调度库

v1.0.0 2017-09-05 21:33 UTC

This package is auto-updated.

Last update: 2024-09-19 18:01:48 UTC


README

Minimum PHP Version Build status SensioLabsInsight

这是一个简单的库,用于管理事件(例如,http-kernel事件,异常事件),它使用Symfony事件调度器类接口作为事件调度器对象,并使用Symfony事件订阅者类接口作为事件订阅者(或监听器集合)来处理当前事件调度器对象。

此事件调度器库使用几个设计模式来提高可扩展性和可维护性

  • 观察者模式用于在观察者(监听器)和可观察(事件)对象之间维护状态。
  • 中介者模式使得所有事物都可以真正地扩展。

目录

快速入门

EventDispatcher对象实例化

use Gandung\EventDispatcher\EventDispatcher;
use Gandung\EventDispatcher\EventContainer;

$dispatcher = new EventDispatcher(new EventContainer);

或者,您可以使用工厂来实现。

use Gandung\EventDispatcher\EventDispatcherFactory;

$factory = new EventDispatcherFactory;
$dispatcher = $factory->getDispatcher();

使用基于闭包的简单监听器解析事件

use Gandung\EventDispatcher\EventDispatcherFactory;

$factory = new EventDispatcherFactory;
$dispatcher = $factory->getDispatcher();
$listener = function() {
	echo "i'am a closure based listener.\n";
};

$dispatcher->attachListener('event.simple.closure', $listener, 20);
$dispatcher->dispatch('event.simple.closure');

使用基于对象的简单监听器解析事件

use Gandung\EventDispatcher\EventDispatcherFactory;

class Foo
{
	public function dummyResolver()
	{
		echo sprintf("Inside {%s}@{%s}", \spl_object_hash($this), __METHOD__);
	}
}

$foo = new Foo();
$factory = new EventDispatcherFactory;
$dispatcher = $factory->getDispatcher();
$dispatcher->attachListener('event.simple.object', [$foo, 'dummyResolver'], 20);
$dispatcher->dispatch('event.simple.object');

解析已订阅的事件

use Gandung\EventDispatcher\EventDispatcherFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class FooSubscriber implements EventSubscriberInterface
{
	public static function getSubscribedEvents()
	{
		return [
			'event.simple.prioritized' => [
				['dummyResolver1', 20],
				['dummyResolver2', 10],
				['dummyResolver3', -90]
			],
			'event.simple.unprioritized' => [
				'unprioritizedResolver'
			],
			'event.simple.single' => 'singleResolver'
		];
	}

	public function dummyResolver1()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function dummyResolver2()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function dummyResolver3()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function unprioritizedResolver()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}

	public function singleResolver()
	{
		echo sprintf("{%s@%s}\n", \spl_object_hash($this), __METHOD__);
	}
}

$subscriber = new FooSubscriber;
$factory = new EventDispatcherFactory();
$dispatcher->attachSubscriber($subscriber);
$dispatcher->dispatch('event.simple.prioritized');
$dispatcher->dispatch('event.simple.unprioritized');
$dispatcher->dispatch('event.simple.single');

API

EventDispatcherFactory

getDispatcher()

返回EventDispatcher对象实例。

EventDispatcher

attachListener($event, $listener, $priority = 0)

将监听器处理程序附加到指定事件。

detachListener($event, $listener)

从指定事件中删除监听器处理程序。

getListeners($event = null)

获取绑定到指定事件上的监听器。

hasListeners($event = null)

确定指定事件名称是否有监听器。

setListenerPriority($event, $listener, $priority)

设置事件监听器优先级。

getListenerPriority($event, $listener)

获取事件监听器优先级。

attachSubscriber(EventSubscriberInterface $subscriber)

注册事件订阅者。

detachSubscriber(EventSubscriberInterface $subscriber)

删除事件订阅者。

dispatch($event, Event $eventHandler = null)

调度指定的事件。