API Index — Namespaces, Packages, Reports, Indices
MimeResolver
in package
Multi-suffix MIME metadata resolver — Apache mod_mime find_ct parity.
Apache walks EVERY dot-separated suffix of a filename left-to-right
(mod_mime.c find_ct, ~874–1007), accumulating Content-Type,
Content-Encoding, Content-Language and charset from each suffix. PHP's
pathinfo(…, PATHINFO_EXTENSION) only ever returns the rightmost suffix,
which is wrong for files such as document.html.gz (HTML body carried with
a gzip Content-Encoding) or page.fr.html (French HTML).
This resolver reproduces Apache's algorithm exactly:
- Basename rule (
find_ctlines 874–887): leading dots are part of the basename, and the segment up to the first real dot is the basename and carries no metadata. So.pngis a hidden file namedpngwith zero extensions — NO type is assigned (the M12 dotfile fix). - Suffix walk (
find_ctlines 891–1007): each remaining suffix is lowercased and looked up independently. Empty suffixes ("bad..html") are skipped (line 898). - Content-Type: last matching suffix wins — Apache calls
ap_set_content_typeper match, overwriting (line 921/930). - Content-Language: every matching suffix is accumulated in order
(lines 938–946,
apr_array_push). - Content-Encoding: every matching suffix is accumulated in order,
comma-joined; duplicates and double-encoding are intentionally preserved
(lines 947–962, the
-- ndcomment).
The resolver is pure: it takes three case-insensitive extension→value maps (type, encoding, language) and a filename, and returns the resolved metadata. It performs no I/O and never inspects file contents.
Table of Contents
Properties
- $encodingMap : array<string, string>
- $languageMap : array<string, string>
- $typeMap : array<string, string>
Methods
- __construct() : mixed
-
resolve()
: array{type: ?string, encoding: ?string, languages: list
} - Walk every suffix of $filename and accumulate metadata.
- suffixes() : array<int, string>
- Decompose a filename into its lowercased suffix list, applying Apache's basename rule (leading dots + segment before the first real dot are the basename and excluded). Empty suffixes are dropped.
- normaliseMap() : array<string, string>
- Normalise a caller-supplied extension map: lowercase + dot-strip keys, stringify values (Apache lowercases stored values; we preserve the caller's value casing, matching MimeTypeMiddleware's prior behaviour).
Properties
$encodingMap
private
array<string, string>
$encodingMap
ext => content-encoding (keys lowercased, dot-stripped)
$languageMap
private
array<string, string>
$languageMap
ext => content-language (keys lowercased, dot-stripped)
$typeMap
private
array<string, string>
$typeMap
ext => mime-type (keys lowercased, dot-stripped)
Methods
__construct()
public
__construct([array<string, string|int> $typeMap = [] ][, array<string, string|int> $encodingMap = [] ][, array<string, string|int> $languageMap = [] ]) : mixed
Parameters
- $typeMap : array<string, string|int> = []
-
ext => mime-type
- $encodingMap : array<string, string|int> = []
-
ext => content-encoding (e.g. gz => gzip)
- $languageMap : array<string, string|int> = []
-
ext => content-language (e.g. fr => fr)
resolve()
Walk every suffix of $filename and accumulate metadata.
public
resolve(string $filename) : array{type: ?string, encoding: ?string, languages: list}
Parameters
- $filename : string
-
basename or full path; only the basename's suffix chain is considered.
Return values
array{type: ?string, encoding: ?string, languages: listtype is null when no suffix mapped a Content-Type; encoding
is the comma-joined encoding chain (null when none); languages
is the ordered list of matched languages (empty when none).
suffixes()
Decompose a filename into its lowercased suffix list, applying Apache's basename rule (leading dots + segment before the first real dot are the basename and excluded). Empty suffixes are dropped.
public
suffixes(string $filename) : array<int, string>
document.html.gz => ['html', 'gz']
.png => [] (hidden file, no extension)
archive.TAR.GZ => ['tar', 'gz']
noext => []
Parameters
- $filename : string
Return values
array<int, string>normaliseMap()
Normalise a caller-supplied extension map: lowercase + dot-strip keys, stringify values (Apache lowercases stored values; we preserve the caller's value casing, matching MimeTypeMiddleware's prior behaviour).
private
static normaliseMap(array<string, string|int> $map) : array<string, string>
Parameters
- $map : array<string, string|int>