protected function doSortMiddleware(array $priorityMap, array $middleware): array
    {
        $lastIndex = $lastPriorityIndex = 0;

        foreach ($middleware as $index => $mware) {
            if (\in_array($mware, $priorityMap, true)) {
                $priorityIndex = \array_search($mware, $priorityMap, true);

                // This middleware is in the priority map. If we have encountered another middleware
                // that was also in the priority map and was at a lower priority than the current
                // middleware, we will move this middleware to be above the previous encounter.
                if (isset($lastPriorityIndex) && $priorityIndex < $lastPriorityIndex) {
                    return $this->doSortMiddleware(
                        $priorityMap,
                        \array_values(
                            $this->moveMiddleware($middleware, $index, $lastIndex)
                        )
                    );
                }

                // This middleware is in the priority map; but, this is the first middleware we have
                // encountered from the map thus far. We'll save its current index plus its index
                // from the priority map so we can compare against them on the next iterations.
                $lastIndex         = $index;
                $lastPriorityIndex = $priorityIndex;
            }
        }

        return \array_values(\array_unique($middleware, \SORT_REGULAR));
    }