44 lines
1.0 KiB
PHP
44 lines
1.0 KiB
PHP
|
<?php
|
||
|
|
||
|
class Request
|
||
|
{
|
||
|
public array $uri;
|
||
|
public string $method;
|
||
|
public array $headers;
|
||
|
public int $status;
|
||
|
|
||
|
public bool $htmx;
|
||
|
|
||
|
public function __construct()
|
||
|
{
|
||
|
// Get the URI and trim slashes, then break it into pieces
|
||
|
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
|
||
|
$uri = '/'.trim(trim($uri), '/');
|
||
|
$this->uri = explode('/', $uri);
|
||
|
array_shift($this->uri);
|
||
|
|
||
|
// Get the request method
|
||
|
$this->method = $_SERVER['REQUEST_METHOD'];
|
||
|
|
||
|
// Get all headers
|
||
|
$this->headers = $this->getAllHeaders();
|
||
|
}
|
||
|
|
||
|
public function uri(int $index): string|false
|
||
|
{
|
||
|
if ($index == 0 && empty($this->uri[0])) return '/';
|
||
|
return $this->uri[$index] ?? false;
|
||
|
}
|
||
|
|
||
|
private function getAllHeaders(): array
|
||
|
{
|
||
|
$headers = [];
|
||
|
foreach ($_SERVER as $k => $v) {
|
||
|
if (substr($k, 0, 5) == 'HTTP_') {
|
||
|
$headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($k, 5)))))] = $v;
|
||
|
}
|
||
|
}
|
||
|
return $headers;
|
||
|
}
|
||
|
}
|