Zend Framework 2.0事件管理器(The EventManager)入門教程,zendeventmanager_PHP教程

來源:互聯網
上載者:User

Zend Framework 2.0事件管理器(The EventManager)入門教程,zendeventmanager


概述

EventManger是一個為以下使用方式設計的組件:
複製代碼 代碼如下:
實現簡單的主題/觀察者模式
實現面向切面的設計
實現事件驅動的架構

基本的架構允許你添加和解除指定事件的接聽程式,無論是在一個執行個體基礎還是一個共用的集合;觸發事件;終止接聽程式的執行。

快速入門

通常,你將會在一個類中建立一個EventManager。

複製代碼 代碼如下:
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執行個體,或使用一個新的執行個體重設它;如果不存在,它將會在被用到的時候惰性執行個體化。

EventManager僅僅對它是否觸發了一些事件感興趣。基礎的觸發接受三個參數:事件的名字,它通常是當前的函數/方法名;上下文,它通常是當前的對象的執行個體;和參數,它通常是提供給當前函數/方法的參數。
複製代碼 代碼如下:
class Foo
{
// ... assume events definition from above

public function bar($baz, $bat = null)
{
$params = compact('baz', 'bat');
$this->getEventManager()->trigger(__FUNCTION__, $this, $params);
}
}

按順序,觸發事件僅關心否有一些東西偵聽了事件。接聽程式添加到EventManager,指定一個指定的事件和要通知的回調。回調接受一個Event對象,它有一個用於擷取事件名字,上下文和參數的訪問器。讓我們添加一個接聽程式,並且觸發事件。

複製代碼 代碼如下:
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()的第二個參數是一個任何有效回調;例子中展示了一個匿名函數來保持例子是自包含的。然而,你同樣可以使用一個有效函數名字,一個函數對象,一個引用靜態方法的字串,或一個帶有一個指定靜態方法或執行個體方法的回調數組。再一次,任何PHP回調都是有效。

有時候你可能想要指定一個接聽程式沒有一個建立了一個EventManager的類的對象執行個體。Zend Framework通過一個SharedEventCollection的概念來實現它。簡單的說,你可以使用一個眾所周知的SharedEventCollection來注入一個獨立的EventManager執行個體,並且EventManager執行個體將會為附加的接聽程式來查詢它。添加到SharedEventCollection的接聽程式與正常的事件管理器大略相同;調用attach與EventManager完全相同,但是在開始需要一個附加的參數:一個指定的執行個體。還記得建立一個EventManager的執行個體,我們是如何傳遞給他__CLASS__的?在使用一個SharedEventCollection時,那個值,或者任何你提供給構造器的數組中的任何字串,可能用於識別一個執行個體。作為一個樣本,假設我們有一個SharedEventManager執行個體我們知道已經被注入到我們的EventManager執行個體中了(對於執行個體,通過依賴注入),我們可以更改上面的例子來通過共用集合來添加:
複製代碼 代碼如下:
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。這樣,你不需要擔心在哪或者如何來訪問SharedEventCollection;它通過簡單的調用StaticEventManager::getInstance()是全域可用的。

要知道,然而,架構不贊成它的使用,並且在2.0.0beta4中,你將通過配置一個SharedEventManager執行個體並注入到一個單獨的EventManager執行個體中來代替它。

萬用字元接聽程式

有時候你可能會想要為一個給定的執行個體的很多事件或全部事件添加相同的接聽程式,或者可能,使用一個共用事件集合,很多上下文,並且很多事件。EventManager組件允許這樣做。

一次添加多個事件
複製代碼 代碼如下:
$events = new EventManager();
$events->attach(array('these', 'are', 'event', 'names'), $callback);

通過萬用字元添加

複製代碼 代碼如下:
$events = new EventManager();
$events->attach('*', $callback);

注意如果你指定了一個優先順序,那個優先順序將會用於這個接聽程式觸發的任何事件。

上面的代碼指定的是任何時間觸發將會導致這個特定接聽程式的通知。

通過一個SharedEventManager一次添加多個事件
複製代碼 代碼如下:
$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);

注意如果你指定了一個優先順序,那個優先順序將會被用於所有指定的事件。

通過一個SharedEventManager一次添加所有事件
複製代碼 代碼如下:
$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);

注意如果你指定了一個優先順序,那個優先順序將會被用於所有指定的事件。

上面的代碼指定了上下文“foo”和“bar”,指定的接聽程式將會在任何事件觸發時被通知。

配置選項

EventManager選項

標識符

給定的EventManager執行個體可以回答的字串或字串數組,當通過一個SharedEventManager訪問時。

event_class

一個替代的Event類的名字用於代表傳給接聽程式的事件。

shared_collections

當觸發事件時的一個SharedEventCollection執行個體。

可用方法

__construct

__construct(null|string|int Sidentifier)

構造一個新的EventManager執行個體,使用給定的標識符,如果提供了的話,為了共用集合的目的。

setEventClass

setEventClass(string $class)

提供替換Event類的名字用在建立傳遞給觸發的接聽程式的事件時。

setSharedCollections

setSharedCollections(SharedEventCollection $collections=null)

用於觸發事件時的SharedEventCollection執行個體。

getSharedCollections

getSharedCollections()

返回當前添加到的SharedEventCollection執行個體。如果沒有添加集合,返回空,或者一個SharedEventCollection執行個體。

trigger

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

觸發指定事件的所有接聽程式。推薦為$event使用當前的函數/方法名,在後面加上諸如“.pre”、“.post”等,如果有需要的話。$context應該是當前對象的執行個體,或者是函數的名字如果不是使用對象觸發。$params通常應該是一個關聯陣列或者ArrayAccess執行個體;我們推薦使用傳遞給函數/方法的參數(compact()在這裡通常很有用處)。這個方法同樣可以接受一個回調並且表現與triggerUntil()相同。

方法返回一個ResponseCollection的執行個體,它可以用於反省各種各樣的接聽程式返回的值,測試短路,以及更多。

triggerUntil

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

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

attach

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

添加$callback到EventManager執行個體,偵聽事件$event。如果提供了一個$priority,接聽程式將會使用那個優先順序插入到內部的接聽程式堆棧;高的值會先執行。(預設的優先順序是“1”,並且運行使用負的值。)

方法返回一個Zend\Stdlib\CallbackHandler的執行個體;這個值可以在稍後傳遞給detach(),如果需要的話。

attachAggregate

attachAggregate(string|ListenerAggregate $aggregate)

如果一個字串被傳遞作為$aggregate,執行個體化那個類。$aggregate然後被傳遞給EventManager執行個體的attache()方法因此他可以註冊接聽程式。

返回ListenerAggregate執行個體。

detach

detach(CallbackHandler $listener)

掃描所有的接聽程式,並且取消匹配$listener的所有接聽程式因此它們將不再會被觸發。

返回一個true布爾值如果任何接聽程式已經被指定並且取消訂閱,否則返回一個false布爾值。

detachAggregate

detachAggregate(ListenerAggregate $aggregate)

迴圈所有的事件來確定集合代表的接聽程式;對於所有的匹配項,接聽程式將會被移除。

如果任何接聽程式被確定並被取消訂閱返回一個true布爾值,否則返回一個false布爾值。

getEvents

getEvent()

返回一個有接聽程式附加的所有事件名字的數組。

getListeners

getListeners(string $event)

返回一個添加到$event的所有接聽程式的Zend\Stdlib\PriorityQueue執行個體

clearListeners

clearListeners(string $event)

移除添加到$event的所有接聽程式。

prepareArgs

prepareArgs(array $args)

從提供的$args建立一個ArrayObject。如果你想要你的接聽程式可以更改參數讓稍後的接聽程式或觸發的方法可以看到這些更改的情況下著將很有用。


zend framework 2 入門學習及交流

借你的寶地跟一個回答,zend framework 2 交流學習,來這裡吧,人稍微多點。群號213966183
 

對於zend framework 20 的module設定的問題

知道就好
 

http://www.bkjia.com/PHPjc/861791.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/861791.htmlTechArticleZend Framework 2.0事件管理器(The EventManager)入門教程,zendeventmanager 概述 EventManger是一個為以下使用方式設計的組件: 複製代碼 代碼如下:...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.