Repository navigation
Internals Response Response
Framework Internals · Subsystem overview · Developer guide
Source: awt_src/classes/response/Response.php · Snapshot 8411f4287706.
Response builds status, headers and data/file payloads, chooses a MIME sender, and emits HTTP output. JSON arrays are wrapped in a status/message/data envelope.
use response\interface\ResponseInterface;
use response\interface\SenderInterface;
use response\registry\SenderRegistry;
use response\resolvers\MimeResolver;
use response\senders\{
AudioSender,
BinarySender,
CssSender,
HtmlSender,
ImageSender,
JsSender,
JsonSender,
PdfSender,
TextSender,
VideoSender,
XmlSender
};class Response implements ResponseInterfaceSource description (behavioral corrections are recorded in Known discrepancies):
Fluent HTTP Response Facade.
Instantiate via the static factory or new directly.
Examples ────────
// Auto-resolve sender from the request's Accept header: Response::make(200)->data(['users' => []])->send();
// Explicit type helpers: Response::make(200)->data(['ok' => true])->asJson()->send(); Response::make(200)->file('/var/www/clip.mp4')->send(); Response::make(201)->data(['id' => 42])->asXml()->send();
// Custom header + redirect: Response::make(302)->header('Location', '/home')->send(); Response::make()->redirect('/login');
Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.
| Member | Declaration | Purpose or source contract |
|---|---|---|
| $statusCode | private int $statusCode = 200; |
Used by make(), status(), send(). |
| $statusMessage | private string $statusMessage = 'OK'; |
Used by make(), message(), send(). |
| $headers | private array $headers = []; |
Used by header(), send(). |
| $payload | private mixed $payload = null; |
Used by data(), file(), send(), resolveMime(). |
| $forcedMime | private ?string $forcedMime = null; |
Used by as(), resolveMime(). |
| $registry | private static ?SenderRegistry $registry = null; |
Used by registry(), setRegistry(). |
| $resolver | private static ?MimeResolver $resolver = null; |
Used by resolver(). |
| Method | Signature |
|---|---|
| make | public static function make(int $status = 200, string $message = 'OK'): static |
| status | public function status(int $code): static |
| message | public function message(string $message): static |
| header | public function header(string $name, string $value): static |
| data | public function data(array $data): static |
| file | public function file(string $path): static |
| as | public function as(string $mimeType): static |
| asJson | public function asJson(): static |
| asHtml | public function asHtml(): static |
| asXml | public function asXml(): static |
| asCss | public function asCss(): static |
| asJs | public function asJs(): static |
| asText | public function asText(): static |
| asPdf | public function asPdf(): static |
| asFile | public function asFile(): static |
| redirect | public function redirect(string $url, int $statusCode = 302): void |
| send | public function send(): void |
| resolveMime | private function resolveMime(): string |
| registry | private static function registry(): SenderRegistry |
| resolver | private static function resolver(): MimeResolver |
| setRegistry | public static function setRegistry(SenderRegistry $registry): void |
public static function make(int $status = 200, string $message = 'OK'): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$status |
int $status = 200 |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$message |
string $message = 'OK' |
Response or exception message text. |
Return expressions (including local callbacks): $instance.
Implementation, validation guards and interaction order
// ── Factory ───────────────────────────────────────────────────────────────
public static function make(int $status = 200, string $message = 'OK'): static
{
$instance = new static();
$instance->statusCode = $status;
$instance->statusMessage = $message;
return $instance;
}public function status(int $code): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$code |
int $code |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
State writes: $this->statusCode.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
// ── Chainable builder methods ─────────────────────────────────────────────
public function status(int $code): static
{
$this->statusCode = $code;
return $this;
}public function message(string $message): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$message |
string $message |
Response or exception message text. |
State writes: $this->statusMessage.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
public function message(string $message): static
{
$this->statusMessage = $message;
return $this;
}public function header(string $name, string $value): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$name |
string $name |
Named key/label selected by this API; its exact scope is shown by the implementation trace. |
$value |
string $value |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
State writes: $this->headers[$name].
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
public function header(string $name, string $value): static
{
$this->headers[$name] = $value;
return $this;
}public function data(array $data): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$data |
array $data |
Input data array or payload; see allowed fields and validation guards below. |
Set structured data payload (array).
State writes: $this->payload.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
/** Set structured data payload (array). */
public function data(array $data): static
{
$this->payload = $data;
return $this;
}public function file(string $path): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$path |
string $path |
Filesystem or route path as defined by the owning component; no universal path normalization is implied. |
Set a file path as the payload; auto-resolves MIME from extension.
State writes: $this->payload.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
/** Set a file path as the payload; auto-resolves MIME from extension. */
public function file(string $path): static
{
$this->payload = $path;
return $this;
}public function as(string $mimeType): static| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$mimeType |
string $mimeType |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
Force a specific MIME type, overriding auto-resolution.
State writes: $this->forcedMime.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
/** Force a specific MIME type, overriding auto-resolution. */
public function as(string $mimeType): static
{
$this->forcedMime = $mimeType;
return $this;
}public function asJson(): staticReturn expressions (including local callbacks): $this->as('application/json').
Implementation, validation guards and interaction order
// ── Convenience type aliases (readable & IDE-friendly) ────────────────────
public function asJson(): static { return $this->as('application/json'); }public function asHtml(): staticReturn expressions (including local callbacks): $this->as('text/html').
Implementation, validation guards and interaction order
public function asHtml(): static { return $this->as('text/html'); }public function asXml(): staticReturn expressions (including local callbacks): $this->as('application/xml').
Implementation, validation guards and interaction order
public function asXml(): static { return $this->as('application/xml'); }public function asCss(): staticReturn expressions (including local callbacks): $this->as('text/css').
Implementation, validation guards and interaction order
public function asCss(): static { return $this->as('text/css'); }public function asJs(): staticReturn expressions (including local callbacks): $this->as('application/javascript').
Implementation, validation guards and interaction order
public function asJs(): static { return $this->as('application/javascript'); }public function asText(): staticReturn expressions (including local callbacks): $this->as('text/plain').
Implementation, validation guards and interaction order
public function asText(): static { return $this->as('text/plain'); }public function asPdf(): staticReturn expressions (including local callbacks): $this->as('application/pdf').
Implementation, validation guards and interaction order
public function asPdf(): static { return $this->as('application/pdf'); }public function asFile(): staticReturn expressions (including local callbacks): $this->as('application/octet-stream').
Implementation, validation guards and interaction order
public function asFile(): static { return $this->as('application/octet-stream'); }public function redirect(string $url, int $statusCode = 302): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$url |
string $url |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
$statusCode |
int $statusCode = 302 |
Operation input; the exact use, validation and propagation are shown in the implementation trace. |
External effects include http_response_code, header, exit.
Implementation, validation guards and interaction order
// ── Redirect shortcut ─────────────────────────────────────────────────────
public function redirect(string $url, int $statusCode = 302): void
{
http_response_code($statusCode);
header("Location: $url");
exit;
}public function send(): voidResolve the correct sender and emit the response.
Resolution order:
- Explicitly forced MIME via ->as() / ->asJson() / …
- MIME inferred from the file extension (when ->file() was used).
- MIME inferred from the HTTP Accept request header.
- Default fallback: application/json.
Delegates to resolveMime().
Implementation, validation guards and interaction order
// ── Core send ─────────────────────────────────────────────────────────────
/**
* Resolve the correct sender and emit the response.
*
* Resolution order:
* 1. Explicitly forced MIME via ->as() / ->asJson() / …
* 2. MIME inferred from the file extension (when ->file() was used).
* 3. MIME inferred from the HTTP Accept request header.
* 4. Default fallback: application/json.
*/
public function send(): void
{
$mime = $this->resolveMime();
$sender = static::registry()->resolve($mime);
// Wrap plain arrays in a standard envelope when using JSON and no file.
$payload = $this->payload;
if (is_array($payload) && $mime === 'application/json') {
$payload = [
'status' => $this->statusCode,
'message' => $this->statusMessage,
'data' => $payload,
];
}
$sender->send($payload, $this->statusCode, $this->headers);
}private function resolveMime(): stringReturn expressions (including local callbacks): $this->forcedMime; $resolved; 'application/json'.
External effects include header.
Implementation, validation guards and interaction order
// ── Private: MIME resolution ──────────────────────────────────────────────
private function resolveMime(): string
{
// 1. Explicit override.
if ($this->forcedMime !== null) {
return $this->forcedMime;
}
// 2. File extension.
if (is_string($this->payload) && $this->payload !== '') {
$resolved = static::resolver()->fromPath($this->payload);
if ($resolved !== null) {
return $resolved;
}
}
// 3. HTTP Accept header.
$accept = $_SERVER['HTTP_ACCEPT'] ?? '';
if ($accept !== '') {
$resolved = static::resolver()->fromAccept($accept);
if ($resolved !== null && static::registry()->has($resolved)) {
return $resolved;
}
}
// 4. Default.
return 'application/json';
}private static function registry(): SenderRegistryState writes: static::$registry.
Return expressions (including local callbacks): static::$registry.
Implementation, validation guards and interaction order
// ── Private: lazy singleton infrastructure ────────────────────────────────
private static function registry(): SenderRegistry
{
if (static::$registry === null) {
static::$registry = new SenderRegistry();
static::$registry->register(new JsonSender());
static::$registry->register(new HtmlSender());
static::$registry->register(new CssSender());
static::$registry->register(new JsSender());
static::$registry->register(new XmlSender());
static::$registry->register(new TextSender());
static::$registry->register(new ImageSender());
static::$registry->register(new VideoSender());
static::$registry->register(new AudioSender());
static::$registry->register(new PdfSender());
static::$registry->register(new BinarySender());
}
return static::$registry;
}private static function resolver(): MimeResolverState writes: static::$resolver.
Return expressions (including local callbacks): static::$resolver.
Implementation, validation guards and interaction order
private static function resolver(): MimeResolver
{
if (static::$resolver === null) {
static::$resolver = new MimeResolver();
}
return static::$resolver;
}public static function setRegistry(SenderRegistry $registry): void| Parameter | Type, modifiers and default | Purpose |
|---|---|---|
$registry |
SenderRegistry $registry |
Registration or lookup collaborator. |
Swap the registry at runtime (useful for testing or custom senders).
State writes: static::$registry.
Implementation, validation guards and interaction order
/**
* Swap the registry at runtime (useful for testing or custom senders).
*/
public static function setRegistry(SenderRegistry $registry): void
{
static::$registry = $registry;
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes