Tabs to spaces

This commit is contained in:
William 2023-03-21 21:51:12 +01:00
parent d60e072010
commit 74bd080709

150
Route.php
View File

@ -4,85 +4,85 @@ namespace WillySoft;
abstract class Route abstract class Route
{ {
static $prefix = ''; static $prefix = '';
static $groups = [[]]; static $groups = [[]];
static function match(string $methods, string $uri, callable $callback) static function match(string $methods, string $uri, callable $callback)
{ {
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_uri_parts = explode('/', urldecode( $request_uri_parts = explode('/', urldecode(
strtok($_SERVER['REQUEST_URI'], '?') strtok($_SERVER['REQUEST_URI'], '?')
)); ));
$uri_parts = explode('/', self::$prefix . $uri); $uri_parts = explode('/', self::$prefix . $uri);
$callback_args = []; $callback_args = [];
for ($i = 0; $i < count($uri_parts); $i++) { for ($i = 0; $i < count($uri_parts); $i++) {
if ($uri_parts[$i] === '') if ($uri_parts[$i] === '')
continue; continue;
if ($uri_parts[$i][0] !== '$') if ($uri_parts[$i][0] !== '$')
continue; continue;
if (!isset($request_uri_parts[$i])) if (!isset($request_uri_parts[$i]))
return; return;
if ( if (
$uri_parts[$i][-1] !== '?' $uri_parts[$i][-1] !== '?'
&& $request_uri_parts[$i] === '' && $request_uri_parts[$i] === ''
) return; ) return;
if ($request_uri_parts[$i] !== '') if ($request_uri_parts[$i] !== '')
array_push($callback_args, $request_uri_parts[$i]); array_push($callback_args, $request_uri_parts[$i]);
$request_uri_parts[$i] = $uri_parts[$i]; $request_uri_parts[$i] = $uri_parts[$i];
} }
if ( if (
implode('/', $uri_parts) implode('/', $uri_parts)
!== implode('/', $request_uri_parts) !== implode('/', $request_uri_parts)
) return; ) return;
foreach (self::$groups as $middlewares) { foreach (self::$groups as $middlewares) {
foreach ($middlewares as $middleware) { foreach ($middlewares as $middleware) {
$middleware(); $middleware();
} }
} }
$callback(...$callback_args); $callback(...$callback_args);
die(); die();
} }
static function get(string $uri, callable $callback) static function get(string $uri, callable $callback)
{self::match('get', $uri, $callback);} {self::match('get', $uri, $callback);}
static function post(string $uri, callable $callback) static function post(string $uri, callable $callback)
{self::match('post', $uri, $callback);} {self::match('post', $uri, $callback);}
static function put(string $uri, callable $callback) static function put(string $uri, callable $callback)
{self::match('put', $uri, $callback);} {self::match('put', $uri, $callback);}
static function patch(string $uri, callable $callback) static function patch(string $uri, callable $callback)
{self::match('patch', $uri, $callback);} {self::match('patch', $uri, $callback);}
static function delete(string $uri, callable $callback) static function delete(string $uri, callable $callback)
{self::match('delete', $uri, $callback);} {self::match('delete', $uri, $callback);}
static function options(string $uri, callable $callback) static function options(string $uri, callable $callback)
{self::match('options', $uri, $callback);} {self::match('options', $uri, $callback);}
static function form(string $uri, callable $callback) static function form(string $uri, callable $callback)
{self::match('get|post', $uri, $callback);} {self::match('get|post', $uri, $callback);}
static function any(string $uri, callable $callback) static function any(string $uri, callable $callback)
{self::match('get|post|put|patch|delete|options', $uri, $callback);} {self::match('get|post|put|patch|delete|options', $uri, $callback);}
static function use(callable $middleware) static function use(callable $middleware)
{ {
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) static function group(string $prefix = '', ?callable $callback = null)
{ {
if (!str_starts_with( if (!str_starts_with(
$_SERVER['REQUEST_URI'], $_SERVER['REQUEST_URI'],
self::$prefix . $prefix self::$prefix . $prefix
)) return; )) return;
self::$prefix = self::$prefix . $prefix; self::$prefix = self::$prefix . $prefix;
array_push(self::$groups, []); array_push(self::$groups, []);
$callback(); $callback();
array_pop(self::$groups); array_pop(self::$groups);
self::$prefix = rtrim(self::$prefix, $prefix); self::$prefix = rtrim(self::$prefix, $prefix);
} }
} }