-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Middleware sorting was broken if middleware had parameters. Totally rewrote the algorithm and added more tests.
- Loading branch information
1 parent
98533d2
commit 6b69fb8
Showing
3 changed files
with
131 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class SortedMiddleware extends Collection | ||
{ | ||
/** | ||
* Create a new Sorted Middleware container. | ||
* | ||
* @param array $priorityMap | ||
* @param array $middlewares | ||
* @return void | ||
*/ | ||
public function __construct(array $priorityMap, $middlewares) | ||
{ | ||
if ($middlewares instanceof Collection) { | ||
$middlewares = $middlewares->all(); | ||
} | ||
|
||
$this->items = $this->sortMiddleware($priorityMap, $middlewares); | ||
} | ||
|
||
/** | ||
* Sort the middlewares by the given priority map. | ||
* | ||
* @param array $priorityMap | ||
* @param array $middlewares | ||
* @return array | ||
*/ | ||
protected function sortMiddleware($priorityMap, $middlewares) | ||
{ | ||
$lastIndex = 0; | ||
|
||
foreach ($middlewares as $index => $middleware) { | ||
if (! is_string($middleware)) { | ||
continue; | ||
} | ||
|
||
$stripped = head(explode(':', $middleware)); | ||
|
||
if (in_array($stripped, $priorityMap)) { | ||
$priorityIndex = array_search($stripped, $priorityMap); | ||
|
||
if (isset($lastPriorityIndex) && $priorityIndex < $lastPriorityIndex) { | ||
return $this->sortMiddleware( | ||
$priorityMap, array_values($this->spliceMiddleware($middlewares, $index, $lastIndex)) | ||
); | ||
} else { | ||
$lastIndex = $index; | ||
$lastPriorityIndex = $priorityIndex; | ||
} | ||
} | ||
} | ||
|
||
return $middlewares; | ||
} | ||
|
||
/** | ||
* Splice a middleware into a new position and remove the old entry. | ||
* | ||
* @param array $middlewares | ||
* @param int $from | ||
* @param int $to | ||
* @return array | ||
*/ | ||
protected function spliceMiddleware($middlewares, $from, $to) | ||
{ | ||
array_splice($middlewares, $to, 0, $middlewares[$from]); | ||
|
||
unset($middlewares[$from + 1]); | ||
|
||
return $middlewares; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
use Illuminate\Routing\SortedMiddleware; | ||
|
||
class RoutingSortedMiddlewareTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testMiddlewareCanBeSortedByPriority() | ||
{ | ||
$priority = [ | ||
'First', | ||
'Second', | ||
'Third', | ||
]; | ||
|
||
$middleware = [ | ||
'Something', | ||
'Something', | ||
'Something', | ||
'Something', | ||
'Second', | ||
'Something', | ||
'Something', | ||
'First:api', | ||
'Third:foo', | ||
'First:foo,bar', | ||
'Third', | ||
'Second', | ||
]; | ||
|
||
$expected = [ | ||
'Something', | ||
'Something', | ||
'Something', | ||
'Something', | ||
'First:api', | ||
'First:foo,bar', | ||
'Second', | ||
'Something', | ||
'Something', | ||
'Second', | ||
'Third:foo', | ||
'Third', | ||
]; | ||
|
||
$this->assertEquals($expected, (new SortedMiddleware($priority, $middleware))->all()); | ||
|
||
$this->assertEquals([], (new SortedMiddleware(['First'], []))->all()); | ||
$this->assertEquals(['First'], (new SortedMiddleware(['First'], ['First']))->all()); | ||
$this->assertEquals(['First', 'Second'], (new SortedMiddleware(['First', 'Second'], ['Second', 'First']))->all()); | ||
|
||
$closure = function () {}; | ||
$this->assertEquals(['Second', $closure], (new SortedMiddleware(['First', 'Second'], ['Second', $closure]))->all()); | ||
} | ||
} |