feat: Add basic router

This commit is contained in:
overflowerror 2023-11-22 21:25:33 +01:00
parent bcd67b1802
commit f2f5cb9491
16 changed files with 183 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea

5
controllers/GET.php Normal file
View file

@ -0,0 +1,5 @@
<?php
return function (array $context) {
echo "Hello World";
};

14
controllers/routes.php Normal file
View file

@ -0,0 +1,14 @@
<?php
require_once(ROOT . "/middleware/renderer.php");
function fromController(string $path) {
return function(array $context) use ($path) {
return (require(ROOT . "/controllers/" . $path . ".php"))($context);
};
}
return function(Router $router) {
$router->addRoute(GET, "/", fromController("/GET"));
$router->addRoute(GET, "/test", useRenderer(fromController("/test/GET"), "JSON"));
};

7
controllers/test/GET.php Normal file
View file

@ -0,0 +1,7 @@
<?php
return function (array $context) {
$context["renderer"]([
"value" => "Hello World!",
]);
};

17
core.php Normal file
View file

@ -0,0 +1,17 @@
<?php
const ROOT = __DIR__;
define("MAINTENANCE_MODE", require(ROOT . "/maintenance.php"));
require_once(ROOT . "/utils/arrays.php");
if (MAINTENANCE_MODE) {
require(ROOT . "./templates/maintenance.php");
} else {
$router = require(ROOT . "/router/Router.php");
(require(ROOT . "/controllers/routes.php"))($router);
$router->execute();
}

10
html/.htaccess Normal file
View file

@ -0,0 +1,10 @@
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/static/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

3
html/index.php Normal file
View file

@ -0,0 +1,3 @@
<?php
require("../core.php");

3
maintenance.php Normal file
View file

@ -0,0 +1,3 @@
<?php
return false;

17
middleware/renderer.php Normal file
View file

@ -0,0 +1,17 @@
<?php
function useRenderer($handler, string $default, string $query_param = "format") {
return function (array $context) use ($handler, $default, $query_param) {
$rendererMap = require(ROOT . "/renderer/renderer.php");
$format = array_default($query_param, $_GET);
if (!$format || !key_exists(strtoupper($format), $rendererMap)) {
$format = $default;
}
$context["renderer"] = $rendererMap[strtoupper($format)];
$handler($context);
};
}

5
renderer/JSON.php Normal file
View file

@ -0,0 +1,5 @@
<?php
return function($data) {
echo json_encode($data);
};

5
renderer/debug.php Normal file
View file

@ -0,0 +1,5 @@
<?php
return function($data) {
var_dump($data);
};

6
renderer/renderer.php Normal file
View file

@ -0,0 +1,6 @@
<?php
return [
"JSON" => require(__DIR__ . "/JSON.php"),
"DEBUG" => require(__DIR__ . "/debug.php"),
];

70
router/Router.php Normal file
View file

@ -0,0 +1,70 @@
<?php
const GET = "GET";
const POST = "POST";
const PUT = "PUT";
const DELETE = "DELETE";
const HEAD = "HEAD";
const CONNECT = "CONNECT";
const OPTIONS = "OPTIONS";
const TRACE = "TRACE";
const PATCH = "PATCH";
class Router {
private $routes = [];
public $notFoundHandler;
function __construct() {
$this->notFoundHandler = function($context) {
header("HTTP/", true, 404);
require(ROOT . "/templates/404.php");
};
}
private function findRoute(string $method, string $url) {
if (!key_exists($method, $this->routes)) {
return null;
}
$paths = $this->routes[$method];
foreach ($paths as $path => $handler) {
if (preg_match("/^" . str_replace("/", "\/", $path, ) . "$/", $url)) {
return $handler;
}
}
return null;
}
private function getPath($uri) {
if (($i = strpos($uri, "?")) !== false) {
return substr($uri, 0, $i);
} else {
return $uri;
}
}
public function addRoute(string $method, string $path, $handler) {
array_get_or_add($method, $this->routes, [])[$path] = $handler;
}
public function execute() {
//var_dump($this->routes);
$path = $this->getPath($_SERVER["REQUEST_URI"]);
$route = $this->findRoute($_SERVER["REQUEST_METHOD"], $path);
if (!$route) {
$route = $this->notFoundHandler;
}
$context = [
"REQUEST_PATH" => $path,
];
$route($context);
}
}
return new Router();

1
templates/404.php Normal file
View file

@ -0,0 +1 @@
404 Not found

View file

@ -0,0 +1 @@
We are currently in maintenance mode.

18
utils/arrays.php Normal file
View file

@ -0,0 +1,18 @@
<?php
function &array_get_or_add($needle, array &$haystack, $default=null) {
if (key_exists($needle, $haystack)) {
return $haystack[$needle];
} else {
$haystack[$needle] = $default;
return $haystack[$needle];
}
}
function array_default($needle, array $haystack, $default=null) {
if (key_exists($needle, $haystack)) {
return $haystack[$needle];
} else {
return $default;
}
}