Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Use parents to resolve middleware priority #39647

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
//
}