diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/controllers/GET.php b/controllers/GET.php new file mode 100644 index 0000000..39ad8dc --- /dev/null +++ b/controllers/GET.php @@ -0,0 +1,5 @@ +addRoute(GET, "/", fromController("/GET")); + $router->addRoute(GET, "/test", useRenderer(fromController("/test/GET"), "JSON")); +}; diff --git a/controllers/test/GET.php b/controllers/test/GET.php new file mode 100644 index 0000000..5b1fcc3 --- /dev/null +++ b/controllers/test/GET.php @@ -0,0 +1,7 @@ + "Hello World!", + ]); +}; diff --git a/core.php b/core.php new file mode 100644 index 0000000..d49d312 --- /dev/null +++ b/core.php @@ -0,0 +1,17 @@ +execute(); +} diff --git a/html/.htaccess b/html/.htaccess new file mode 100644 index 0000000..c6a799b --- /dev/null +++ b/html/.htaccess @@ -0,0 +1,10 @@ + +RewriteEngine On +RewriteBase / + +RewriteRule ^index\.php$ - [L] +RewriteCond %{REQUEST_URI} !^/static/ +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteRule . /index.php [L] + diff --git a/html/index.php b/html/index.php new file mode 100644 index 0000000..bee614d --- /dev/null +++ b/html/index.php @@ -0,0 +1,3 @@ + require(__DIR__ . "/JSON.php"), + "DEBUG" => require(__DIR__ . "/debug.php"), +]; diff --git a/router/Router.php b/router/Router.php new file mode 100644 index 0000000..4d11921 --- /dev/null +++ b/router/Router.php @@ -0,0 +1,70 @@ +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(); \ No newline at end of file diff --git a/templates/404.php b/templates/404.php new file mode 100644 index 0000000..1bcdf15 --- /dev/null +++ b/templates/404.php @@ -0,0 +1 @@ +404 Not found \ No newline at end of file diff --git a/templates/maintenance.php b/templates/maintenance.php new file mode 100644 index 0000000..bc7e662 --- /dev/null +++ b/templates/maintenance.php @@ -0,0 +1 @@ +We are currently in maintenance mode. \ No newline at end of file diff --git a/utils/arrays.php b/utils/arrays.php new file mode 100644 index 0000000..1ca50ea --- /dev/null +++ b/utils/arrays.php @@ -0,0 +1,18 @@ +