Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 14, 2016
1 parent 98a18dd commit 9cc5334
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Illuminate/Routing/SortedMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function __construct(array $priorityMap, $middlewares)
/**
* Sort the middlewares by the given priority map.
*
* Each call to this method makes one discrete middleware movement if necessary.
*
* @param array $priorityMap
* @param array $middlewares
* @return array
Expand All @@ -43,10 +45,19 @@ protected function sortMiddleware($priorityMap, $middlewares)
if (in_array($stripped, $priorityMap)) {
$priorityIndex = array_search($stripped, $priorityMap);

// 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->sortMiddleware(
$priorityMap, array_values($this->spliceMiddleware($middlewares, $index, $lastIndex))
$priorityMap, array_values(
$this->moveMiddleware($middlewares, $index, $lastIndex)
)
);

// This middlewarwe 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.
} else {
$lastIndex = $index;
$lastPriorityIndex = $priorityIndex;
Expand All @@ -65,7 +76,7 @@ protected function sortMiddleware($priorityMap, $middlewares)
* @param int $to
* @return array
*/
protected function spliceMiddleware($middlewares, $from, $to)
protected function moveMiddleware($middlewares, $from, $to)
{
array_splice($middlewares, $to, 0, $middlewares[$from]);

Expand Down

0 comments on commit 9cc5334

Please sign in to comment.