From bb0a392af9fa9f5af90a69293672b3139af41246 Mon Sep 17 00:00:00 2001 From: NaysKutzu <87282334+NaysKutzu@users.noreply.github.com> Date: Tue, 9 Jul 2024 11:43:28 +0200 Subject: [PATCH] PUSH -> Fix router -> Added more api things :) --- src/MythicalSystems/Api/Api.php | 22 ++++++++++++++++++++++ src/MythicalSystems/Api/Methods.php | 16 ++++++++++++++++ src/MythicalSystems/Database/SQLite.php | 3 ++- src/Router/Route.php | 5 ++--- src/Router/Router.php | 6 +++--- 5 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/MythicalSystems/Api/Methods.php diff --git a/src/MythicalSystems/Api/Api.php b/src/MythicalSystems/Api/Api.php index 8f94a22..5f376ef 100644 --- a/src/MythicalSystems/Api/Api.php +++ b/src/MythicalSystems/Api/Api.php @@ -111,4 +111,26 @@ public static function allowOnlyHEAD(): void ResponseHandler::MethodNotAllowed("Please use a HEAD request to access this endpoint!",null); } } + + public static function allowOnly(Methods $method) { + if ($_SERVER['REQUEST_METHOD'] !== $method) { + ResponseHandler::MethodNotAllowed("Please use a " . $method . " request to access this endpoint!",null); + } + } + + /** + * Get the request method + * + * @return string|null|array + */ + public static function getRequestMethod() : string|null|array { + if ($_SERVER['REQUEST_METHOD'] == null) { + return null; + } else if ($_SERVER['REQUEST_METHOD'] == "") { + return null; + } + else { + return $_SERVER['REQUEST_METHOD']; + } + } } \ No newline at end of file diff --git a/src/MythicalSystems/Api/Methods.php b/src/MythicalSystems/Api/Methods.php new file mode 100644 index 0000000..5a896a4 --- /dev/null +++ b/src/MythicalSystems/Api/Methods.php @@ -0,0 +1,16 @@ +bindValue('?' . ++$i, $param); } } diff --git a/src/Router/Route.php b/src/Router/Route.php index e4b6cb4..6128d08 100644 --- a/src/Router/Route.php +++ b/src/Router/Route.php @@ -16,14 +16,13 @@ class Route private $callback; /** @var array The matches of $expr, which will be the arguments of the callback */ private $matches; - /** @var Allowed methods for this route */ private $methods = array('GET', 'POST', 'HEAD', 'PUT', 'DELETE'); /** * Constructor * * @param string $expr regular expression to test against - * @param function $callback function executed if route matches + * @param callable $callback function executed if route matches * @param string|array $methods methods allowed */ public function __construct($expr, $callback, $methods = null) @@ -48,7 +47,7 @@ public function matches($path) : bool { if ( preg_match($this->expr, $path, $this->matches) && - in_array($_SERVER['REQUEST_METHOD'], $this->methods) + in_array($_SERVER['REQUEST_METHOD'], (array) $this->methods) ) { return true; } diff --git a/src/Router/Router.php b/src/Router/Router.php index 520d5df..7d8e67c 100644 --- a/src/Router/Router.php +++ b/src/Router/Router.php @@ -67,7 +67,7 @@ public function add(string $expr, callable $callback, null|array $methods = nul * * @return void */ - public function get(string $expr, callble $callback) : void + public function get(string $expr, callable $callback) : void { $this->routes[] = new Route($expr, $callback, 'GET'); } @@ -165,7 +165,7 @@ public function url(string $path = null) : string * * @return void */ - public function redirectfrom($from_path, $to_path, $code = 302) : void + public function RedirectFrom($from_path, $to_path, $code = 302) : void { $this->all($from_path, function () use ($to_path, $code) { http_response_code($code); @@ -180,7 +180,7 @@ public function redirectfrom($from_path, $to_path, $code = 302) : void * * @return void */ - public function redirectto(string $to_path, int $code = 302) : void + public function RedirectTo(string $to_path, int $code = 302) : void { }