Skip to content

Commit

Permalink
[8.x] Use parents to resolve middleware priority (#39647)
Browse files Browse the repository at this point in the history
* Use parents to resolve middleware priority

* Add test for middleware sorting with parents
  • Loading branch information
netpok committed Nov 17, 2021
1 parent e75a59e commit cfab148
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Illuminate/Routing/SortedMiddleware.php
Expand Up @@ -101,6 +101,14 @@ protected function middlewareNames($middleware)
yield $interface;
}
}

$parents = @class_parents($stripped);

if ($parents !== false) {
foreach ($parents as $parent) {
yield $parent;
}
}
}

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/Routing/RoutingSortedMiddlewareTest.php
Expand Up @@ -64,4 +64,59 @@ public function testItDoesNotMoveNonStringValues()
$this->assertEquals(['a', $closure, 'b', $closure2, 'foo'], (new SortedMiddleware(['a', 'b'], ['a', $closure, 'b', $closure2, 'foo']))->all());
$this->assertEquals([$closure, $closure2, 'foo', 'a'], (new SortedMiddleware(['a', 'b'], [$closure, $closure2, 'foo', 'a']))->all());
}

public function testItSortsUsingParentsAndContracts()
{
$priority = [
FirstContractStub::class,
SecondStub::class,
'Third',
];

$middleware = [
'Something',
'Something',
'Something',
'Something',
SecondChildStub::class,
'Otherthing',
FirstStub::class.':api',
'Third:foo',
FirstStub::class.':foo,bar',
'Third',
SecondChildStub::class,
];

$expected = [
'Something',
FirstStub::class.':api',
FirstStub::class.':foo,bar',
SecondChildStub::class,
'Otherthing',
'Third:foo',
'Third',
];

$this->assertEquals($expected, (new SortedMiddleware($priority, $middleware))->all());
}
}

interface FirstContractStub
{
//
}

class FirstStub implements FirstContractStub
{
//
}

class SecondStub
{
//
}

class SecondChildStub extends SecondStub
{
//
}

0 comments on commit cfab148

Please sign in to comment.