ZF3:在EventManager中注入SharedEventManager

时间:2019-03-01 17:35:26

标签: zend-framework dependency-injection zend-framework3

在ZF2中,使用共享事件管理器在多个控制器之间共享事件

Module.php

use Zend\Mvc\MvcEvent;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function onBootstrap(MvcEvent $event)
    {
        $services = $event->getApplication()->getServiceManager();
        $sharedEventManager = $eventManager->getSharedManager();

        $sharedEventManager->attach('user', 'log-fail', function($event) use ($services) {
            $username = $event->getParam('username');
            $log = $services->get('log');
            $log->warn('Error logging user: ' . $username);
        });
    }
}

LogController.php

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManager;

class LogController extends AbstractActionController
{
    public function __construct()
    {
    }

    public function inAction()
    {
        //...
        if (! $isValid) {
            $event = new EventManager('user');
            $event->trigger('log-fail', $this, array('username'=> $username));
            //...
        }
    }
}

但是ZF3要求在实例化时注入共享事件管理器,而不是通过setter注入。 https://docs.zendframework.com/zend-eventmanager/migration/changed/

所以我创建了一个日志控制器工厂来注入共享事件管理器,但是找不到如何注入

LogControllerFactory.php

<?php

namespace Application\Controller;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class LogControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $sharedEventManager = ???

        return new LogController($sharedEventManager);
    }
}

您是否对如何获取实例化的共享事件管理器以将其注入到控制器中以便在控制器__construct函数中获取它有任何想法?

1 个答案:

答案 0 :(得分:3)

要回答我自己的问题:

1。 config.php

配置文件

/..
'controllers' => [
    'factories' => [
        Controller\LogController::class => Controller\LogControllerFactory::class,
        /..
    ],
    'aliases' => [
        'log' => Controller\LogController::class,
        /..
    ]
],
/..
'service_manager' => [
    'factories' => [
        'log' => Service\Log::class,
    ],
],
/..

2。 Module.php

模块文件

namespace Application;

use Zend\Mvc\MvcEvent;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function onBootstrap(MvcEvent $event)
    {
        // Get shared service manager
        $services = $event->getApplication()->getServiceManager();
        $sharedEventManager = $eventManager->getSharedManager();

        // Attach listener
        $sharedEventManager->attach('user', 'log-fail', function($event) use ($services) {
            $username = $event->getParam('username');
            $log = $services->get('log');
            $log->warn('Error logging user: ' . $username);
        });
    }
}

3。 LogService.php

服务文件

namespace Application\Service;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream as StreamWriter;
use Zend\Log\Filter\Priority as PriorityFilter;

class Log implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $controllerPluginManager = $container;
        $serviceManager = $controllerPluginManager->get('ServiceManager');

        // Instantiate Zend\Log\Logger
        $log = new Logger();
        // Set stream to write in a log file
        $writer = new StreamWriter('data/logs/audit.log');
        $log->addWriter($writer);

        return $log;
    }
}

4。 LogControllerFactory.php *

控制器出厂文件

namespace Application\Controller;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class LogControllerFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $controllerPluginManager = $container;
        $eventManager = $controllerPluginManager->get('EventManager');
        $sharedEventManager = $eventManager->getSharedManager();

        return new LogController($sharedEventManager);
    }
}

5。 LogController.php

控制器文件

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;

class LogController extends AbstractActionController
{
    public function __construct(SharedEventManager $sharedEventManager)
    {
        $this->_sharedEventManager = $sharedEventManager;
    }

    public function inAction()
    {
        //...
        if (! $isValid) {
            $event = new EventManager($this->_sharedEventManager, ['user']);
            $event->trigger('log-fail', $this, array('username'=> $username));
            //...
        }
    }
}