ZF3-附加到EventManager的事件不会触发

时间:2019-05-17 18:26:28

标签: events zend-framework event-handling zend-framework3

我有一个EntityRepository类,我想将一个事件附加到其“保存”方法。这是我设置事件管理器标识符的AbstractRepository类:

abstract class AbstractRepository extends EntityRepository implements RepositoryInterface
{
    /**
     * @var EventManagerInterface
     */
    protected $events;

    /**
     * Set the event manager instance used by this context.
     *
     * @param EventManagerInterface $events
     *
     * @return mixed
     */
    public function setEventManager(EventManagerInterface $events)
    {
        $events->setIdentifiers([
            __CLASS__,
            get_class($this),
        ]);

        $this->events = $events;

        return $this;
    }

    /**
     * Retrieve the event manager.
     *
     * Lazy-loads an EventManager instance if none registered.
     *
     * @return EventManagerInterface
     */
    public function getEventManager()
    {
        if (!$this->events) {
            $this->setEventManager(new EventManager());
        }

        return $this->events;
    }
}

和名为DocumentRepository的存储库类:

class DocumentsRepository extends AbstractRepository
{
    public function save(Documents $entity)
    {
        $this->getEventManager()->trigger(RepositoryInterface::EVENT_BEFORE_SAVE);

        $this->getEntityManager()->persist($entity);
        $this->getEntityManager()->flush($entity);

        $this->getEventManager()->trigger(RepositoryInterface::EVENT_AFTER_SAVE);

        return $entity;
    }
}

还有一个用于定义事件名称的接口:

interface RepositoryInterface extends EventManagerAwareInterface
{
    const EVENT_BEFORE_SAVE   = 'before.save';
    const EVENT_AFTER_SAVE    = 'after.save';
}

要注册侦听器,我将以下代码添加到module.php。

public function onBootstrap(MvcEvent $event)
{
    $application    = $event->getApplication();
    $eventManager   = $application->getEventManager();

    /* Register event listener(s) */
    (new DocumentsRepositoryListener($serviceManager))->attach($eventManager);
}

最后,添加了侦听器类:

class DocumentsRepositoryListener extends AbstractListener implements ListenerAggregateInterface
{
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $sharedEvents = $events->getSharedManager();

        $this->listeners[] = $sharedEvents
            ->attach(DocumentsRepository::class, RepositoryInterface::EVENT_BEFORE_SAVE, [$this, 'beforeSave']);
        $this->listeners[] = $sharedEvents
            ->attach(DocumentsRepository::class, RepositoryInterface::EVENT_AFTER_SAVE, [$this, 'afterSave']);
    }

    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    public function beforeSave(EventInterface $event)
    {
        /* Something to do before saving */
    }

    public function afterSave(EventInterface $event)
    {
        /* Something to do after saving */
    }
}

AbstractListener类还包含一个用于注入serviceLocator和setter / getter方法的构造函数。

现在,我不知道为什么触发器不起作用。还有其他可能错过的地方吗?

1 个答案:

答案 0 :(得分:3)

您已为DocumentsRepository中的事件 trigger 编写的代码没有问题。但是,您对使用事件管理器有误解。特别是您如何附加事件监听器。

首先要说明的是,给定存储库类是EventManager own 实例; this 是您应将事件侦听器附加到的实例。 Module::onBootstrap()中的代码正在向 application 事件管理器注册,这是错误的实例。应用程序事件管理器处理应用程序事件,因此它将永远不会触发您的自定义事件。

/* Register event listener(s) */
(new DocumentsRepositoryListener($serviceManager))->attach($eventManager);

将替换为:

$documentRepositoryListener->attach($repository->getEventManager());

注册侦听器的逻辑可能更适合RepositoryFactory,例如:

class RepositoryFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {       
        $repository = new Repository();

        if ($repository instanceof EventManagerAwareInterface) {
            $eventManager = $repository->getEventManager();
            $listener = $container->get('Foo/Event/Listener');

            $listener->attach($eventManager);
        }

        return $repository;
    }
}

最后,您还需要删除$sharedEvents中的DocumentsRepositoryListener::attach()用法;您想将事件侦听器附加到存储库事件管理器,这可以是$events参数。