Cleanup WillySoft\Http\Route
This commit is contained in:
parent
1bd9c5d5dd
commit
254b3e9f56
@ -2,38 +2,27 @@
|
||||
|
||||
namespace WillySoft\Http;
|
||||
|
||||
abstract class Route
|
||||
{
|
||||
public static array $groups = [
|
||||
// default global group
|
||||
abstract class Route {
|
||||
static array $groups = [
|
||||
[]
|
||||
];
|
||||
|
||||
public static function match(string $methods, string $path, callable $callback)
|
||||
{
|
||||
// check if request methods match
|
||||
static function match(string $methods, string $path, callable $callback) {
|
||||
if (!in_array($_SERVER['REQUEST_METHOD'],
|
||||
array_map(strtoupper(...), explode('|', $methods))
|
||||
)) return;
|
||||
|
||||
// check if the route matches and add parameters if any
|
||||
$args = [];
|
||||
$request_path = (function() {
|
||||
$haystack = strtok($_SERVER['REQUEST_URI'], '?');
|
||||
$needle = dirname($_SERVER['SCRIPT_NAME']);
|
||||
if ($needle === '/') {
|
||||
$needle = '';
|
||||
}
|
||||
$pos = strpos($haystack, $needle);
|
||||
return substr_replace($haystack, '', $pos, strlen($needle));
|
||||
})();
|
||||
$callback_args = [];
|
||||
$request_path = urldecode($_SERVER['REQUEST_URI']);
|
||||
$path_parts = explode('/', $path);
|
||||
$request_path_parts = explode('/', $request_path);
|
||||
for ($i=0; $i < count($path_parts); $i++) {
|
||||
if (empty($path_parts[$i])) {
|
||||
continue;
|
||||
}
|
||||
if ($path_parts[$i][0] === '$') {
|
||||
if ($path_parts[$i][0] !== '$') {
|
||||
continue;
|
||||
}
|
||||
if (!isset($request_path_parts[$i])) {
|
||||
return;
|
||||
}
|
||||
@ -41,35 +30,33 @@ abstract class Route
|
||||
return;
|
||||
}
|
||||
if (!empty($request_path_parts[$i])) {
|
||||
$args[] = $request_path_parts[$i];
|
||||
$callback_args[] = $request_path_parts[$i];
|
||||
}
|
||||
$request_path_parts[$i] = $path_parts[$i];
|
||||
}
|
||||
}
|
||||
if (
|
||||
implode('/', $path_parts) !== implode('/', $request_path_parts)
|
||||
) return;
|
||||
|
||||
// we have a match! run the middlewares before the callback before we die :)
|
||||
|
||||
foreach (self::$groups as $middlewares) {
|
||||
foreach ($middlewares as $middleware) {
|
||||
$middleware();
|
||||
}
|
||||
}
|
||||
$callback(...$args);
|
||||
$callback(...$callback_args);
|
||||
die();
|
||||
}
|
||||
|
||||
public static function get(string $path, callable $callback) { self::match('get', $path, $callback); }
|
||||
public static function post(string $path, callable $callback) { self::match('post', $path, $callback); }
|
||||
public static function put(string $path, callable $callback) { self::match('put', $path, $callback); }
|
||||
public static function patch(string $path, callable $callback) { self::match('patch', $path, $callback); }
|
||||
public static function delete(string $path, callable $callback) { self::match('delete', $path, $callback); }
|
||||
public static function options(string $path, callable $callback) { self::match('options', $path, $callback); }
|
||||
public static function any(string $path, callable $callback) { self::match('get|post|put|patch|delete|options', $path, $callback); }
|
||||
static function get(string $path, callable $callback) { self::match('get', $path, $callback); }
|
||||
static function post(string $path, callable $callback) { self::match('post', $path, $callback); }
|
||||
static function put(string $path, callable $callback) { self::match('put', $path, $callback); }
|
||||
static function patch(string $path, callable $callback) { self::match('patch', $path, $callback); }
|
||||
static function delete(string $path, callable $callback) { self::match('delete', $path, $callback); }
|
||||
static function options(string $path, callable $callback) { self::match('options', $path, $callback); }
|
||||
static function any(string $path, callable $callback) { self::match('get|post|put|patch|delete|options', $path, $callback); }
|
||||
|
||||
public static function middleware(callable|array $middlewares)
|
||||
{
|
||||
static function middleware(callable|array $middlewares) {
|
||||
if (!is_array($middlewares)) {
|
||||
$middlewares = [$middlewares];
|
||||
}
|
||||
@ -78,8 +65,7 @@ abstract class Route
|
||||
}
|
||||
}
|
||||
|
||||
public static function group(callable $callback)
|
||||
{
|
||||
static function group(callable $callback) {
|
||||
self::$groups[] = [];
|
||||
$callback();
|
||||
array_pop(self::$groups);
|
||||
|
Loading…
Reference in New Issue
Block a user