Skip to content

Internals Response Resolvers MimeResolver

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

response\resolvers\MimeResolver

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/response/resolvers/MimeResolver.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.

MimeResolver

Declaration and architectural role

class MimeResolver

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
const EXT_MAP private const EXT_MAP = [ 'json' => 'application/json', 'html' => 'text/html', 'htm' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'mjs' => 'application/javascript', 'xml' => 'application/xml', 'txt' => 'text/plain', 'csv' => 'text/csv', 'pdf' => 'application/pdf', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', 'svg' => 'image/svg+xml', 'ico' => 'image/x-icon', 'bmp' => 'image/bmp', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'avif' => 'image/avif', 'mp3' => 'audio/mpeg', 'wav' => 'audio/wav', 'ogg' => 'audio/ogg', 'oga' => 'audio/ogg', 'aac' => 'audio/aac', 'flac' => 'audio/flac', 'm4a' => 'audio/x-m4a', 'weba' => 'audio/webm', 'mp4' => 'video/mp4', 'webm' => 'video/webm', 'ogv' => 'video/ogg', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo', 'mkv' => 'video/x-matroska', '3gp' => 'video/3gpp', ]; Map of file extension → MIME type.

Method index

Method Signature
fromPath public function fromPath(string $path): ?string
fromAccept public function fromAccept(string $accept): ?string

fromPath

Source line 56

public function fromPath(string $path): ?string

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.

Resolve MIME from a file path's extension. Returns null when extension is unknown.

Return expressions (including local callbacks): self::EXT_MAP[$ext] ?? null.

Implementation, validation guards and interaction order
/**
     * Resolve MIME from a file path's extension.
     * Returns null when extension is unknown.
     */
    public function fromPath(string $path): ?string
    {
        $ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
        return self::EXT_MAP[$ext] ?? null;
    }

fromAccept

Source line 68

public function fromAccept(string $accept): ?string

Parameters

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

Resolve the best MIME from an HTTP Accept header string.

Picks the first accepted type that we recognise, respecting q-values. Falls back to null when nothing matches.

Return expressions (including local callbacks): $accepted; $mime; null.

Implementation, validation guards and interaction order
/**
     * Resolve the best MIME from an HTTP Accept header string.
     *
     * Picks the first accepted type that we recognise, respecting q-values.
     * Falls back to null when nothing matches.
     */
    public function fromAccept(string $accept): ?string
    {
        $known = array_unique(array_values(self::EXT_MAP));
        $parts = array_map('trim', explode(',', $accept));

        // Build [ mime => q ] with q-value sorting.
        $weighted = [];
        foreach ($parts as $part) {
            [$type, $params] = array_pad(explode(';', $part, 2), 2, '');
            $type = strtolower(trim($type));
            $q    = 1.0;
            if (preg_match('/q=([\d.]+)/', (string) $params, $m)) {
                $q = (float) $m[1];
            }
            $weighted[$type] = $q;
        }
        arsort($weighted);

        foreach (array_keys($weighted) as $accepted) {
            // Exact match.
            if (in_array($accepted, $known, true)) {
                return $accepted;
            }
            // Wildcard: e.g. "text/*" matches "text/html".
            if (str_ends_with($accepted, '/*')) {
                $prefix = substr($accepted, 0, -1); // "text/"
                foreach ($known as $mime) {
                    if (str_starts_with($mime, $prefix)) {
                        return $mime;
                    }
                }
            }
        }

        return null;
    }

Clone this wiki locally