fileeye / mimemap
一个用于处理 MIME 内容类型字段及其相关文件扩展名的 PHP 库。
Requires
- php: >=7.4
Requires (Dev)
- composer-runtime-api: ^2.0.0
- sebastian/comparator: >=4
- sebastian/diff: >=4
- symfony/console: >=5.4
- symfony/filesystem: >=5.4
- symfony/var-dumper: >=5.4
- symfony/yaml: >=5.4
This package is auto-updated.
Last update: 2024-09-14 13:40:33 UTC
README
一个用于处理 MIME 内容类型字段及其相关文件扩展名的 PHP 库。
功能
- 解析 MIME 内容类型字段
- 支持RFC 2045规范
- 提供用于处理和确定 MIME 类型信息的实用函数
- 将文件扩展名映射到 MIME 类型,反之亦然
- 自动从最权威的来源更新 MIME 类型与文件扩展名之间的映射,例如Apache 文档和freesesktop.org 项目。
- PHPUnit 测试,100% 测试覆盖率
- PHPStan 测试,等级 9
- Psalm 测试
致谢
MimeMap 是 PEAR 的MIME_Type软件包的分支。查看所有原始贡献者。
请注意,与 PEAR 的 MIME_Type 相比,此库具有不同的作用域,主要关注找到每个 MIME 类型及其通常接受的文件扩展名之间的映射。已删除检测文件 MIME 类型的功能。建议使用symfony/http-foundation库及其MimeTypeGuesser API 来处理该用例。
其他软件包
MimeMap 与类似软件包的主要区别在于,它提供使用多个类型到扩展名映射的功能,以及可以在 PHP 类中动态或静态地更改映射。有关替代 PHP MIME 类型处理库的精美列表,请参阅wgenial/php-mimetyper。
安装
$ composer require fileeye/mimemap
使用方法
基本用法
此软件包附带一个默认映射,描述了 MIME 类型及其通常与每个 MIME 类型关联的文件扩展名。映射还存储有关 MIME 类型别名(描述相同 MIME 类型的替代媒体/子类型组合)和大多数 MIME 类型以及所使用的缩写词的描述信息。
例如:MIME 类型 'application/pdf'
- 描述为 'PDF 文档'
- PDF 缩写描述为 'PDF: 可移植文档格式'
- 通常使用文件扩展名 'pdf'
- 具有别名,如 'application/x-pdf','image/pdf'
此软件包实现的 API 非常简单
- 您有一个 MIME 类型,并想获取与其通常关联的文件扩展名
use FileEye\MimeMap\Type; ... $type = new Type('image/jpeg'); print_r($type->getExtensions()); // will print ['jpeg', 'jpg', 'jpe'] print_r($type->getDefaultExtension()); // will print 'jpeg' // When passing an alias to a MIME type, the API will // return the extensions to the parent type: $type = new Type('image/pdf'); print_r($type->getDefaultExtension()); // will print 'pdf' which is the default extension for 'application/pdf'
- 反之亦然,您有一个文件扩展名,并想获取与其通常关联的 MIME 类型
use FileEye\MimeMap\Extension; ... $ext = new Extension('xar'); print_r($ext->getTypes()); // will return ['application/vnd.xara', 'application/x-xar'] print_r($ext->getDefaultType()); // will return 'application/vnd.xara'
- 您有一个原始的 MIME 内容类型字符串,并想添加一个参数
use FileEye\MimeMap\Type; ... $type = new Type('text / (Unstructured text) plain ; charset = (UTF8, not ASCII) utf-8'); $type->addParameter('lang', 'it', 'Italian'); echo $type->toString(Type::SHORT_TEXT); // will print 'text/plain' echo $type->toString(Type::FULL_TEXT); // will print 'text/plain; charset="utf-8"; lang="it"' echo $type->toString(Type::FULL_TEXT_WITH_COMMENTS); // will print 'text/plain (Unstructured text); charset="utf-8" (UTF8, not ASCII), lang="it" (Italian)'
- 您有一个 MIME 内容类型字符串,并想添加类型描述作为注释
use FileEye\MimeMap\Type; ... $type = new Type('text/html'); $type_desc = $type->getDescription(); $type->setSubTypeComment($type_desc); echo $type->toString(Type::FULL_TEXT_WITH_COMMENTS); // will print 'text/html (HTML document)' // Setting the $include_acronym parameter of getDescription to true // will extend the description to include the meaning of the acronym $type_desc = $type->getDescription(true); $type->setSubTypeComment($type_desc); echo $type->toString(Type::FULL_TEXT_WITH_COMMENTS); // will print 'text/html (HTML document, HTML: HyperText Markup Language)'
指定替代 MIME 类型映射
您还可以在运行时更改默认映射,通过添加/删除映射,或者指示 MimeMap 使用完全不同的映射。替代映射必须存储在扩展 \FileEye\MimeMap\Map\AbstractMap
的 PHP 类中。
- 您想向默认类添加额外的 MIME 类型到扩展名映射
use FileEye\MimeMap\Extension; use FileEye\MimeMap\MapHandler; use FileEye\MimeMap\Type; ... $map = MapHandler::map(); $map->addTypeExtensionMapping('foo/bar', 'baz'); $type = new Type('foo/bar'); $default_extension = $type->getDefaultExtension(); // will return 'baz' $ext = new Extension('baz'); $default_type = $ext->getDefaultExtension(); // will return 'foo/bar'
- 您想将替代映射类设置为默认值
use FileEye\MimeMap\Extension; use FileEye\MimeMap\MapHandler; use FileEye\MimeMap\Type; ... MapHandler::setDefaultMapClass('MyProject\MyMap'); ...
- 您还可以仅针对单个类型或扩展对象使用替代映射
use FileEye\MimeMap\Extension; use FileEye\MimeMap\Type; ... $type = new Type('foo/bar', 'MyProject\MyMap'); $ext = new Extension('baz', 'MyProject\MyMap');
开发
更新扩展映射代码
默认的扩展到类型的映射类可以从源代码仓库更新,使用fileeye-mimemap
实用程序
$ cd [project_directory]/vendor/bin
$ fileeye-mimemap update
默认情况下,该实用程序从Apache文档网站获取映射源,将其与freedesktop.org项目中的另一个映射源合并,然后将结果与resources/default_map_build.yml
文件中指定的任何覆盖内容集成,最后更新存储\FileEye\MimeMap\Map\DefaultMap
类的PHP文件。
--script
和--class
选项允许指定不同的更新逻辑和不同的类文件进行更新。类型
$ fileeye-mimemap update --help
以获取更多信息。