xdg / mime
PHP implementation of the Freedesktop Shared MIME-info Database standard.
1.0.4
2024-04-08 08:25 UTC
Requires
- php: >=8.1
- ext-dom: *
- ext-libxml: *
- ext-xml: *
- ju1ius/luigi: ^1.0
- symfony/filesystem: ^5.4||^6.1
- xdg/base-directory: ^1.0
Requires (Dev)
- ext-fileinfo: *
- ext-posix: *
- ext-sockets: *
- symfony/stopwatch: ^6.1
README
This package is a pure PHP implementation of the XDG Shared MIME-Info Database specification.
It is primarily aimed at MIME-type detection.
Installation
$ composer require xdg/mime
Usage
Implementation notes
Most XDG Shared MIME-Info implementations read their data from binary cache files
generated by the update-mime-database
program.
This turns out to be impractical in the context of PHP for several reasons:
- First, PHP doesn't have native
mmap
functionality, so reading these files at runtime can produce a lot of I/O. - Second, parsing these files at runtime has a non-negligible overhead.
- And last, PHP runs on server environments where the
update-mime-database
program is often not available (and possibly even not installable).
Therefore, this library takes another approach: parsing the MIME database in its original XML format, and compiling it to executable PHP code. This yields several advantages:
- Since most PHP applications have a build / deploy step, the overhead of parsing the database at runtime is eliminated entirely.
- Generating PHP code allows leveraging the PHP opcode cache, which makes loading the database very fast.
- Since performance is less paramount at build time, the library does additional work to optimize the generated PHP code for optimal runtime performance.
For convenience, this package ships with a pre-compiled database, built from the latest shared-mime-info database.