Skip to content

Internals Response Response

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

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.

Imports and dependencies

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
};

Response

Declaration and architectural role

class Response implements ResponseInterface

Source 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');

Constants, enum cases, traits and properties

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 index

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

make

Source line 60

public static function make(int $status = 200, string $message = 'OK'): static

Parameters

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;
    }

status

Source line 70

public function status(int $code): static

Parameters

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;
    }

message

Source line 76

public function message(string $message): static

Parameters

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;
    }

header

Source line 82

public function header(string $name, string $value): static

Parameters

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;
    }

data

Source line 89

public function data(array $data): static

Parameters

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;
    }

file

Source line 96

public function file(string $path): static

Parameters

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;
    }

as

Source line 103

public function as(string $mimeType): static

Parameters

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;
    }

asJson

Source line 111

public function asJson(): static

Return 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'); }

asHtml

Source line 112

public function asHtml(): static

Return expressions (including local callbacks): $this->as('text/html').

Implementation, validation guards and interaction order
public function asHtml(): static  { return $this->as('text/html'); }

asXml

Source line 113

public function asXml(): static

Return expressions (including local callbacks): $this->as('application/xml').

Implementation, validation guards and interaction order
public function asXml(): static   { return $this->as('application/xml'); }

asCss

Source line 114

public function asCss(): static

Return expressions (including local callbacks): $this->as('text/css').

Implementation, validation guards and interaction order
public function asCss(): static   { return $this->as('text/css'); }

asJs

Source line 115

public function asJs(): static

Return expressions (including local callbacks): $this->as('application/javascript').

Implementation, validation guards and interaction order
public function asJs(): static    { return $this->as('application/javascript'); }

asText

Source line 116

public function asText(): static

Return expressions (including local callbacks): $this->as('text/plain').

Implementation, validation guards and interaction order
public function asText(): static  { return $this->as('text/plain'); }

asPdf

Source line 117

public function asPdf(): static

Return expressions (including local callbacks): $this->as('application/pdf').

Implementation, validation guards and interaction order
public function asPdf(): static   { return $this->as('application/pdf'); }

asFile

Source line 118

public function asFile(): static

Return 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'); }

redirect

Source line 122

public function redirect(string $url, int $statusCode = 302): void

Parameters

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;
    }

send

Source line 140

public function send(): void

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.

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);
    }

resolveMime

Source line 160

private function resolveMime(): string

Return 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';
    }

registry

Source line 190

private static function registry(): SenderRegistry

State 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;
    }

resolver

Source line 209

private static function resolver(): MimeResolver

State 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;
    }

setRegistry

Source line 220

public static function setRegistry(SenderRegistry $registry): void

Parameters

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;
    }

Related types

Clone this wiki locally