Zend\Mvc\Controller\Plugin\Forward::detachProblemListeners PHP Method

detachProblemListeners() protected method

Detach problem listeners specified by getListenersToDetach() and return an array of information that will allow them to be reattached.
protected detachProblemListeners ( Zend\EventManager\SharedEventManagerInterface $sharedEvents ) : array
$sharedEvents Zend\EventManager\SharedEventManagerInterface Shared event manager
return array
    protected function detachProblemListeners(SharedEvents $sharedEvents)
    {
        // Convert the problem list from two-dimensional array to more convenient id => event => class format:
        $formattedProblems = [];
        foreach ($this->getListenersToDetach() as $current) {
            if (!isset($formattedProblems[$current['id']])) {
                $formattedProblems[$current['id']] = [];
            }
            if (!isset($formattedProblems[$current['id']][$current['event']])) {
                $formattedProblems[$current['id']][$current['event']] = [];
            }
            $formattedProblems[$current['id']][$current['event']][] = $current['class'];
        }
        // Loop through the class blacklist, detaching problem events and remembering their CallbackHandlers
        // for future reference:
        $results = [];
        foreach ($formattedProblems as $id => $eventArray) {
            $results[$id] = [];
            foreach ($eventArray as $eventName => $classArray) {
                $results[$id][$eventName] = [];
                $events = $this->getSharedListenersById($id, $eventName, $sharedEvents);
                foreach ($events as $priority => $currentPriorityEvents) {
                    // v2 fix
                    if (!is_array($currentPriorityEvents)) {
                        $currentPriorityEvents = [$currentPriorityEvents];
                    }
                    // v3
                    foreach ($currentPriorityEvents as $currentEvent) {
                        $currentCallback = $currentEvent;
                        // zend-eventmanager v2 compatibility:
                        if ($currentCallback instanceof CallbackHandler) {
                            $currentCallback = $currentEvent->getCallback();
                            $priority = $currentEvent->getMetadatum('priority');
                        }
                        // If we have an array, grab the object
                        if (is_array($currentCallback)) {
                            $currentCallback = array_shift($currentCallback);
                        }
                        // This routine is only valid for object callbacks
                        if (!is_object($currentCallback)) {
                            continue;
                        }
                        foreach ($classArray as $class) {
                            if ($currentCallback instanceof $class) {
                                // Pass $currentEvent; when using zend-eventmanager v2,
                                // this is the CallbackHandler, while in v3 it's
                                // the actual listener.
                                $this->detachSharedListener($id, $currentEvent, $sharedEvents);
                                $results[$id][$eventName][$priority] = $currentEvent;
                            }
                        }
                    }
                }
            }
        }
        return $results;
    }