routesDir = $routesDir; $this->config = $config; } function registerStatusHandler(int $status, callable $handler) { $this->statusHandlers[$status] = $handler; } private function dealWithError(int $status, string $path) { http_response_code($status); if (isset($this->statusHandlers[$status])) { $this->statusHandlers[$status]($path); } else { echo '
' . $status . ' for ' . htmlspecialchars($path) . '
'; } } function dispatch(string $path) { if (str_contains($path, '..')) { $this->dealWithError(404, $path); return; } if (file_exists($this->routesDir . $path . '.php')) { require_once $this->routesDir . $path . '.php'; } elseif (file_exists($this->routesDir . $path . 'index.php')) { require_once $this->routesDir . $path . 'index.php'; } else { $this->dealWithError(404, $path); return; } if (function_exists($_SERVER['REQUEST_METHOD'])) { $_SERVER['REQUEST_METHOD']($this->config); } else { $this->dealWithError(405, $path); } } }