Skip to content

Internals Response Senders AbstractSender

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

response\senders\AbstractSender

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/response/senders/AbstractSender.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\SenderInterface;

AbstractSender

Declaration and architectural role

abstract class AbstractSender implements SenderInterface

Constants, enum cases, traits and properties

No own constants, enum cases, traits or property declarations. Inherited members are documented on the parent reference page.

Method index

Method Signature
emitStatus protected function emitStatus(int $status): void
emitHeaders protected function emitHeaders(array $headers): void
emitContentType protected function emitContentType(string $mime): void
requireReadable protected function requireReadable(string $path): void
fileSize protected function fileSize(string $path): int
streamFile protected function streamFile(string $path): void
streamRange protected function streamRange(string $path, int $start, int $length): void
parseRange protected function parseRange(string $range, int $fileSize): array

emitStatus

Source line 11

protected function emitStatus(int $status): void

Parameters

Parameter Type, modifiers and default Purpose
$status int $status Operation input; the exact use, validation and propagation are shown in the implementation trace.

External effects include http_response_code.

Implementation, validation guards and interaction order
// ── Shared helpers ────────────────────────────────────────────────────────

    protected function emitStatus(int $status): void
    {
        http_response_code($status);
    }

emitHeaders

Source line 16

protected function emitHeaders(array $headers): void

Parameters

Parameter Type, modifiers and default Purpose
$headers array $headers Operation input; the exact use, validation and propagation are shown in the implementation trace.

External effects include header.

Implementation, validation guards and interaction order
protected function emitHeaders(array $headers): void
    {
        foreach ($headers as $name => $value) {
            header("$name: $value");
        }
    }

emitContentType

Source line 23

protected function emitContentType(string $mime): void

Parameters

Parameter Type, modifiers and default Purpose
$mime string $mime Operation input; the exact use, validation and propagation are shown in the implementation trace.

External effects include header.

Implementation, validation guards and interaction order
protected function emitContentType(string $mime): void
    {
        header("Content-Type: $mime");
    }

requireReadable

Source line 30

protected function requireReadable(string $path): void

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.

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
// ── File helpers ──────────────────────────────────────────────────────────

    protected function requireReadable(string $path): void
    {
        if (!file_exists($path)) {
            throw new \RuntimeException("File not found: '$path'.");
        }
        if (!is_readable($path)) {
            throw new \RuntimeException("File not readable: '$path'.");
        }
    }

fileSize

Source line 40

protected function fileSize(string $path): int

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.

Return expressions (including local callbacks): $size.

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
protected function fileSize(string $path): int
    {
        $size = filesize($path);
        if ($size === false) {
            throw new \RuntimeException("Cannot determine file size: '$path'.");
        }
        return $size;
    }

streamFile

Source line 50

protected function streamFile(string $path): void

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.

Stream a whole file in 8 KB chunks.

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/** Stream a whole file in 8 KB chunks. */
    protected function streamFile(string $path): void
    {
        $this->requireReadable($path);
        $handle = fopen($path, 'rb');
        if ($handle === false) {
            throw new \RuntimeException("Cannot open file: '$path'.");
        }
        while (!feof($handle)) {
            echo fread($handle, 8192);
            flush();
        }
        fclose($handle);
    }

streamRange

Source line 65

protected function streamRange(string $path, int $start, int $length): void

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.
$start int $start Operation input; the exact use, validation and propagation are shown in the implementation trace.
$length int $length Operation input; the exact use, validation and propagation are shown in the implementation trace.

Stream a byte range from a file (for Range requests).

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/** Stream a byte range from a file (for Range requests). */
    protected function streamRange(string $path, int $start, int $length): void
    {
        $handle = fopen($path, 'rb');
        if ($handle === false) {
            throw new \RuntimeException("Cannot open file: '$path'.");
        }
        fseek($handle, $start);
        $remaining = $length;
        while ($remaining > 0 && !feof($handle)) {
            $chunk      = min(8192, $remaining);
            $data       = fread($handle, $chunk);
            $remaining -= strlen($data);
            echo $data;
            flush();
        }
        fclose($handle);
    }

parseRange

Source line 84

protected function parseRange(string $range, int $fileSize): array

Parameters

Parameter Type, modifiers and default Purpose
$range string $range Operation input; the exact use, validation and propagation are shown in the implementation trace.
$fileSize int $fileSize Operation input; the exact use, validation and propagation are shown in the implementation trace.

Parse an HTTP Range header → [start, end].

Return expressions (including local callbacks): [$start, $end].

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

External effects include header, http_response_code.

Implementation, validation guards and interaction order
/** Parse an HTTP Range header → [start, end]. */
    protected function parseRange(string $range, int $fileSize): array
    {
        if (!preg_match('/bytes=(\d*)-(\d*)/', $range, $m)) {
            throw new \RuntimeException("Malformed Range header: '$range'.");
        }
        $start = $m[1] !== '' ? (int) $m[1] : 0;
        $end   = $m[2] !== '' ? (int) $m[2] : $fileSize - 1;

        if ($start > $end || $end >= $fileSize) {
            http_response_code(416);
            header("Content-Range: bytes */$fileSize");
            throw new \RuntimeException("Range not satisfiable.");
        }
        return [$start, $end];
    }

Related types

Clone this wiki locally