[ROUTER] Sort routes so that the one with a smaller list of Accept types matches first

This requires a copy, but gets cached, so it's the ideal place to do it.

Note that only routes that match the incoming Accept match anyway, so the order between those with different accept types is not relevant
This commit is contained in:
Hugo Sales 2021-09-01 15:54:13 +01:00
parent 45734d882c
commit 983e0303a5
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
1 changed files with 11 additions and 0 deletions

View File

@ -64,6 +64,16 @@ class RouteLoader extends Loader
Event::handle('AddRoute', [&$this]);
// Sort routes so that whichever route has the smallest accept option matches first, as it's more specific
// This requires a copy, sadly, as it doesn't seem to be possible to modify the collection in-place
// However, this is fine since this gets cached
$it = $this->rc->getIterator();
$it->uasort(fn (Route $left, Route $right) => count($left->getDefaults()['accept']) <=> count($right->getDefaults()['accept']));
$this->rc = new RouteCollection();
foreach ($it as $id => $route) {
$this->rc->add($id, $route);
}
return $this->rc;
}
@ -93,6 +103,7 @@ class RouteLoader extends Loader
'_fragment' => $options['fragment'] ?? '',
'_locale' => $options['locale'] ?? 'en',
'template' => $options['template'] ?? '',
'accept' => $options['accept'] ?? [],
],
$options['defaults'] ?? []
),