Zend SharedEventManager 附加自定义目标

Zend SharedEventManager attaching custom target

我正在玩 SharedEventManager 这是代码的工作部分:

public function init(ModuleManager $moduleManager) {
        echo("<H1>LISTENER</H1>");

        $eventManager = $moduleManager->getEventManager(); 
        $sharedEventManager=$eventManager->getSharedManager();

        // attach a listener        
        $sharedEventManager->attach('*','do', function ($e) {
         echo("<H4>Message receveid</H4>");   
         echo("<p>name={$e->getName()}<p>");
         echo("<p>Target {$e->getTarget()}</p>");
         echo("<p>param1={$e->getParams()['param1']}</p>");
        });

        // sending event
        //  set params        
        $params = ['param1' => 'value of param1'];
        //  way 1 : works...
        $eventManager->trigger('do', 'CUSTOM_IDENTIFIER', $params);
        //  way 2 : works too
        $event1=new  \Zend\EventManager\Event('do', 'CUSTOM_IDENTIFIER', $params);
        $eventManager->triggerEvent($event1);
    }

并且输出正常:

LISTENER
Message receveid
name=do
Target CUSTOM_IDENTIFIER
param1=value of param1
Message receveid
name=do
Target CUSTOM_IDENTIFIER
param1=value of param1

所以... $sharedEventManager->attach('*','do', function ($e) ... 附加所有目标 *,用于 'do' 事件名称 它有效...

A)

 $sharedEventManager->attach('CUSTOM_IDENTIFIER','do', function ($e) ...

没用。 ...我的意思是:没有输出,消息没有被处理, 为什么 ??

B)

 $sharedEventManager->attach('CUSTOM_IDENTIFIER','*', function ($e) ...

没用。 ...我的意思是:没有输出,消息没有被处理, 为什么 ??

问题:可以附加事件,但仅限于 CUSTOM_IDENTIFIER 目标而不使用 * ?

查看您的代码,您混合了 EventtargetSharedEventManageridentifiers

您的代码的正确版本和正确的名称是:

public function init(ModuleManager $moduleManager) {
    echo("<H1>LISTENER</H1>");

    $eventManager = $moduleManager->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();

    // attach a listener        
    $identifier = '*'; // Use all identifiers
    $eventName = 'do';
    $sharedEventManager->attach($identifier, $eventName, function ($e) {
        echo '<p>Event received, <b>name:</b> "'.$e->getName().'", <b>Target:</b> '.$e->getTarget().'</p>';
    });

    // Sending event
    $eventManager->trigger('do', 'CUSTOM_TARGET');
    $eventManager->triggerEvent(new \Zend\EventManager\Event('do', 'CUSTOM_TARGET_NEW_EVENT'));

}

结果如下:

LISTENER
Event received, name: "do", Target: CUSTOM_TARGET
Event received, name: "do", Target: CUSTOM_TARGET_NEW_EVENT

您必须将标识符设置为 EventManager,否则您的 CUSTOM_IDENTIFIER 将不会在事件触发时被使用。

public function init(ModuleManager $moduleManager) {
    echo("<H1>LISTENER</H1>");

    $eventManager = $moduleManager->getEventManager();
    $sharedEventManager = $eventManager->getSharedManager();

    // Attach listener to all identifiers       
    $identifier = '*'; // Use all identifiers
    $eventName = 'do';
    $sharedEventManager->attach($identifier, $eventName, function ($e) {
        echo '<p>ALL identifier, Event received, <b>name:</b> "'.$e->getName().'", <b>Target:</b> '.$e->getTarget().'</p>';
    });       
    // Attach listener to custom identifier
    $customIdentifier = 'MY_CUSTOM_IDENTIFIER'; // Use all identifiers
    $sharedEventManager->attach($customIdentifier, $eventName, function ($e) {
        echo '<p>Custom identifier, Event received, <b>name:</b> "'.$e->getName().'", <b>Target:</b> '.$e->getTarget().'</p>';
    });

    // Sending event
    $eventManager->trigger('do', 'CUSTOM_TARGET');
    $eventManager->triggerEvent(new \Zend\EventManager\Event('do', 'CUSTOM_TARGET_NEW_EVENT'));

    $eventManager->addIdentifiers(['MY_CUSTOM_IDENTIFIER']);
    $eventManager->trigger('do', 'CUSTOM_TARGET_CUSTOM_IDENTIFIER');
    $eventManager->triggerEvent(new \Zend\EventManager\Event('do', 'CUSTOM_TARGET_CUSTOM_IDENTIFIER_NEW_EVENT'));

}

结果是:

LISTENER
ALL identifier, Event received, name: "do", Target: CUSTOM_TARGET
ALL identifier, Event received, name: "do", Target: CUSTOM_TARGET_NEW_EVENT
Custom identifier, Event received, name: "do", Target: CUSTOM_TARGET_CUSTOM_IDENTIFIER
ALL identifier, Event received, name: "do", Target: CUSTOM_TARGET_CUSTOM_IDENTIFIER
Custom identifier, Event received, name: "do", Target: CUSTOM_TARGET_CUSTOM_IDENTIFIER_NEW_EVENT
ALL identifier, Event received, name: "do", Target: CUSTOM_TARGET_CUSTOM_IDENTIFIER_NEW_EVENT

如您所见,标识符 * 捕获所有事件(因此,ALL identifier 的 4 行),而自定义标识符只有 2 行。

显然,最好使用更好(更易于理解)的自定义标识符,例如 class 名称或接口。

希望对您有所帮助