diff --git a/src/Illuminate/Routing/SortedMiddleware.php b/src/Illuminate/Routing/SortedMiddleware.php index 853378cf7e92..3c2c7912d219 100644 --- a/src/Illuminate/Routing/SortedMiddleware.php +++ b/src/Illuminate/Routing/SortedMiddleware.php @@ -101,6 +101,14 @@ protected function middlewareNames($middleware) yield $interface; } } + + $parents = @class_parents($stripped); + + if ($parents !== false) { + foreach ($parents as $parent) { + yield $parent; + } + } } /** diff --git a/tests/Routing/RoutingSortedMiddlewareTest.php b/tests/Routing/RoutingSortedMiddlewareTest.php index e5877a844ce7..4416f9be3686 100644 --- a/tests/Routing/RoutingSortedMiddlewareTest.php +++ b/tests/Routing/RoutingSortedMiddlewareTest.php @@ -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 +{ + // }