Skip to content

Commit

Permalink
PUSH
Browse files Browse the repository at this point in the history
-> Fix router
-> Added more api things :)
  • Loading branch information
NaysKutzu committed Jul 9, 2024
1 parent ecc5062 commit bb0a392
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
22 changes: 22 additions & 0 deletions src/MythicalSystems/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
}
16 changes: 16 additions & 0 deletions src/MythicalSystems/Api/Methods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MythicalSystems\Api;

interface Methods
{
public static const get="GET";
public static const post="POST";
public static const put="PUT";
public static const delete="DELETE";
public static const head="HEAD";
public static const options="OPTIONS";
public static const patch="PATCH";
public static const trace="TRACE";
public static const connect="CONNECT";
}
3 changes: 2 additions & 1 deletion src/MythicalSystems/Database/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class SQLite extends Connection
{
/**
/**
* Execute a database query
*
* @param string $query The SQL query
Expand All @@ -24,6 +24,7 @@ private static function executeQuery(string $query, array $params = [], bool $re

if (!empty($params)) {
foreach ($params as $param) {
$i = 0;
$stmt->bindValue('?' . ++$i, $param);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down Expand Up @@ -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);
Expand All @@ -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
{

}
Expand Down

0 comments on commit bb0a392

Please sign in to comment.