diff options
| author | winter | 2024-12-07 21:44:42 +0000 |
|---|---|---|
| committer | winter | 2024-12-07 21:44:42 +0000 |
| commit | a84232811dadccf7047424f27340a7f9847bedff (patch) | |
| tree | a6a0823c8b9485b6a5b60ea7ae854f9511a6ae1e /WpfTest/Router.php | |
| parent | e1ad307b531c27ec6bfaf8b4d018991b0d4a78f3 (diff) | |
many many changes but actors are loaded from db now
Diffstat (limited to 'WpfTest/Router.php')
| -rw-r--r-- | WpfTest/Router.php | 29 |
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); } } |
