Merge pull request #10 from splashsky/add-route-group

🌊 Add prefix route group + fixes
This commit is contained in:
Skylear 2021-07-16 07:53:45 -05:00 committed by GitHub
commit bf56160b33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ class Router
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 = '';
/** /**
* 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.
@ -20,8 +21,13 @@ class Router
*/ */
public static function add(string $route, callable|string $action, string|array $methods = 'GET') public static function add(string $route, callable|string $action, string|array $methods = 'GET')
{ {
// If a prefix exists, prepend it to the route
if (!empty(self::$currentPrefix)) {
$route = self::$currentPrefix.$route;
}
self::$routes[] = [ self::$routes[] = [
'route' => $route, 'route' => self::trimRoute($route),
'action' => $action, 'action' => $action,
'methods' => $methods 'methods' => $methods
]; ];
@ -34,6 +40,7 @@ class Router
* *
* @param string $route * @param string $route
* @param callable $action * @param callable $action
* @return Router
*/ */
public static function get(string $route, callable $action) public static function get(string $route, callable $action)
{ {
@ -45,6 +52,7 @@ class Router
* *
* @param string $route * @param string $route
* @param callable $action * @param callable $action
* @return Router
*/ */
public static function post(string $route, callable $action) public static function post(string $route, callable $action)
{ {
@ -65,6 +73,7 @@ class Router
* Defines an action to be called when a path isn't found - i.e. a 404 * Defines an action to be called when a path isn't found - i.e. a 404
* *
* @param callable $action * @param callable $action
* @return void
*/ */
public static function pathNotFound(callable $action) public static function pathNotFound(callable $action)
{ {
@ -75,6 +84,7 @@ class Router
* Defines an action to be called with a method isn't allowed on a route - i.e. a 405 * Defines an action to be called with a method isn't allowed on a route - i.e. a 405
* *
* @param callable $action * @param callable $action
* @return void
*/ */
public static function methodNotAllowed(callable $action) public static function methodNotAllowed(callable $action)
{ {
@ -92,6 +102,28 @@ 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.
*
* @param string $prefix The prefix you want added to the routes.
* @param callable $routes A function that defines routes.
* @return void
*/
public static function prefix(string $prefix, callable $routes)
{
self::$currentPrefix = $prefix;
$routes();
self::$currentPrefix = '';
}
/** /**
* Define a constraint for a route parameter. If only passing one parameter, * Define a constraint for a route parameter. If only passing one parameter,
* provide the parameter name as first argument and constraint as second. If * provide the parameter name as first argument and constraint as second. If
@ -134,7 +166,10 @@ class Router
$pattern = '{'.$match.'}'; $pattern = '{'.$match.'}';
if (in_array($match, $constraints)) { if (in_array($match, $constraints)) {
$uri = str_replace($pattern, '('.self::$constraints[$match].')', $uri); // Do some voodoo to allow users to use parentheses in their constraints if they want
$constraint = '('.rtrim(ltrim(trim(self::$constraints[$match]), '('), ')').')';
$uri = str_replace($pattern, $constraint, $uri);
} else { } else {
$uri = str_replace($pattern, self::$defaultConstraint, $uri); $uri = str_replace($pattern, self::$defaultConstraint, $uri);
} }
@ -149,25 +184,22 @@ class Router
* *
* @param string $basePath * @param string $basePath
* @param boolean $multimatch * @param boolean $multimatch
* @return void
*/ */
public static function run(string $basePath = '', bool $multimatch = false) public static function run(string $basePath = '', bool $multimatch = false)
{ {
$basePath = rtrim($basePath, '/'); $basePath = self::trimRoute($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(rtrim($uri, '/')); $path = urldecode(self::trimRoute($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 ($basePath != '' && $basePath != '/') { // If the basePath isn't just "root"
if ($basePath != '/') {
$route['route'] = $basePath.$route['route']; $route['route'] = $basePath.$route['route'];
} }