Merge pull request #11 from splashsky/fix-overlapping-constraint-bug

🚧 Fix overlap by making constraints route-specific
This commit is contained in:
Skylear 2021-07-16 13:03:42 -05:00 committed by GitHub
commit f85ca7666a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,11 +5,11 @@ namespace Splashsky;
class Router class Router
{ {
private static array $routes = []; private static array $routes = [];
private static array $constraints = [];
private static $pathNotFound; private static $pathNotFound;
private static $methodNotAllowed; private static $methodNotAllowed;
private static string $defaultConstraint = '([\w\-]+)'; private static string $defaultConstraint = '([\w\-]+)';
private static string $currentPrefix = ''; private static string $currentPrefix = '';
private static string $lastInsertedRoute = '';
/** /**
* A quick static function to register a route in the router. Used by the shorthand methods as well. * A quick static function to register a route in the router. Used by the shorthand methods as well.
@ -26,12 +26,17 @@ class Router
$route = self::$currentPrefix.$route; $route = self::$currentPrefix.$route;
} }
self::$routes[] = [ $trimmed = self::trimRoute($route);
'route' => self::trimRoute($route),
self::$routes[$trimmed] = [
'route' => $trimmed,
'action' => $action, 'action' => $action,
'methods' => $methods 'methods' => $methods,
'constraints' => []
]; ];
self::$lastInsertedRoute = $trimmed;
return new self; return new self;
} }
@ -136,15 +141,17 @@ class Router
*/ */
public static function with(string|array $parameter, string $constraint = '') public static function with(string|array $parameter, string $constraint = '')
{ {
$last = self::$lastInsertedRoute;
if (is_array($parameter)) { if (is_array($parameter)) {
foreach ($parameter as $param => $constraint) { foreach ($parameter as $param => $constraint) {
self::$constraints[$param] = $constraint; self::$routes[$last]['constraints'][$param] = $constraint;
} }
return new self; return new self;
} }
self::$constraints[$parameter] = $constraint; self::$routes[$last]['constraints'][$parameter] = $constraint;
return new self; return new self;
} }
@ -157,7 +164,8 @@ class Router
*/ */
private static function tokenize(string $uri) private static function tokenize(string $uri)
{ {
$constraints = array_keys(self::$constraints); $constraints = self::$routes[$uri]['constraints'];
$constraintKeys = array_keys($constraints);
preg_match_all('/(?:{([\w\-]+)})+/', $uri, $matches); preg_match_all('/(?:{([\w\-]+)})+/', $uri, $matches);
$matches = $matches[1]; $matches = $matches[1];
@ -165,9 +173,9 @@ class Router
foreach ($matches as $match) { foreach ($matches as $match) {
$pattern = '{'.$match.'}'; $pattern = '{'.$match.'}';
if (in_array($match, $constraints)) { if (in_array($match, $constraintKeys)) {
// Do some voodoo to allow users to use parentheses in their constraints if they want // Do some voodoo to allow users to use parentheses in their constraints if they want
$constraint = '('.rtrim(ltrim(trim(self::$constraints[$match]), '('), ')').')'; $constraint = '('.rtrim(ltrim(trim($constraints[$match]), '('), ')').')';
$uri = str_replace($pattern, $constraint, $uri); $uri = str_replace($pattern, $constraint, $uri);
} else { } else {