Skip to content

Commit

Permalink
Fix #121 improve catch all route order
Browse files Browse the repository at this point in the history
  • Loading branch information
nickl- committed Mar 22, 2019
1 parent 72cde63 commit 8fb8792
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions library/Respect/Rest/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,6 @@ public static function compareOcurrences($patternA, $patternB, $sub)
< substr_count($patternB, $sub);
}

/**
* Compares two patterns and returns the first one according to
* similarity or presence of catch-all pattern
*
* @param string $patternA some pattern
* @param string $patternB some pattern
*
* @return bool true if $patternA is before $patternB
*/
public static function comparePatternSimilarity($patternA, $patternB)
{
return 0 === stripos($patternA, $patternB)
|| $patternA === AbstractRoute::CATCHALL_IDENTIFIER;
}

/**
* Compares two patterns and returns the first one according to
* similarity, patterns or ocurrences of a subpattern
Expand Down Expand Up @@ -780,23 +765,29 @@ function ($a, $b) {
$a = $a->pattern;
$b = $b->pattern;

//Compare similarity and ocurrences of "/**"
if (Router::compareRoutePatterns($a, $b,
AbstractRoute::CATCHALL_IDENTIFIER))
return -1;
//Compare the same
if ($a === $b)
return 0;

//Compare similarity and ocurrences of "/*"
elseif (Router::compareRoutePatterns($a, $b,
//Compare occurences of '/' reusable
$slash_count = Router::compareOcurrences($a, $b, '/');

//Compare catch all "/**"
$a_catchall = preg_match('#/\*\*$#', $a);
$b_catchall = preg_match('#/\*\*$#', $b);
if ($a_catchall != $b_catchall)
return $a_catchall ? 1 : -1;
//Compare occurances of '/' of two catch alls
if ($a_catchall && $b_catchall)
return $slash_count ? 1 : -1;

//Compare ocurrences of "/*"
if (Router::compareOcurrences($a, $b,
AbstractRoute::PARAM_IDENTIFIER))
return -1;

//Compare for "/" without wildcards
elseif (Router::compareRoutePatterns(
preg_replace('#(/\*+)*$#', '', $a),
preg_replace('#(/\*+)*$#', '', $b), '/'))
return 1;

return 0;
//Compare occurances of '/'
return $slash_count ? -1 : 1;
}
);
}
Expand Down

0 comments on commit 8fb8792

Please sign in to comment.