如何在ZF2中触发附加到共享事件管理器的事件? [英] How to trigger an event attached to the Shared Event Manager in ZF2?

查看:104
本文介绍了如何在ZF2中触发附加到共享事件管理器的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附加到(本地)事件管理器的事件

An event attached to a (local) event manager

public class myClass implements EventManagerAwareInterface {
    ...
    public function __construct(...) {
        ...
        $this->getEventManager()->attach('doEvent', function ($e) {
            $event = $e->getName();
            $params = $e->getParams();
            printf(
                'Handled event "%s", with parameters %s',
                $event,
                json_encode($params)
            );
        });
    }
    ...
    public function getEventManager() {
        if (!$this->eventManager) {
            $this->setEventManager(new EventManager());
        }
        return $this->eventManager;
    }
    public function setEventManager(EventManagerInterface $eventManager) {
        $eventManager->setIdentifiers(array(
            __CLASS__,
            get_class($this),
            'exampleIdentifier',
        ));
        $this->eventManager = $eventManager;
        return $this;
    }
}

可以在本地触发,如下所示:

can be triggered locally as follows:

public class myClass {
    ...
    public function doWhatEver(...) {
        $this->getEventManager()->trigger('doEvent', null, ['foo' => 'bar', 'baz' => 'bat']);
    }
    ...
}

如果我正确理解了共享事件管理器,则它是绑定到identifiers的事件和侦听器的中央寄存器.每个(本地)事件管理器都知道它. (顺便说一句:ZF2魔术如何工作?)每个事件管理器都可以访问1.在其中注册的侦听器,以及2.访问共享事件管理器的池".将事件附加到该池类似于将事件附加到事件管理器,但是需要附加参数-identifier(共享池"中侦听器捆绑包"的键):

If I understand the Shared Event Manager correctly, it's a central register of events and listeners, bundled to identifiers. It is known by every (local) event manager. (Btw.: How does this ZF2 magic work?) Every event manager has access 1. to the listeners registered in it and 2. to the shared event manager's "pool". Attaching an event to this pool is similar to the common event attaching to an event manager, but requires an additional parameter -- identifier (the key of the "listeners bundle" in the shared "pool"):

this->getEventManager()->getSharedManager()->attach('exampleIdentifier', 'doEvent', function ($e) {
    ...
});

来自同一类的触发完全相同:

The triggering from the same class is exactly identical:

$this->getEventManager()->trigger('doEvent', null, ['foo' => 'bar', 'baz' => 'bat']);

现在,我想将触发器调用移至另一个类.但是我不明白如何传递正确的identifier. 如何从具有identifier/identifier设置的ServiceManager的类中触发附加到共享事件管理器的事件?

Now I want to move the trigger-call to another class. But I don't understand, how to pass the correct identifier. How to trigger an event attached to the Shared Event Manager from a class, that has a ServiceManager with a different identifier / identifiers set?

推荐答案

如何在ZF2中触发附加到共享事件管理器的事件?

How to trigger an event attached to the Shared Event Manager in ZF2?

尽管名称很大,但是SharedEventManager是 不是事件管理器;因此它不能触发"事件.

Despite the name, the SharedEventManager is not an event manager; therefore it cannot 'trigger' events.

这个想法是,您可以将事件侦听器附加到共享事件管理器,该事件管理器通常会附加到服务(事件管理器可以识别).

The idea is that you can attach event listener(s) to the shared event manager that would normally be attached to a service (which is event manager aware).

为什么要打扰?

共享事件管理器具有以下优点.

The shared event manager provides the following benefits.

  • 新的事件侦听器可以附加到可能不存在的事件管理器上

  • The new event listeners can be attached to event managers that may or may not exist yet

允许使用标识符"将侦听器附加到可能来自不同来源的一组事件上.

Allows listeners to be attached to a set of events that may come from different sources, by using 'identifiers'.

第二点很重要.通过使用事件管理器标识符",您可以将事件侦听器附加到多个事件管理器/服务.

The second point is the important one. By using a event manager 'identifier' you are able to attach event listeners to more than one event manager/service.

请考虑以下内容

use Zend\EventManager\EventManagerAwareTrait;
use Zend\EventManager\EventManagerAwareInterface;

abstract class AbstractService implements EventManagerAwareInterface
{
    use EventManagerAwareTrait;

    public function __construct()
    {
        $this->getEventManager()->addIdentifiers([
            'my_custom_identifier',
        ]);
    }

    public function doSomething()
    {
        $this->getEventManager()->trigger(__FUNCTION__, ['foo' => 'bar']);
    }
}

class ServiceA extends AbstractService
{}

class ServiceB extends AbstractService
{}

没有共享事件管理器,如果我们想将侦听器附加到'doSomething'事件,则需要为每个事件管理器执行此操作.

Without the shared event manager if we wanted to attach a listener to the 'doSomething' event we would need to do so for each event manager.

使用共享管理器,您可以在一个呼叫中同时定位两个目标,而无需创建serviceAserviceB或复制事件侦听器.

With the shared manager you can target both in one call, without needing to create serviceA or serviceB or duplicate the event listener.

use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\Mvc\MvcEvent;

class Module implements BootstrapListenerInterface
{
    public function onBootstrap(MvcEvent $event)
    {
        $application = $event->getApplication();
        $sharedEventManager = $application->getEventManager()->getSharedManager();

        $sharedEventManager->attach(
            'my_custom_identifier',
            'doSomething',
            function($event) {
                // some work
            }
        );
    }
}

这篇关于如何在ZF2中触发附加到共享事件管理器的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆