概述
EventManger是一個(gè)為以下使用情況設(shè)計(jì)的組件:
復(fù)制代碼 代碼如下:
實(shí)現(xiàn)簡(jiǎn)單的主題/觀察者模式
實(shí)現(xiàn)面向切面的設(shè)計(jì)
實(shí)現(xiàn)事件驅(qū)動(dòng)的架構(gòu)
基本的架構(gòu)允許你添加和解除指定事件的偵聽(tīng)器,無(wú)論是在一個(gè)實(shí)例基礎(chǔ)還是一個(gè)共享的集合;觸發(fā)事件;終止偵聽(tīng)器的執(zhí)行。
快速入門
通常,你將會(huì)在一個(gè)類中創(chuàng)建一個(gè)EventManager。
復(fù)制代碼 代碼如下:
use Zend/EventManager/EventManagerInterface;
use Zend/EventManager/EventManager;
use Zend/EventManager/EventManagerAwareInterface;
class Foo implements EventManagerAwareInterface
{
protected $events;
public function setEventManager(EventManagerInterface $events)
{
$events->setIdentifiers(array(
__CLASS__,
get_called_class(),
));
$this->events = $events;
return $this;
}
public function getEventManager()
{
if (null === $this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
}
上面的代碼允許用戶訪問(wèn)EventManager實(shí)例,或使用一個(gè)新的實(shí)例重置它;如果不存在,它將會(huì)在被用到的時(shí)候惰性實(shí)例化。
EventManager僅僅對(duì)它是否觸發(fā)了一些事件感興趣。基礎(chǔ)的觸發(fā)接受三個(gè)參數(shù):事件的名字,它通常是當(dāng)前的函數(shù)/方法名;上下文,它通常是當(dāng)前的對(duì)象的實(shí)例;和參數(shù),它通常是提供給當(dāng)前函數(shù)/方法的參數(shù)。
復(fù)制代碼 代碼如下:
class Foo
{
// ... assume events definition from above
public function bar($baz, $bat = null)
{
$params = compact('baz', 'bat');
$this->getEventManager()->trigger(__FUNCTION__, $this, $params);
}
}
按順序,觸發(fā)事件僅關(guān)心否有一些東西偵聽(tīng)了事件。偵聽(tīng)器添加到EventManager,指定一個(gè)指定的事件和要通知的回調(diào)。回調(diào)接受一個(gè)Event對(duì)象,它有一個(gè)用于獲取事件名字,上下文和參數(shù)的訪問(wèn)器。讓我們添加一個(gè)偵聽(tīng)器,并且觸發(fā)事件。
復(fù)制代碼 代碼如下:
use Zend/Log/Factory as LogFactory;
$log = LogFactory($someConfig);
$foo = new Foo();
$foo->getEventManager()->attach('bar', function ($e) use ($log) {
$event = $e->getName();
$target = get_class($e->getTarget());
$params = json_encode($e->getParams());
$log->info(sprintf(
'%s called on %s, using params %s',
$event,
$target,
$params
));
});
// Results in log message:
$foo->bar('baz', 'bat');
// reading: bar called on Foo, using params {"baz" : "baz", "bat" : "bat"}"
注意,attach()的第二個(gè)參數(shù)是一個(gè)任何有效的回調(diào);例子中展示了一個(gè)匿名函數(shù)來(lái)保持例子是自包含的。然而,你同樣可以使用一個(gè)有效的函數(shù)名字,一個(gè)函數(shù)對(duì)象,一個(gè)引用靜態(tài)方法的字符串,或一個(gè)帶有一個(gè)指定靜態(tài)方法或?qū)嵗椒ǖ幕卣{(diào)數(shù)組。再一次,任何php回調(diào)都是有效的。
有時(shí)候你可能想要指定一個(gè)偵聽(tīng)器沒(méi)有一個(gè)創(chuàng)建了一個(gè)EventManager的類的對(duì)象實(shí)例。Zend Framework通過(guò)一個(gè)SharedEventCollection的概念來(lái)實(shí)現(xiàn)它。簡(jiǎn)單的說(shuō),你可以使用一個(gè)眾所周知的SharedEventCollection來(lái)注入一個(gè)獨(dú)立的EventManager實(shí)例,并且EventManager實(shí)例將會(huì)為附加的偵聽(tīng)器來(lái)查詢它。添加到SharedEventCollection的偵聽(tīng)器與正常的事件管理器大略相同;調(diào)用attach與EventManager完全相同,但是在開(kāi)始需要一個(gè)附加的參數(shù):一個(gè)指定的實(shí)例。還記得創(chuàng)建一個(gè)EventManager的實(shí)例,我們是如何傳遞給他__CLASS__的?在使用一個(gè)SharedEventCollection時(shí),那個(gè)值,或者任何你提供給構(gòu)造器的數(shù)組中的任何字符串,可能用于識(shí)別一個(gè)實(shí)例。作為一個(gè)示例,假設(shè)我們有一個(gè)SharedEventManager實(shí)例我們知道已經(jīng)被注入到我們的EventManager實(shí)例中了(對(duì)于實(shí)例,通過(guò)依賴注入),我們可以更改上面的例子來(lái)通過(guò)共享集合來(lái)添加:
復(fù)制代碼 代碼如下:
use Zend/Log/Factory as LogFactory;
// Assume $events is a Zend/EventManager/SharedEventManager instance
$log = LogFactory($someConfig);
$events->attach('Foo', 'bar', function ($e) use ($log) {
$event = $e->getName();
$target = get_class($e->getTarget());
$params = json_encode($e->getParams());
$log->info(sprintf(
'%s called on %s, using params %s',
$event,
$target,
$params
));
});
// Later, instantiate Foo:
$foo = new Foo();
$foo->getEventManager()->setSharedEventCollection($events);
// And we can still trigger the above event:
$foo->bar('baz', 'bat');
// results in log message:
// bar called on Foo, using params {"baz" : "baz", "bat" : "bat"}"
注意:StaticEventManager
在2.0.0beta3中,你可以使用StaticEventManager單例作為一個(gè)SharedEventCollection。這樣,你不需要擔(dān)心在哪或者如何來(lái)訪問(wèn)SharedEventCollection;它通過(guò)簡(jiǎn)單的調(diào)用StaticEventManager::getInstance()是全局可用的。
要知道,然而,框架不贊成它的使用,并且在2.0.0beta4中,你將通過(guò)配置一個(gè)SharedEventManager實(shí)例并注入到一個(gè)單獨(dú)的EventManager實(shí)例中來(lái)代替它。
通配符偵聽(tīng)器
有時(shí)候你可能會(huì)想要為一個(gè)給定的實(shí)例的很多事件或全部事件添加相同的偵聽(tīng)器,或者可能,使用一個(gè)共享事件集合,很多上下文,并且很多事件。EventManager組件允許這樣做。
一次添加多個(gè)事件
復(fù)制代碼 代碼如下:
$events = new EventManager();
$events->attach(array('these', 'are', 'event', 'names'), $callback);
通過(guò)通配符添加
復(fù)制代碼 代碼如下:
$events = new EventManager();
$events->attach('*', $callback);
注意如果你指定了一個(gè)優(yōu)先級(jí),那個(gè)優(yōu)先級(jí)將會(huì)用于這個(gè)偵聽(tīng)器觸發(fā)的任何事件。
上面的代碼指定的是任何時(shí)間觸發(fā)將會(huì)導(dǎo)致這個(gè)特定偵聽(tīng)器的通知。
通過(guò)一個(gè)SharedEventManager一次添加多個(gè)事件
復(fù)制代碼 代碼如下:
$events = new SharedEventManager();
// Attach to many events on the context "foo"
$events->attach('foo', array('these', 'are', 'event', 'names'), $callback);
// Attach to many events on the contexts "foo" and "bar"
$events->attach(array('foo', 'bar'), array('these', 'are', 'event', 'names'), $callback);
注意如果你指定了一個(gè)優(yōu)先級(jí),那個(gè)優(yōu)先級(jí)將會(huì)被用于所有指定的事件。
通過(guò)一個(gè)SharedEventManager一次添加所有事件
復(fù)制代碼 代碼如下:
$events = new SharedEventManager();
// Attach to all events on the context "foo"
$events->attach('foo', '*', $callback);
// Attach to all events on the contexts "foo" and "bar"
$events->attach(array('foo', 'bar'), '*', $callback);
注意如果你指定了一個(gè)優(yōu)先級(jí),那個(gè)優(yōu)先級(jí)將會(huì)被用于所有指定的事件。
上面的代碼指定了上下文“foo”和“bar”,指定的偵聽(tīng)器將會(huì)在任何事件觸發(fā)時(shí)被通知。
配置選項(xiàng)
EventManager選項(xiàng)
標(biāo)識(shí)符
給定的EventManager實(shí)例可以回答的字符串或字符串?dāng)?shù)組,當(dāng)通過(guò)一個(gè)SharedEventManager訪問(wèn)時(shí)。
event_class
一個(gè)替代的Event類的名字用于代表傳給偵聽(tīng)器的事件。
shared_collections
當(dāng)觸發(fā)事件時(shí)的一個(gè)SharedEventCollection實(shí)例。
可用方法
__construct
__construct(null|string|int Sidentifier)
構(gòu)造一個(gè)新的EventManager實(shí)例,使用給定的標(biāo)識(shí)符,如果提供了的話,為了共享集合的目的。
setEventClass
setEventClass(string $class)
提供替換Event類的名字用在創(chuàng)建傳遞給觸發(fā)的偵聽(tīng)器的事件時(shí)。
setSharedCollections
setSharedCollections(SharedEventCollection $collections=null)
用于觸發(fā)事件時(shí)的SharedEventCollection實(shí)例。
getSharedCollections
getSharedCollections()
返回當(dāng)前添加到的SharedEventCollection實(shí)例。如果沒(méi)有添加集合,返回空,或者一個(gè)SharedEventCollection實(shí)例。
trigger
trigger(string $event, mixed $target, mixed $argv, callback $callback)
觸發(fā)指定事件的所有偵聽(tīng)器。推薦為$event使用當(dāng)前的函數(shù)/方法名,在后面加上諸如“.pre”、“.post”等,如果有需要的話。$context應(yīng)該是當(dāng)前對(duì)象的實(shí)例,或者是函數(shù)的名字如果不是使用對(duì)象觸發(fā)。$params通常應(yīng)該是一個(gè)關(guān)聯(lián)數(shù)組或者ArrayAccess實(shí)例;我們推薦使用傳遞給函數(shù)/方法的參數(shù)(compact()在這里通常很有用處)。這個(gè)方法同樣可以接受一個(gè)回調(diào)并且表現(xiàn)與triggerUntil()相同。
方法返回一個(gè)ResponseCollection的實(shí)例,它可以用于反省各種各樣的偵聽(tīng)器返回的值,測(cè)試短路,以及更多。
triggerUntil
triggerUntil(string $event, mixed $context, mixed $argv, callback $callback)
觸發(fā)指定事件的所有偵聽(tīng)器,就像trigger(),額外的是它將每個(gè)偵聽(tīng)器的返回值傳遞給$callback;如果$callback返回一個(gè)布爾true值,偵聽(tīng)器的執(zhí)行將被終止。你可以使用$result->stopped()來(lái)測(cè)試它。
attach
attach(string $event, callback $callback, int $priority)
添加$callback到EventManager實(shí)例,偵聽(tīng)事件$event。如果提供了一個(gè)$priority,偵聽(tīng)器將會(huì)使用那個(gè)優(yōu)先級(jí)插入到內(nèi)部的偵聽(tīng)器堆棧;高的值會(huì)先執(zhí)行。(默認(rèn)的優(yōu)先級(jí)是“1”,并且運(yùn)行使用負(fù)的值。)
方法返回一個(gè)Zend/Stdlib/CallbackHandler的實(shí)例;這個(gè)值可以在稍后傳遞給detach(),如果需要的話。
attachAggregate
attachAggregate(string|ListenerAggregate $aggregate)
如果一個(gè)字符串被傳遞作為$aggregate,實(shí)例化那個(gè)類。$aggregate然后被傳遞給EventManager實(shí)例的attache()方法因此他可以注冊(cè)偵聽(tīng)器。
返回ListenerAggregate實(shí)例。
detach
detach(CallbackHandler $listener)
掃描所有的偵聽(tīng)器,并且取消匹配$listener的所有偵聽(tīng)器因此它們將不再會(huì)被觸發(fā)。
返回一個(gè)true布爾值如果任何偵聽(tīng)器已經(jīng)被指定并且取消訂閱,否則返回一個(gè)false布爾值。
detachAggregate
detachAggregate(ListenerAggregate $aggregate)
循環(huán)所有的事件來(lái)確定集合代表的偵聽(tīng)器;對(duì)于所有的匹配項(xiàng),偵聽(tīng)器將會(huì)被移除。
如果任何偵聽(tīng)器被確定并被取消訂閱返回一個(gè)true布爾值,否則返回一個(gè)false布爾值。
getEvents
getEvent()
返回一個(gè)有偵聽(tīng)器附加的所有事件名字的數(shù)組。
getListeners
getListeners(string $event)
返回一個(gè)添加到$event的所有偵聽(tīng)器的Zend/Stdlib/PriorityQueue實(shí)例
clearListeners
clearListeners(string $event)
移除添加到$event的所有偵聽(tīng)器。
prepareArgs
prepareArgs(array $args)
從提供的$args創(chuàng)建一個(gè)ArrayObject。如果你想要你的偵聽(tīng)器可以更改參數(shù)讓稍后的偵聽(tīng)器或觸發(fā)的方法可以看到這些更改的情況下著將很有用。
php技術(shù):Zend Framework 2.0事件管理器(The EventManager)入門教程,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。