1
0
Fork 0

Minimize URI parsing in loop

pull/281/head
Trevor Bortins 2017-02-06 14:18:19 -08:00
parent f84b22e05d
commit 8d439f82f5
1 changed files with 14 additions and 3 deletions

View File

@ -138,11 +138,22 @@ function stripStringOverlap(string $a, string $b): string
* in ascending order. * in ascending order.
* *
* @param array * @param array
* @return array * @return void
*/ */
function sortUrisLevelOrder(&$uriList) function sortUrisLevelOrder(&$uriList)
{ {
usort($uriList, function ($a, $b) { // Parse URIs so we are not continually parsing them while sorting
return substr_count(Uri\parse($a)['path'], '/') - substr_count(Uri\parse($b)['path'], '/'); $parsedUriList = array_map(function ($uri) {
return Uri\parse($uri);
}, $uriList);
// Sort by number of slashes in parsed URI path, ascending
usort($parsedUriList, function ($a, $b) {
return substr_count($a['path'], '/') - substr_count($b['path'], '/');
}); });
// Reassemble the URIs
$uriList = array_map(function ($parsedUri) {
return Uri\build($parsedUri);
}, $parsedUriList);
} }