Skip to content

Commit 025c912

Browse files
authored
respect parents on middleware priority (#46972)
1 parent 07a5e09 commit 025c912

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Illuminate/Routing/SortedMiddleware.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ protected function middlewareNames($middleware)
101101
yield $interface;
102102
}
103103
}
104+
105+
$parents = @class_parents($stripped);
106+
107+
if ($parents !== false) {
108+
foreach ($parents as $parent) {
109+
yield $parent;
110+
}
111+
}
104112
}
105113

106114
/**

tests/Routing/RoutingSortedMiddlewareTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,59 @@ public function testItDoesNotMoveNonStringValues()
6464
$this->assertEquals(['a', $closure, 'b', $closure2, 'foo'], (new SortedMiddleware(['a', 'b'], ['a', $closure, 'b', $closure2, 'foo']))->all());
6565
$this->assertEquals([$closure, $closure2, 'foo', 'a'], (new SortedMiddleware(['a', 'b'], [$closure, $closure2, 'foo', 'a']))->all());
6666
}
67+
68+
public function testItSortsUsingParentsAndContracts()
69+
{
70+
$priority = [
71+
FirstContractStub::class,
72+
SecondStub::class,
73+
'Third',
74+
];
75+
76+
$middleware = [
77+
'Something',
78+
'Something',
79+
'Something',
80+
'Something',
81+
SecondChildStub::class,
82+
'Otherthing',
83+
FirstStub::class.':api',
84+
'Third:foo',
85+
FirstStub::class.':foo,bar',
86+
'Third',
87+
SecondChildStub::class,
88+
];
89+
90+
$expected = [
91+
'Something',
92+
FirstStub::class.':api',
93+
FirstStub::class.':foo,bar',
94+
SecondChildStub::class,
95+
'Otherthing',
96+
'Third:foo',
97+
'Third',
98+
];
99+
100+
$this->assertEquals($expected, (new SortedMiddleware($priority, $middleware))->all());
101+
}
102+
}
103+
104+
interface FirstContractStub
105+
{
106+
//
107+
}
108+
109+
class FirstStub implements FirstContractStub
110+
{
111+
//
112+
}
113+
114+
class SecondStub
115+
{
116+
//
117+
}
118+
119+
class SecondChildStub extends SecondStub
120+
{
121+
//
67122
}

0 commit comments

Comments
 (0)