xdg/mime

PHP implementation of the Freedesktop Shared MIME-info Database standard.

1.0.4 2024-04-08 08:25 UTC

This package is auto-updated.

Last update: 2024-11-17 02:05:45 UTC


README

codecov

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.