method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET'); $uri = $_SERVER['REQUEST_URI'] ?? '/'; $this->path = parse_url($uri, PHP_URL_PATH) ?: '/'; $this->queryParams = $_GET ?? []; $this->headers = $this->extractHeaders(); $this->rawBody = file_get_contents('php://input') ?: ''; $contentType = $this->getHeader('content-type'); if (str_contains($contentType, 'application/json') && !empty($this->rawBody)) { $decoded = json_decode($this->rawBody, true); $this->body = is_array($decoded) ? $decoded : []; } else { $this->body = $_POST ?? []; } } private function extractHeaders(): array { $headers = []; foreach ($_SERVER as $key => $value) { if (str_starts_with($key, 'HTTP_')) { $headerName = strtolower(str_replace('_', '-', substr($key, 5))); $headers[$headerName] = (string)$value; } elseif (in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'], true)) { $headerName = strtolower(str_replace('_', '-', $key)); $headers[$headerName] = (string)$value; } } return $headers; } public function getMethod(): string { return $this->method; } public function getPath(): string { return $this->path; } public function getHeader(string $name, string $default = ''): string { $name = strtolower($name); return $this->headers[$name] ?? $default; } public function getHeaders(): array { return $this->headers; } public function getQueryParams(): array { return $this->queryParams; } public function getQuery(string $key, $default = null) { return $this->queryParams[$key] ?? $default; } public function getBody(): array { return $this->body; } public function get(string $key, $default = null) { return $this->body[$key] ?? $default; } public function getRawBody(): string { return $this->rawBody; } public function getIp(): string { if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) { return $_SERVER['HTTP_CF_CONNECTING_IP']; } if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); return trim($ips[0]); } return $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1'; } public function getUserAgent(): string { return $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown'; } }