Skip to content

Commit

Permalink
respect parents on middleware priority (laravel#46972)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell authored May 5, 2023
1 parent 07a5e09 commit 025c912
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 025c912

Please sign in to comment.