2022-11-29 12:49:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace WillySoft\Http;
|
|
|
|
|
|
|
|
abstract class Route
|
|
|
|
{
|
|
|
|
public static array $groups = [
|
|
|
|
// default global group
|
|
|
|
[]
|
|
|
|
];
|
|
|
|
|
|
|
|
public static function match(string $methods, string $path, callable $callback)
|
|
|
|
{
|
|
|
|
// check if request methods match
|
|
|
|
if (!in_array($_SERVER['REQUEST_METHOD'],
|
|
|
|
array_map(strtoupper(...), explode('|', $methods))
|
|
|
|
)) return;
|
|
|
|
|
|
|
|
// check if the route matches and add parameters if any
|
|
|
|
$args = [];
|
2023-01-02 14:38:51 +00:00
|
|
|
$request_path = (function() {
|
2023-01-02 16:02:26 +00:00
|
|
|
$haystack = strtok($_SERVER['REQUEST_URI'], '?');
|
2023-01-02 14:38:51 +00:00
|
|
|
$needle = dirname($_SERVER['SCRIPT_NAME']);
|
|
|
|
if ($needle === '/') {
|
|
|
|
$needle = '';
|
|
|
|
}
|
|
|
|
$pos = strpos($haystack, $needle);
|
|
|
|
return substr_replace($haystack, '', $pos, strlen($needle));
|
|
|
|
})();
|
2022-11-29 12:49:29 +00:00
|
|
|
$path_parts = explode('/', $path);
|
2023-01-02 14:38:51 +00:00
|
|
|
$request_path_parts = explode('/', $request_path);
|
2022-11-29 12:49:29 +00:00
|
|
|
for ($i=0; $i < count($path_parts); $i++) {
|
|
|
|
if (empty($path_parts[$i])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($path_parts[$i][0] === '$') {
|
|
|
|
if (!isset($request_path_parts[$i])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($path_parts[$i][-1] !== '?' && empty($request_path_parts[$i])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!empty($request_path_parts[$i])) {
|
|
|
|
$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);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:33:45 +00:00
|
|
|
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); }
|
2022-11-29 12:49:29 +00:00
|
|
|
|
|
|
|
public static function middleware(callable|array $middlewares)
|
|
|
|
{
|
|
|
|
if (!is_array($middlewares)) {
|
|
|
|
$middlewares = [$middlewares];
|
|
|
|
}
|
|
|
|
foreach ($middlewares as $middleware) {
|
|
|
|
self::$groups[(count(self::$groups) - 1)][] = $middleware;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function group(callable $callback)
|
|
|
|
{
|
|
|
|
self::$groups[] = [];
|
|
|
|
$callback();
|
|
|
|
array_pop(self::$groups);
|
|
|
|
}
|
|
|
|
}
|