Update Route.php

This commit is contained in:
William 2025-03-26 16:21:37 +00:00
parent 74cb8b62c9
commit e3aa113f9e

View File

@ -5,15 +5,36 @@ namespace WillySoft;
use \Exception; use \Exception;
use \InvalidArgumentException; use \InvalidArgumentException;
/**
* Minimalistic Web Application Router
*/
abstract class Route abstract class Route
{ {
/**
* Used for group prefixes
*/
static string $prefix = ''; static string $prefix = '';
/**
* Stores the middleware callbacks in their respective group; each nesting represented by index
*/
static array $groups = [ static array $groups = [
[] // Default initialized group where all middlewares are added to if they are not inside any other groups
[
//fn() => print('this could be a middleware, and it would print on every single route!')
],
//[
// fn() => print('this is what it would look like if a new group was created and a middleware was
// added inside of it!')
//]
]; ];
private static function getRequestPath(): string /**
* Get request path relative to the entrypoint of the application
*/
static function getRelativeRequestPath(): string
{ {
// try to save a negligible amount of performance by caching the computated result
static $request_url_path; static $request_url_path;
if (isset($request_url_path)) { if (isset($request_url_path)) {
@ -29,14 +50,17 @@ abstract class Route
return $request_url_path; return $request_url_path;
} }
static function match(string $methods, string $url_path, callable $callback) /**
* Run all middlewares then callback if supplied arguments correspond with the request
*/
static function match(string $methods, string $url_path, callable $callback): void
{ {
if (!in_array( if (!in_array(
$_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_METHOD'],
array_map(strtoupper(...), explode('|', $methods)) array_map(strtoupper(...), explode('|', $methods))
)) return; )) return;
$request_url_path_parts = explode('/', self::getRequestPath()); $request_url_path_parts = explode('/', self::getRelativeRequestPath());
$url_path_parts = explode('/', self::$prefix . $url_path); $url_path_parts = explode('/', self::$prefix . $url_path);
$callback_args = []; $callback_args = [];
@ -79,38 +103,56 @@ abstract class Route
die(); die();
} }
static function get(string $url_path, callable $callback) // Shorthand for self::match(...)
static function get(string $url_path, callable $callback): void
{self::match('get', $url_path, $callback);} {self::match('get', $url_path, $callback);}
static function post(string $url_path, callable $callback) // Shorthand for self::match(...)
static function post(string $url_path, callable $callback): void
{self::match('post', $url_path, $callback);} {self::match('post', $url_path, $callback);}
static function put(string $url_path, callable $callback) // Shorthand for self::match(...)
static function put(string $url_path, callable $callback): void
{self::match('put', $url_path, $callback);} {self::match('put', $url_path, $callback);}
static function patch(string $url_path, callable $callback) // Shorthand for self::match(...)
static function patch(string $url_path, callable $callback): void
{self::match('patch', $url_path, $callback);} {self::match('patch', $url_path, $callback);}
static function delete(string $url_path, callable $callback) // Shorthand for self::match(...)
static function delete(string $url_path, callable $callback): void
{self::match('delete', $url_path, $callback);} {self::match('delete', $url_path, $callback);}
static function options(string $url_path, callable $callback) // Shorthand for self::match(...)
static function options(string $url_path, callable $callback): void
{self::match('options', $url_path, $callback);} {self::match('options', $url_path, $callback);}
static function form(string $url_path, callable $callback) // Shorthand for self::match(...)
static function form(string $url_path, callable $callback): void
{self::match('get|post', $url_path, $callback);} {self::match('get|post', $url_path, $callback);}
static function any(string $url_path, callable $callback) // Shorthand for self::match(...)
static function any(string $url_path, callable $callback): void
{self::match('get|post|put|patch|delete|options', $url_path, $callback);} {self::match('get|post|put|patch|delete|options', $url_path, $callback);}
static function use(callable $middleware) /**
* Adds Adds middleware to its respective group
*/
static function use(callable $middleware): void
{ {
array_push( array_push(
self::$groups[array_key_last(self::$groups)], self::$groups[array_key_last(self::$groups)],
$middleware $middleware
); );
} }
static function group(string $prefix = '', ?callable $callback = null) /**
* Create new route group
*
* @param string $prefix Optional prefix can be supplied; the group is ignored if getRelativeRequestPath
* does not correspond with the supplied value.
* @param callable $callback Required; the insides of the group
*/
static function group(string $prefix = '', ?callable $callback = null): void
{ {
if ($callback === null) { if ($callback === null) {
throw new InvalidArgumentException('Argument $callback must be of type callable.'); throw new InvalidArgumentException('Argument callback must be of type callable.');
} }
if (!str_starts_with( if (!str_starts_with(
self::getRequestPath(), self::getRelativeRequestPath(),
self::$prefix . $prefix self::$prefix . $prefix
)) return; )) return;
self::$prefix = self::$prefix . $prefix; self::$prefix = self::$prefix . $prefix;