2
0
Web/Context.php
2025-09-11 11:36:46 -05:00

337 lines
6.6 KiB
PHP

<?php
namespace Web;
/**
* Context holds the request, response, and shared state for a request
*/
class Context
{
public Request $request;
public Response $response;
public Session $session;
public Cookies $cookie;
public array $state = [];
/**
* __construct creates a new Context with request and response
*/
public function __construct(?Cookies $cookie = null)
{
$this->request = new Request();
$this->response = new Response();
$this->session = new Session();
$this->session->start();
$this->cookie = $cookie ?: new Cookies();
$this->response->setCookieManager($this->cookie);
}
/**
* set stores a value in the context state
*/
public function set(string $key, mixed $value): void
{
$this->state[$key] = $value;
}
/**
* get retrieves a value from the context state
*/
public function get(string $key): mixed
{
return $this->state[$key] ?? null;
}
/**
* json sends a JSON response
*/
public function json(mixed $data, int $status = 200): void
{
$this->response->json($data, $status)->send();
}
/**
* text sends a plain text response
*/
public function text(string $text, int $status = 200): void
{
$this->response->text($text, $status)->send();
}
/**
* html sends an HTML response
*/
public function html(string $html, int $status = 200): void
{
$this->response->html($html, $status)->send();
}
/**
* redirect sends a redirect response
*/
public function redirect(string $url, int $status = 302): void
{
$this->response->redirect($url, $status)->send();
}
/**
* error sends an error response with appropriate content type
*/
public function error(int $status, string $message = ''): void
{
$messages = [
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
500 => 'Internal Server Error',
502 => 'Bad Gateway',
503 => 'Service Unavailable'
];
$message = $message ?: ($messages[$status] ?? 'Error');
$this->response->status($status);
if ($this->request->header('accept') && str_contains($this->request->header('accept'), 'application/json')) {
$this->json(['error' => $message], $status);
} else {
$this->text($message, $status);
}
}
/**
* validate validates input data against rules
*/
public function validate(array $data, array $rules, array $messages = []): Validator
{
$validator = new Validator();
$validator->validate($data, $rules, $messages);
if ($validator->failed()) {
throw new ValidationException($validator->errors());
}
return $validator;
}
/**
* validateRequest validates request data against rules
*/
public function validateRequest(array $rules, array $messages = []): Validator
{
$data = $this->request->all();
return $this->validate($data, $rules, $messages);
}
/**
* header returns the value of a request header
*/
public function header(string $name): ?string
{
return $this->request->header($name);
}
/**
* input returns a form input value (POST data)
*/
public function input(string $name, mixed $default = null): mixed
{
return $this->request->input($name, $default);
}
/**
* query returns a query parameter value
*/
public function query(string $name, mixed $default = null): mixed
{
return $this->request->query($name, $default);
}
/**
* jsonValue returns a value from JSON body
*/
public function jsonValue(string $name, mixed $default = null): mixed
{
return $this->request->jsonValue($name, $default);
}
/**
* param returns a route parameter by integer index
*/
public function param(int $index, mixed $default = null): mixed
{
return $this->request->param($index, $default);
}
/**
* all returns all input data merged from all sources
*/
public function all(): array
{
return $this->request->all();
}
/**
* only returns only specified keys from input
*/
public function only(array $keys): array
{
return $this->request->only($keys);
}
/**
* except returns all input except specified keys
*/
public function except(array $keys): array
{
return $this->request->except($keys);
}
/**
* has checks if input key exists
*/
public function has(string $key): bool
{
return $this->request->has($key);
}
/**
* cookie returns the value of a request cookie
*/
public function cookie(string $name): ?string
{
return $this->request->cookie($name);
}
/**
* expectsJson checks if request expects JSON response
*/
public function expectsJson(): bool
{
return $this->request->expectsJson();
}
/**
* isAjax checks if request is AJAX
*/
public function isAjax(): bool
{
return $this->request->isAjax();
}
/**
* ip returns the client IP address
*/
public function ip(): string
{
return $this->request->ip();
}
/**
* userAgent returns the user agent string
*/
public function userAgent(): string
{
return $this->request->userAgent();
}
/**
* referer returns the referer URL
*/
public function referer(): ?string
{
return $this->request->referer();
}
/**
* isSecure checks if request is over HTTPS
*/
public function isSecure(): bool
{
return $this->request->isSecure();
}
/**
* url returns the full URL of the request
*/
public function url(): string
{
return $this->request->url();
}
/**
* fullUrl returns the URL with query string
*/
public function fullUrl(): string
{
return $this->request->fullUrl();
}
/**
* is checks if the request path matches a pattern
*/
public function is(string $pattern): bool
{
return $this->request->is($pattern);
}
/**
* contentType returns the request content type without charset
*/
public function contentType(): string
{
return $this->request->contentType();
}
/**
* status sets the HTTP status code for the response
*/
public function status(int $code): Response
{
return $this->response->status($code);
}
/**
* setHeader sets a header for the response
*/
public function setHeader(string $name, string $value): Response
{
return $this->response->header($name, $value);
}
/**
* setCookie sets a cookie with the given name, value, and options
*/
public function setCookie(string $name, string $value, array $options = []): Response
{
return $this->response->cookie($name, $value, $options);
}
/**
* write adds content to the response body
*/
public function write(string $content): Response
{
return $this->response->write($content);
}
/**
* end ends the response with optional content
*/
public function end(string $content = ''): void
{
$this->response->end($content);
}
/**
* send sends the response to the client
*/
public function send(): void
{
$this->response->send();
}
}