EventManager and Listener class

Ein Listener:

namespace Lcamo\CamoTransfer\Event;

use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Log\Logger;

class CamoListener extends AbstractListenerAggregate
{
    protected $listeners = [];

    /**
     * @var Logger
     */
    protected $logger;

    /**
     * @param Logger $logger
     */
    public function setLogger(Logger $logger): void
    {
        $this->logger = $logger;
    }

    /**
     * Attach one or more listeners
     *
     * Implementors may add an optional $priority argument; the EventManager
     * implementation will pass this to the aggregate.
     *
     * @param EventManagerInterface $events
     * @param int $priority
     * @return void
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach('before_login', array($this, 'beforeLogin'));
    }

    public function beforeLogin(EventInterface $event)
    {
        $params = $event->getParams();
        $this->logger->info(print_r($params, true));
    }
}

Factory des Listeners (hier bekommt der Listener den EventManager vom UserService injiziert:

namespace Lcamo\CamoTransfer\Factory;

use Bitkorn\User\Service\UserService;
use Interop\Container\ContainerInterface;
use Lcamo\CamoTransfer\Event\CamoListener;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;

class CamoListenerFactory implements FactoryInterface
{

    /**
     * Create an object
     *
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param null|array $options
     * @return object
     * @throws ServiceNotFoundException if unable to resolve the service.
     * @throws ServiceNotCreatedException if an exception is raised when
     *     creating a service.
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        /** @var UserService $userService */
        $userService = $container->get(UserService::class);
        $eventManager = $userService->getEvents();
        $listener = new CamoListener();
        $listener->setLogger($container->get('logger'));
        $listener->attach($eventManager);
        return $listener;
    }
}

Den Listener in der /config/application.config.php eintragen:

use Lcamo\CamoTransfer\Event\CamoListener;

return [
    // Retrieve list of modules used in this application.
    'modules' => require __DIR__ . '/modules.config.php',
    'listeners' => [
        CamoListener::class
    ],
    // These are various options for the listeners attached to the ModuleManager
    'module_listener_options' => [
        // This should be an array of paths in which modules reside.
        // If a string key is provided, the listener will consider that a module
        // namespace, the value of that key the specific path to that module's
        // Module class.
        'module_paths' => [
            './module',
            './vendor',
        ],

        // An array of paths from which to glob configuration files after
        // modules are loaded. These effectively override configuration
        // provided by modules themselves. Paths may use GLOB_BRACE notation.
        'config_glob_paths' => [
            realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php',
        ],
        // ...
];

Und der UserService, der den EventManager hält und das Event triggert:

namespace Bitkorn\User\Service;

use Zend\EventManager\EventManager;

class UserService
{

    /**
     * @var EventManager
     */
    protected $events;

    /**
     * @return EventManager
     */
    public function getEvents(): EventManager
    {
        return $this->events;
    }

    /**
     *
     * @param string $login
     * @param string $password
     * @return bool
     */
    public function loginWithLoginAndPassword(string $login, string $password): bool
    {
        $this->events->trigger('before_login', $this, ['login' => $login, 'passwd' => $password]);

        /**
         * ...some other stuff
         */
        return true;
    }

    /**
     * ...some other stuff
     */
}

U.a. Dank an Samsonasik.