Update Route.php

This commit is contained in:
William 2025-03-31 19:27:06 +00:00
parent 26c8469984
commit b05d48204b

View File

@ -10,31 +10,12 @@ use \InvalidArgumentException;
*/
abstract class Route
{
/**
* Used for group prefixes
*/
static string $prefix = '';
/**
* Stores the middleware callbacks in their respective group; each nesting represented by index
*/
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!')
//]
];
static array $groups = [ [ ] ];
/**
* 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;
if (isset($request_url_path)) {
@ -50,9 +31,6 @@ abstract class Route
return $request_url_path;
}
/**
* 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(
@ -103,34 +81,30 @@ abstract class Route
die();
}
// Shorthand for self::match(...)
static function get(string $url_path, callable $callback): void
{self::match('get', $url_path, $callback);}
// Shorthand for self::match(...)
static function post(string $url_path, callable $callback): void
{self::match('post', $url_path, $callback);}
// Shorthand for self::match(...)
static function put(string $url_path, callable $callback): void
{self::match('put', $url_path, $callback);}
// Shorthand for self::match(...)
static function patch(string $url_path, callable $callback): void
{self::match('patch', $url_path, $callback);}
// Shorthand for self::match(...)
static function delete(string $url_path, callable $callback): void
{self::match('delete', $url_path, $callback);}
// Shorthand for self::match(...)
static function options(string $url_path, callable $callback): void
{self::match('options', $url_path, $callback);}
// Shorthand for self::match(...)
static function form(string $url_path, callable $callback): void
{self::match('get|post', $url_path, $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);}
/**
* Adds middleware to its respective group
*/
static function use(callable $middleware): void
{
array_push(
@ -139,13 +113,6 @@ abstract class Route
);
}
/**
* 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) {