2022-11-29 12:49:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace WillySoft\Http;
|
|
|
|
|
2023-01-27 10:09:12 +00:00
|
|
|
abstract class Route {
|
|
|
|
static array $groups = [
|
2022-11-29 12:49:29 +00:00
|
|
|
[]
|
|
|
|
];
|
|
|
|
|
2023-01-27 10:09:12 +00:00
|
|
|
static function match(string $methods, string $path, callable $callback) {
|
2022-11-29 12:49:29 +00:00
|
|
|
if (!in_array($_SERVER['REQUEST_METHOD'],
|
|
|
|
array_map(strtoupper(...), explode('|', $methods))
|
|
|
|
)) return;
|
|
|
|
|
2023-01-30 21:18:57 +00:00
|
|
|
static $cache;
|
|
|
|
if (!isset($cache)) {
|
|
|
|
$cache = explode('/', urldecode(
|
|
|
|
strtok($_SERVER['REQUEST_URI'], '?')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$request_path_parts = $cache;
|
|
|
|
|
2023-01-27 13:09:02 +00:00
|
|
|
$path_parts = explode('/', $path);
|
|
|
|
$callback_args = [];
|
2022-11-29 12:49:29 +00:00
|
|
|
for ($i=0; $i < count($path_parts); $i++) {
|
|
|
|
if (empty($path_parts[$i])) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-27 10:09:12 +00:00
|
|
|
if ($path_parts[$i][0] !== '$') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isset($request_path_parts[$i])) {
|
|
|
|
return;
|
2022-11-29 12:49:29 +00:00
|
|
|
}
|
2023-01-27 10:09:12 +00:00
|
|
|
if ($path_parts[$i][-1] !== '?' && empty($request_path_parts[$i])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!empty($request_path_parts[$i])) {
|
2023-01-30 16:08:35 +00:00
|
|
|
array_push($callback_args, $request_path_parts[$i]);
|
2023-01-27 10:09:12 +00:00
|
|
|
}
|
|
|
|
$request_path_parts[$i] = $path_parts[$i];
|
2022-11-29 12:49:29 +00:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
implode('/', $path_parts) !== implode('/', $request_path_parts)
|
|
|
|
) return;
|
|
|
|
|
|
|
|
foreach (self::$groups as $middlewares) {
|
|
|
|
foreach ($middlewares as $middleware) {
|
|
|
|
$middleware();
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 10:09:12 +00:00
|
|
|
$callback(...$callback_args);
|
2022-11-29 12:49:29 +00:00
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2023-01-27 10:09:12 +00:00
|
|
|
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); }
|
2022-11-29 12:49:29 +00:00
|
|
|
|
2023-01-27 10:09:12 +00:00
|
|
|
static function middleware(callable|array $middlewares) {
|
2022-11-29 12:49:29 +00:00
|
|
|
if (!is_array($middlewares)) {
|
|
|
|
$middlewares = [$middlewares];
|
|
|
|
}
|
|
|
|
foreach ($middlewares as $middleware) {
|
2023-01-27 12:37:56 +00:00
|
|
|
array_push(
|
|
|
|
self::$groups[array_key_last(self::$groups)], $middleware
|
|
|
|
);
|
2022-11-29 12:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 10:09:12 +00:00
|
|
|
static function group(callable $callback) {
|
2023-01-27 12:37:56 +00:00
|
|
|
array_push(self::$groups, []);
|
2022-11-29 12:49:29 +00:00
|
|
|
$callback();
|
|
|
|
array_pop(self::$groups);
|
|
|
|
}
|
|
|
|
}
|