Compare commits

..

No commits in common. "053d4f30b2aa0ebc3ed20d5b9cc1a92a801192be" and "1393e0face008325fbdeee6c05f93aa8ed40a4d1" have entirely different histories.

2 changed files with 20 additions and 32 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
.DS_Store .DS_Store
._* ._*
/vendor/ /vendor/
/test.php /src/Splashsky/test.php

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,17 +26,12 @@ class Router
$route = self::$currentPrefix.$route; $route = self::$currentPrefix.$route;
} }
$trimmed = self::trimRoute($route);
self::$routes[] = [ self::$routes[] = [
'route' => $trimmed, 'route' => $route,
'action' => $action, 'action' => $action,
'methods' => $methods, 'methods' => $methods
'constraints' => []
]; ];
self::$lastInsertedRoute = $trimmed;
return new self; return new self;
} }
@ -107,12 +102,6 @@ class Router
self::$defaultConstraint = $constraint; self::$defaultConstraint = $constraint;
} }
private static function trimRoute(string $route): string
{
$route = trim(trim($route), '/');
return "/$route";
}
/** /**
* Accepts a callable that defines routes, and adds a prefix to them. * Accepts a callable that defines routes, and adds a prefix to them.
* *
@ -141,17 +130,15 @@ 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::$routes[$last]['constraints'][$param] = $constraint; self::$constraints[$param] = $constraint;
} }
return new self; return new self;
} }
self::$routes[$last]['constraints'][$parameter] = $constraint; self::$constraints[$parameter] = $constraint;
return new self; return new self;
} }
@ -162,9 +149,9 @@ class Router
* @param string $uri * @param string $uri
* @return string * @return string
*/ */
private static function tokenize(string $uri, array $constraints) private static function tokenize(string $uri)
{ {
$constraintKeys = array_keys($constraints); $constraints = array_keys(self::$constraints);
preg_match_all('/(?:{([\w\-]+)})+/', $uri, $matches); preg_match_all('/(?:{([\w\-]+)})+/', $uri, $matches);
$matches = $matches[1]; $matches = $matches[1];
@ -172,11 +159,8 @@ class Router
foreach ($matches as $match) { foreach ($matches as $match) {
$pattern = '{'.$match.'}'; $pattern = '{'.$match.'}';
if (in_array($match, $constraintKeys)) { if (in_array($match, $constraints)) {
// Do some voodoo to allow users to use parentheses in their constraints if they want $uri = str_replace($pattern, '('.self::$constraints[$match].')', $uri);
$constraint = '('.rtrim(ltrim(trim($constraints[$match]), '('), ')').')';
$uri = str_replace($pattern, $constraint, $uri);
} else { } else {
$uri = str_replace($pattern, self::$defaultConstraint, $uri); $uri = str_replace($pattern, self::$defaultConstraint, $uri);
} }
@ -195,23 +179,27 @@ class Router
*/ */
public static function run(string $basePath = '', bool $multimatch = false) public static function run(string $basePath = '', bool $multimatch = false)
{ {
$basePath = self::trimRoute($basePath); $basePath = rtrim($basePath, '/');
$method = $_SERVER['REQUEST_METHOD']; $method = $_SERVER['REQUEST_METHOD'];
$uri = parse_url($_SERVER['REQUEST_URI'])['path']; $uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$path = urldecode(self::trimRoute($uri)); $path = urldecode(rtrim($uri, '/'));
// If the path is empty (no slash in URI) place one to satisfy the root route ('/')
if (empty($path)) {
$path = '/';
}
$pathMatchFound = false; $pathMatchFound = false;
$routeMatchFound = false; $routeMatchFound = false;
// Begin looking through routes // Begin looking through routes
foreach (self::$routes as $route) { foreach (self::$routes as $route) {
// If the basePath isn't just "root" if ($basePath != '' && $basePath != '/') {
if ($basePath != '/') { $route['route'] = $basePath.$route['route'];
$route['route'] = self::trimRoute($basePath.$route['route']);
} }
// Prepare route by tokenizing. // Prepare route by tokenizing.
$tokenized = '#^'.self::tokenize($route['route'], $route['constraints']).'$#u'; $tokenized = '#^'.self::tokenize($route['route']).'$#u';
// If the tokenized route matches the current path... // If the tokenized route matches the current path...
if (preg_match($tokenized, $path, $matches)) { if (preg_match($tokenized, $path, $matches)) {