summaryrefslogtreecommitdiff
path: root/web/core/router.php
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-03-30 12:14:42 -0400
committerFreya Murphy <freya@freyacat.org>2024-03-30 12:14:42 -0400
commit1f04b83be337cc91a3fabcf4e574e2306f3d2eaa (patch)
tree74d7d65a7047e60d1877384e3c7b0d70c7b0e49a /web/core/router.php
parentstart database (user and post), and initial barebones home page (diff)
downloadxssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.gz
xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.tar.bz2
xssbook2-1f04b83be337cc91a3fabcf4e574e2306f3d2eaa.zip
refactor
Diffstat (limited to 'web/core/router.php')
-rw-r--r--web/core/router.php192
1 files changed, 106 insertions, 86 deletions
diff --git a/web/core/router.php b/web/core/router.php
index 6ee28a9..72c7674 100644
--- a/web/core/router.php
+++ b/web/core/router.php
@@ -1,127 +1,147 @@
<?php /* Copyright (c) 2024 Freya Murphy */
class Router {
- private $main;
+ // the loader
private $load;
- private $routes;
-
- function load_route($route) {
- $name = $route['name'];
- $controller_cls = $route['controller'];
- $model_cls = $route['model'];
-
- $root = $GLOBALS['webroot'];
- $dir = $root . '/routes/' . $name;
- require($dir . '/model.php');
- require($dir . '/controller.php');
-
- $model_ref = new ReflectionClass($model_cls);
- $model = $model_ref->newInstance();
- $controller_ref = new ReflectionClass($controller_cls);
- $controller = $controller_ref->newInstance($model);
+ // the main model
+ private $main;
- return $controller;
+ /**
+ * Creates a router
+ * @param Loader $load - the main laoder object
+ */
+ function __construct($load) {
+ $this->load = $load;
+ $this->main = $this->load->model('main');
}
- function __construct($main, $load) {
+ /**
+ * @param string $path - the current request path
+ * Gets the current route
+ * @return array<string,mixed>
+ */
+ private function get_req_route($path): array {
+ // trim the path
+ $path = trim($path);
+ // remove first '/'
+ $path = substr($path, 1);
+ // get path parts
+ $parts = explode('/', $path);
- $routes = array(
- 'home' => array(
- 'slugs' => ['', 'home'],
- 'model' => 'HomeModel',
- 'controller' => 'HomeController',
- ),
- );
+ $len = count($parts);
- $this->routes = array();
- foreach ($routes as $name => $route) {
- foreach ($route['slugs'] as $slug) {
- $this->routes[$slug] = $route;
- $this->routes[$slug]['name'] = $name;
- }
+ // get route info
+ $route = array();
+ // e.g. /
+ if ($path === '') {
+ $route = array(
+ 'route' => '',
+ 'slug' => 'index',
+ );
+ // e.g. /home /login
+ } else if ($len === 1) {
+ $route = array(
+ 'route' => $parts[0],
+ 'slug' => 'index',
+ );
+ // e.g. /home/posts
+ } else {
+ $route = array (
+ 'route' => implode('/', array_slice($parts, 0, -1)),
+ 'slug' => end($parts)
+ );
+ };
+
+ $route['app'] = $route['route'];
+ $routes = $GLOBALS['routes'];
+ if (array_key_exists($route['route'], $routes)) {
+ $route['route'] = $routes[$route['route']];
}
- $this->main = $main;
- $this->load = $load;
+ return $route;
}
- function get_info() {
- $uri = parse_url($_SERVER['REQUEST_URI']);
+ /**
+ * Gets the curret request info
+ * @return array<string,mixed>
+ */
+ private function get_req(): array {
$method = $_SERVER['REQUEST_METHOD'];
- $parts = explode('/', $uri['path']);
-
- $slug = sizeof($parts) > 1 ? $parts[1] : '';
- $path = sizeof($parts) > 2 ? $parts[2] : 'index';
- if (sizeof($parts) > 3) {
- return NULL;
- }
-
- return array(
- 'method' => $method,
- 'uri' => $uri,
+ $uri = parse_url($_SERVER['REQUEST_URI']);
+ $path = $uri['path'];
- 'slug' => $slug,
- 'path' => $path
+ return array_merge(
+ array(
+ 'uri' => $uri,
+ 'method' => $method,
+ 'lang' => $this->get_lang(),
+ ),
+ $this->get_req_route($path),
);
}
- function handle_error($code) {
- $route = array(
- 'name' => 'error',
- 'model' => 'ErrorModel',
- 'controller' => 'ErrorController'
- );
- $this->main->info = array(
- 'slug' => 'error',
- 'lang' => 'en_US',
- 'route' => 'error'
- );
- $controller = $this->load_route($route);
- $_GET['code'] = $code;
- http_response_code($code);
- $controller->index();
+ /**
+ * Gets the current language
+ * @return string
+ */
+ private function get_lang(): string {
+ return 'en_US';
}
- public function handle_request() {
- $request = $this->get_info();
-
- if ($request === NULL) {
- $this->handle_error(404);
- return;
+ /**
+ * Handles a router error code
+ * @param int $code - the http error code
+ * @param bool $recursed
+ */
+ private function handle_error($code, $recursed): void {
+ if ($recursed) {
+ die($code . ' (recursed)');
}
- $slug = $request['slug'];
- if (!array_key_exists($slug, $this->routes)) {
- $this->handle_error(404);
- return;
- }
+ $this->main->info['slug'] = 'index';
+ $this->main->info['app'] = 'error';
+ $this->main->info['route'] = 'apps/error';
+ $req = $this->main->info;
+ $_GET['code'] = $code;
- $route = $this->routes[$slug];
- $this->main->info = array(
- 'lang' => 'en_US',
- 'slug' => $slug,
- 'route' => $route['name'],
- );
+ $this->handle_req($req, TRUE);
+ }
- $controller = $this->load_route($route);
+ /**
+ * @param array $req
+ * @param bool $recursed
+ */
+ private function handle_req($req, $recursed = FALSE): void {
+ $controller = $this->load->controller($req['route']);
- $path = $request['path'];
- $ref = NULL;
+ if ($controller === NULL) {
+ $this->handle_error(404, $recursed);
+ return;
+ }
+ $ref = NULL;
try {
- $ref = new ReflectionMethod($controller, $path);
+ $ref = new ReflectionMethod($controller, $req['slug']);
} catch (Exception $_e) {}
if ($ref === NULL || !$ref->isPublic()) {
- $this->handle_error(404);
+ $this->handle_error(404, $recursed);
return;
}
$ref->invoke($controller);
+ }
+ /**
+ * Handels the incomming reuqest
+ */
+ public function handle_request(): void {
+ $req = $this->get_req();
+ $this->main->info = $req;
+ $this->handle_req($req);
}
}