aboutsummaryrefslogtreecommitdiffhomepage
path: root/WpfTest/Router.php
diff options
context:
space:
mode:
Diffstat (limited to 'WpfTest/Router.php')
-rw-r--r--WpfTest/Router.php29
1 files changed, 13 insertions, 16 deletions
diff --git a/WpfTest/Router.php b/WpfTest/Router.php
index ea71657..c975723 100644
--- a/WpfTest/Router.php
+++ b/WpfTest/Router.php
@@ -1,51 +1,48 @@
<?php
namespace WpfTest;
-use WpfTest\HttpReturnStatus\InternalServerError;
-use WpfTest\HttpReturnStatus\NoHandlerFound;
+use WpfTest\HttpResponseStatus\InternalServerError;
+use WpfTest\HttpResponseStatus\NoHandlerFound;
-class Router {
- private static Router $instance;
+class Router extends Singleton {
private readonly string $requestPath;
private array $routes;
- private function __construct() {
+ protected function __construct() {
$this->requestPath = $_GET['requestPath'] ?? $_SERVER['PATH_INFO'] ?? explode('?', $_SERVER['REQUEST_URI'], 2)[0] ?? '/';
$this->routes = [];
}
/**
- * @return Router the singleton instance
+ * Processes and sends a response to the current request
+ * @return void
*/
- public static function getInstance() {
- if (!isset(self::$instance)) {
- self::$instance = new self();
- }
- return self::$instance;
- }
-
public function processRequest() {
try {
$this->processRequestInner();
- } catch (HttpReturnStatus $e) {
+ } catch (HttpResponseStatus $e) {
http_response_code($e->httpCode());
echo '<h1>' . $e->httpReason() . '</h1><pre>' . $e->getMessage() . '</pre>';
}
}
private function processRequestInner() {
+ $context = null;
try {
$route = $this->matchPath($this->requestPath);
if ($route == null) {
throw new NoHandlerFound($this->requestPath);
}
+ $context = 'handler for /' . implode('/', $route[0]);
$route[1]($route[2]); // call handler with args
} catch (\Exception $e) {
- if (is_a($e, HttpReturnStatus::class)) {
+ if (is_a($e, HttpResponseStatus::class)) {
throw $e;
}
- throw new InternalServerError($e);
+ throw new InternalServerError($e, $context);
+ } catch (\Error $e) {
+ throw new InternalServerError($e, $context);
}
}