src/Factory/EventManager/EventManagerAbstractFactory.php
protected function attachListeners($services, $eventManager, $listeners)
    {
        $lazyListeners = [];

        foreach ($listeners as $name => $options) {
            $options = $this->normalizeListenerOptions($name, $options);

            if ($options['lazy'] && null !== $options['attach']) {
                foreach ($options['attach'] as $spec) {
                    $lazyListeners[] = [
                        'service' => $options['service'],
                        'event' => $spec['event'],
                        'method' => $spec['method'],
                        'priority' => $spec['priority'],
                    ];
                }
                continue;
            }

            if ($services->has($options['service'])) {
                $listener = $services->get($options['service']);
            } elseif (class_exists($options['service'], true)) {
                $listener = new $options['service']();
            } else {
                throw new \UnexpectedValueException(sprintf(
                                                        'Class or service %s does not exists. Cannot create listener instance.',
                    $options['service']
                                                    ));
            }

            if ($listener instanceof ListenerAggregateInterface) {
                $listener->attach($eventManager, $options['priority']);
                continue;
            }

            foreach ($options['attach'] as $spec) {
                $callback = $spec['method'] ? [ $listener, $spec['method'] ] : $listener;
                $eventManager->attach($spec['event'], $callback, $spec['priority']);
            }
        }

        if (!empty($lazyListeners)) {
            /* @var \Core\Listener\DeferredListenerAggregate $aggregate */
            $aggregate = $services->get('Core/Listener/DeferredListenerAggregate');
            $aggregate->setListeners($lazyListeners)
                      ->attach($eventManager);
        }
    }
protected function attachListeners($services, $eventManager, $listeners)
    {
        $lazyListeners = [];

        foreach ($listeners as $name => $options) {
            $options = $this->normalizeListenerOptions($name, $options);

            if ($options['lazy'] && null !== $options['attach']) {
                foreach ($options['attach'] as $spec) {
                    $lazyListeners[] = [
                        'service' => $options['service'],
                        'event' => $spec['event'],
                        'method' => $spec['method'],
                        'priority' => $spec['priority'],
                    ];
                }
                continue;
            }

            if ($services->has($options['service'])) {
                $listener = $services->get($options['service']);
            } elseif (class_exists($options['service'], true)) {
                $listener = new $options['service']();
            } else {
                throw new \UnexpectedValueException(sprintf(
                                                        'Class or service %s does not exists. Cannot create listener instance.',
                    $options['service']
                                                    ));
            }

            if ($listener instanceof ListenerAggregateInterface) {
                $listener->attach($eventManager, $options['priority']);
                continue;
            }

            foreach ($options['attach'] as $spec) {
                $callback = $spec['method'] ? [ $listener, $spec['method'] ] : $listener;
                $eventManager->attach($spec['event'], $callback, $spec['priority']);
            }
        }

        if (!empty($lazyListeners)) {
            /* @var \Core\Listener\DeferredListenerAggregate $aggregate */
            $aggregate = $services->get('Core/Listener/DeferredListenerAggregate');
            $aggregate->setListeners($lazyListeners)
                      ->attach($eventManager);
        }
    }
Attaches listeners provided in the config to the event manager instance.

@param ServiceLocatorInterface $services
@param \Zend\EventManager\EventManagerInterface $eventManager
@param array $listeners

@throws \UnexpectedValueException if a listener name cannot be fetched as service or be instantiated.