private function buildListener($name, $data, &$listeners)
    {
        static $defaults = ['priority' => 0, 'once' => false];

        // If a callable is provided, normalize it to the array format.
        if (is_callable($data)) {
            $data = ['fn' => $data];
        }

        // Prepare the listener and add it to the array, recursively.
        if (isset($data['fn'])) {
            $data['name'] = $name;
            $listeners[] = $data + $defaults;
        } elseif (is_array($data)) {
            foreach ($data as $listenerData) {
                $this->buildListener($name, $listenerData, $listeners);
            }
        } else {
            throw new \InvalidArgumentException('Each event listener must be a '
                . 'callable or an associative array containing a "fn" key.');
        }
    }