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
._*
/vendor/
/test.php
/src/Splashsky/test.php

View File

@ -5,11 +5,11 @@ namespace Splashsky;
class Router
{
private static array $routes = [];
private static array $constraints = [];
private static $pathNotFound;
private static $methodNotAllowed;
private static string $defaultConstraint = '([\w\-]+)';
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.
@ -26,17 +26,12 @@ class Router
$route = self::$currentPrefix.$route;
}
$trimmed = self::trimRoute($route);
self::$routes[] = [
'route' => $trimmed,
'route' => $route,
'action' => $action,
'methods' => $methods,
'constraints' => []
'methods' => $methods
];
self::$lastInsertedRoute = $trimmed;
return new self;
}
@ -107,12 +102,6 @@ class Router
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.
*
@ -141,17 +130,15 @@ class Router
*/
public static function with(string|array $parameter, string $constraint = '')
{
$last = self::$lastInsertedRoute;
if (is_array($parameter)) {
foreach ($parameter as $param => $constraint) {
self::$routes[$last]['constraints'][$param] = $constraint;
self::$constraints[$param] = $constraint;
}
return new self;
}
self::$routes[$last]['constraints'][$parameter] = $constraint;
self::$constraints[$parameter] = $constraint;
return new self;
}
@ -162,9 +149,9 @@ class Router
* @param string $uri
* @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);
$matches = $matches[1];
@ -172,11 +159,8 @@ class Router
foreach ($matches as $match) {
$pattern = '{'.$match.'}';
if (in_array($match, $constraintKeys)) {
// Do some voodoo to allow users to use parentheses in their constraints if they want
$constraint = '('.rtrim(ltrim(trim($constraints[$match]), '('), ')').')';
$uri = str_replace($pattern, $constraint, $uri);
if (in_array($match, $constraints)) {
$uri = str_replace($pattern, '('.self::$constraints[$match].')', $uri);
} else {
$uri = str_replace($pattern, self::$defaultConstraint, $uri);
}
@ -195,23 +179,27 @@ class Router
*/
public static function run(string $basePath = '', bool $multimatch = false)
{
$basePath = self::trimRoute($basePath);
$basePath = rtrim($basePath, '/');
$method = $_SERVER['REQUEST_METHOD'];
$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;
$routeMatchFound = false;
// Begin looking through routes
foreach (self::$routes as $route) {
// If the basePath isn't just "root"
if ($basePath != '/') {
$route['route'] = self::trimRoute($basePath.$route['route']);
if ($basePath != '' && $basePath != '/') {
$route['route'] = $basePath.$route['route'];
}
// 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 (preg_match($tokenized, $path, $matches)) {