溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Zend Framework 2.0事件管理器

發(fā)布時間:2021-09-29 11:18:44 來源:億速云 閱讀:105 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“如何使用Zend Framework 2.0事件管理器”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用Zend Framework 2.0事件管理器”吧!

概述

EventManger是一個為以下使用情況設(shè)計(jì)的組件:

復(fù)制代碼 代碼如下:


實(shí)現(xiàn)簡單的主題/觀察者模式
實(shí)現(xiàn)面向切面的設(shè)計(jì)
實(shí)現(xiàn)事件驅(qū)動的架構(gòu)


基本的架構(gòu)允許你添加和解除指定事件的偵聽器,無論是在一個實(shí)例基礎(chǔ)還是一個共享的集合;觸發(fā)事件;終止偵聽器的執(zhí)行。

快速入門

通常,你將會在一個類中創(chuàng)建一個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;
    }
}

上面的代碼允許用戶訪問EventManager實(shí)例,或使用一個新的實(shí)例重置它;如果不存在,它將會在被用到的時候惰性實(shí)例化。

EventManager僅僅對它是否觸發(fā)了一些事件感興趣?;A(chǔ)的觸發(fā)接受三個參數(shù):事件的名字,它通常是當(dāng)前的函數(shù)/方法名;上下文,它通常是當(dāng)前的對象的實(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)心否有一些東西偵聽了事件。偵聽器添加到EventManager,指定一個指定的事件和要通知的回調(diào)?;卣{(diào)接受一個Event對象,它有一個用于獲取事件名字,上下文和參數(shù)的訪問器。讓我們添加一個偵聽器,并且觸發(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()的第二個參數(shù)是一個任何有效的回調(diào);例子中展示了一個匿名函數(shù)來保持例子是自包含的。然而,你同樣可以使用一個有效的函數(shù)名字,一個函數(shù)對象,一個引用靜態(tài)方法的字符串,或一個帶有一個指定靜態(tài)方法或?qū)嵗椒ǖ幕卣{(diào)數(shù)組。再一次,任何PHP回調(diào)都是有效的。

有時候你可能想要指定一個偵聽器沒有一個創(chuàng)建了一個EventManager的類的對象實(shí)例。Zend Framework通過一個SharedEventCollection的概念來實(shí)現(xiàn)它。簡單的說,你可以使用一個眾所周知的SharedEventCollection來注入一個獨(dú)立的EventManager實(shí)例,并且EventManager實(shí)例將會為附加的偵聽器來查詢它。添加到SharedEventCollection的偵聽器與正常的事件管理器大略相同;調(diào)用attach與EventManager完全相同,但是在開始需要一個附加的參數(shù):一個指定的實(shí)例。還記得創(chuàng)建一個EventManager的實(shí)例,我們是如何傳遞給他__CLASS__的?在使用一個SharedEventCollection時,那個值,或者任何你提供給構(gòu)造器的數(shù)組中的任何字符串,可能用于識別一個實(shí)例。作為一個示例,假設(shè)我們有一個SharedEventManager實(shí)例我們知道已經(jīng)被注入到我們的EventManager實(shí)例中了(對于實(shí)例,通過依賴注入),我們可以更改上面的例子來通過共享集合來添加:

復(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單例作為一個SharedEventCollection。這樣,你不需要擔(dān)心在哪或者如何來訪問SharedEventCollection;它通過簡單的調(diào)用StaticEventManager::getInstance()是全局可用的。

要知道,然而,框架不贊成它的使用,并且在2.0.0beta4中,你將通過配置一個SharedEventManager實(shí)例并注入到一個單獨(dú)的EventManager實(shí)例中來代替它。

通配符偵聽器

有時候你可能會想要為一個給定的實(shí)例的很多事件或全部事件添加相同的偵聽器,或者可能,使用一個共享事件集合,很多上下文,并且很多事件。EventManager組件允許這樣做。

一次添加多個事件

復(fù)制代碼 代碼如下:


$events = new EventManager();
$events->attach(array('these', 'are', 'event', 'names'), $callback);

通過通配符添加

復(fù)制代碼 代碼如下:


$events = new EventManager();
$events->attach('*', $callback);

注意如果你指定了一個優(yōu)先級,那個優(yōu)先級將會用于這個偵聽器觸發(fā)的任何事件。

上面的代碼指定的是任何時間觸發(fā)將會導(dǎo)致這個特定偵聽器的通知。

通過一個SharedEventManager一次添加多個事件

復(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);

注意如果你指定了一個優(yōu)先級,那個優(yōu)先級將會被用于所有指定的事件。

通過一個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);

注意如果你指定了一個優(yōu)先級,那個優(yōu)先級將會被用于所有指定的事件。

上面的代碼指定了上下文“foo”和“bar”,指定的偵聽器將會在任何事件觸發(fā)時被通知。

配置選項(xiàng)

EventManager選項(xiàng)

標(biāo)識符

給定的EventManager實(shí)例可以回答的字符串或字符串?dāng)?shù)組,當(dāng)通過一個SharedEventManager訪問時。

event_class

一個替代的Event類的名字用于代表傳給偵聽器的事件。

shared_collections

當(dāng)觸發(fā)事件時的一個SharedEventCollection實(shí)例。

可用方法

__construct

__construct(null|string|int Sidentifier)

構(gòu)造一個新的EventManager實(shí)例,使用給定的標(biāo)識符,如果提供了的話,為了共享集合的目的。

setEventClass

setEventClass(string $class)

提供替換Event類的名字用在創(chuàng)建傳遞給觸發(fā)的偵聽器的事件時。

setSharedCollections

setSharedCollections(SharedEventCollection $collections=null)

用于觸發(fā)事件時的SharedEventCollection實(shí)例。

getSharedCollections

getSharedCollections()

返回當(dāng)前添加到的SharedEventCollection實(shí)例。如果沒有添加集合,返回空,或者一個SharedEventCollection實(shí)例。

trigger

trigger(string $event, mixed $target, mixed $argv, callback $callback)

觸發(fā)指定事件的所有偵聽器。推薦為$event使用當(dāng)前的函數(shù)/方法名,在后面加上諸如“.pre”、“.post”等,如果有需要的話。$context應(yīng)該是當(dāng)前對象的實(shí)例,或者是函數(shù)的名字如果不是使用對象觸發(fā)。$params通常應(yīng)該是一個關(guān)聯(lián)數(shù)組或者ArrayAccess實(shí)例;我們推薦使用傳遞給函數(shù)/方法的參數(shù)(compact()在這里通常很有用處)。這個方法同樣可以接受一個回調(diào)并且表現(xiàn)與triggerUntil()相同。

方法返回一個ResponseCollection的實(shí)例,它可以用于反省各種各樣的偵聽器返回的值,測試短路,以及更多。

triggerUntil

triggerUntil(string $event, mixed $context, mixed $argv, callback $callback)

觸發(fā)指定事件的所有偵聽器,就像trigger(),額外的是它將每個偵聽器的返回值傳遞給$callback;如果$callback返回一個布爾true值,偵聽器的執(zhí)行將被終止。你可以使用$result->stopped()來測試它。

attach

attach(string $event, callback $callback, int $priority)

添加$callback到EventManager實(shí)例,偵聽事件$event。如果提供了一個$priority,偵聽器將會使用那個優(yōu)先級插入到內(nèi)部的偵聽器堆棧;高的值會先執(zhí)行。(默認(rèn)的優(yōu)先級是“1”,并且運(yùn)行使用負(fù)的值。)

方法返回一個Zend\Stdlib\CallbackHandler的實(shí)例;這個值可以在稍后傳遞給detach(),如果需要的話。

attachAggregate

attachAggregate(string|ListenerAggregate $aggregate)

如果一個字符串被傳遞作為$aggregate,實(shí)例化那個類。$aggregate然后被傳遞給EventManager實(shí)例的attache()方法因此他可以注冊偵聽器。

返回ListenerAggregate實(shí)例。

detach

detach(CallbackHandler $listener)

掃描所有的偵聽器,并且取消匹配$listener的所有偵聽器因此它們將不再會被觸發(fā)。

返回一個true布爾值如果任何偵聽器已經(jīng)被指定并且取消訂閱,否則返回一個false布爾值。

detachAggregate

detachAggregate(ListenerAggregate $aggregate)

循環(huán)所有的事件來確定集合代表的偵聽器;對于所有的匹配項(xiàng),偵聽器將會被移除。

如果任何偵聽器被確定并被取消訂閱返回一個true布爾值,否則返回一個false布爾值。

getEvents

getEvent()

返回一個有偵聽器附加的所有事件名字的數(shù)組。

getListeners

getListeners(string $event)

返回一個添加到$event的所有偵聽器的Zend\Stdlib\PriorityQueue實(shí)例

clearListeners

clearListeners(string $event)

移除添加到$event的所有偵聽器。

prepareArgs

prepareArgs(array $args)

從提供的$args創(chuàng)建一個ArrayObject。如果你想要你的偵聽器可以更改參數(shù)讓稍后的偵聽器或觸發(fā)的方法可以看到這些更改的情況下著將很有用。

感謝各位的閱讀,以上就是“如何使用Zend Framework 2.0事件管理器”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何使用Zend Framework 2.0事件管理器這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI