ZF3/Laminas Shared Event Manager Debugging

Debugging the shared event manager can be difficult after ZF2 removed some of the class methods. Here’s a function to retrieve information from an instance of the shared event manager:

protected function getTargetsFromSharedManager(SharedEventManagerInterface $shared)
{
    try {
        $refl  = new ReflectionProperty($shared, 'identifiers');
        $refl->setAccessible(true);
        $inst  = $refl->getValue($shared);
        $types = [];
        foreach (array_keys($inst) as $key) {
            foreach ($inst[$key] ?? [] as $listener => $events) {
                foreach ($events as $priority => $event) {
                    foreach ($event as $object) {
                        if (is_object($object)) {
                            $types[$key][] = get_class($object);
                        } else if (is_array($object)) {
                            $obj       = explode('\\', get_class($object[0]));
                            $class     = $obj[count($obj) - 1];
                            $namespace = implode('\\', array_map(static function ($s) use ($class) {
                                if ($s !== $class) {
                                    return $s;
                                }
                            },$obj));
                            $types[$key][] = [
                                'Event'     => $listener,
                                'Priority'  => $priority,
                                'Namespace' => trim($namespace, '\\'),
                                'Class'     => $class,
                                'Method'    => $object[1]
                            ];

                        }
                    }
                }
            }
        }
        return $types;
    } catch (ReflectionException $e) {
        // Log any reflection errors
        error_log($e->getMessage(), 3, 'data/error.log');
    }
    return null;
}

Thanks for posting this, @tnrn9b!