<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Mime;
use Symfony\Component\Mime\Exception\LogicException;
/**
* Manages MIME types and file extensions.
*
* For MIME type guessing, you can register custom guessers
* by calling the registerGuesser() method.
* Custom guessers are always called before any default ones:
*
* $guesser = new MimeTypes();
* $guesser->registerGuesser(new MyCustomMimeTypeGuesser());
*
* If you want to change the order of the default guessers, just re-register your
* preferred one as a custom one. The last registered guesser is preferred over
* previously registered ones.
*
* Re-registering a built-in guesser also allows you to configure it:
*
* $guesser = new MimeTypes();
* $guesser->registerGuesser(new FileinfoMimeTypeGuesser('/path/to/magic/file'));
*
* @author Fabien Potencier <fabien@symfony.com>
*/
final class MimeTypes implements MimeTypesInterface
{
private array $extensions = [];
private array $mimeTypes = [];
/**
* @var MimeTypeGuesserInterface[]
*/
private array $guessers = [];
private static MimeTypes $default;
public function __construct(array $map = [])
{
foreach ($map as $mimeType => $extensions) {
$this->extensions[$mimeType] = $extensions;
foreach ($extensions as $extension) {
$this->mimeTypes[$extension][] = $mimeType;
}
}
$this->registerGuesser(new FileBinaryMimeTypeGuesser());
$this->registerGuesser(new FileinfoMimeTypeGuesser());
}
public static function setDefault(self $default): void
{
self::$default = $default;
}
public static function getDefault(): self
{
return self::$default ??= new self();
}
/**
* Registers a MIME type guesser.
*
* The last registered guesser has precedence over the other ones.
*/
public function registerGuesser(MimeTypeGuesserInterface $guesser): void
{
array_unshift($this->guessers, $guesser);
}
public function getExtensions(string $mimeType): array
{
if ($this->extensions) {
$extensions = $this->extensions[$mimeType] ?? $this->extensions[$lcMimeType = strtolower($mimeType)] ?? null;
}
return $extensions ?? self::MAP[$mimeType] ?? self::MAP[$lcMimeType ?? strtolower($mimeType)] ?? [];
}
public function getMimeTypes(string $ext): array
{
if ($this->mimeTypes) {
$mimeTypes = $this->mimeTypes[$ext] ?? $this->mimeTypes[$lcExt = strtolower($ext)] ?? null;
}
return $mimeTypes ?? self::REVERSE_MAP[$ext] ?? self::REVERSE_MAP[$lcExt ?? strtolower($ext)] ?? [];
}
public function isGuesserSupported(): bool
{
foreach ($this->guessers as $guesser) {
if ($guesser->isGuesserSupported()) {
return true;
}
}
return false;
}
/**
* The file is passed to each registered MIME type guesser in reverse order
* of their registration (last registered is queried first). Once a guesser
* returns a value that is not null, this method terminates and returns the
* value.
*/
public function guessMimeType(string $path): ?string
{
foreach ($this->guessers as $guesser) {
if (!$guesser->isGuesserSupported()) {
continue;
}
if (null !== $mimeType = $guesser->guessMimeType($path)) {
return $mimeType;
}
}
if (!$this->isGuesserSupported()) {
throw new LogicException('Unable to guess the MIME type as no guessers are available (have you enabled the php_fileinfo extension?).');
}
return null;
}
/**
* A map of MIME types and their default extensions.
*
* Updated from upstream on 2023-10-14.
*
* @see Resources/bin/update_mime_types.php
*/
private const MAP = [
'application/acrobat' => ['pdf'],
'application/andrew-inset' => ['ez'],
'application/annodex' => ['anx'],
'application/applixware' => ['aw'],
'application/atom+xml' => ['atom'],
'application/atomcat+xml' => ['atomcat'],
'application/atomdeleted+xml' => ['atomdeleted'],
'application/atomsvc+xml' => ['atomsvc'],
'application/atsc-dwd+xml' => ['dwd'],
'application/atsc-held+xml' => ['held'],
'application/atsc-rsat+xml' => ['rsat'],
'application/bat' => ['bat'],
'application/bdoc' => ['bdoc'],
'application/bzip2' => ['bz2', 'bz'],
'application/calendar+xml' => ['xcs'],
'application/cbor' => ['cbor'],
'application/ccxml+xml' => ['ccxml'],
'application/cdfx+xml' => ['cdfx'],
'application/cdmi-capability' => ['cdmia'],
'application/cdmi-container' => ['cdmic'],
'application/cdmi-domain' => ['cdmid'],
'application/cdmi-object' => ['cdmio'],
'application/cdmi-queue' => ['cdmiq'],
'application/cdr' => ['cdr'],
'application/coreldraw' => ['cdr'],
'application/cpl+xml' => ['cpl'],
'application/csv' => ['csv'],
'application/cu-seeme' => ['cu'],
'application/dash+xml' => ['mpd'],
'application/dash-patch+xml' => ['mpp'],
'application/davmount+xml' => ['davmount'],
'application/dbase' => ['dbf'],
'application/dbf' => ['dbf'],
'application/dicom' => ['dcm'],
'application/docbook+xml' => ['dbk', 'docbook'],
'application/dssc+der' => ['dssc'],
'application/dssc+xml' => ['xdssc'],
'application/ecmascript' => ['ecma', 'es'],
'application/emf' => ['emf'],
'application/emma+xml' => ['emma'],
'application/emotionml+xml' => ['emotionml'],
'application/epub+zip' => ['epub'],
'application/exi' => ['exi'],
'application/express' => ['exp'],
'application/fdt+xml' => ['fdt'],
'application/fits' => ['fits', 'fit', 'fts'],
'application/font-tdpfr' => ['pfr'],
'application/font-woff' => ['woff'],
'application/futuresplash' => ['swf', 'spl'],
'application/geo+json' => ['geojson', 'geo.json'],
'application/gml+xml' => ['gml'],
'application/gnunet-directory' => ['gnd'],
'application/gpx' => ['gpx'],
'application/gpx+xml' => ['gpx'],
'application/gxf' => ['gxf'],
'application/gzip' => ['gz'],
'application/hjson' => ['hjson'],
'application/hyperstudio' => ['stk'],
'application/ico' => ['ico'],
'application/ics' => ['vcs', 'ics'],
'application/illustrator' => ['ai'],
'application/inkml+xml' => ['ink', 'inkml'],
'application/ipfix' => ['ipfix'],
'application/its+xml' => ['its'],
'application/java' => ['class'],
'application/java-archive' => ['jar', 'war', 'ear'],
'application/java-byte-code' => ['class'],
'application/java-serialized-object' => ['ser'],
'application/java-vm' => ['class'],
'application/javascript' => ['js', 'mjs', 'jsm'],
'application/jrd+json' => ['jrd'],
'application/json' => ['json', 'map'],
'application/json-patch+json' => ['json-patch'],
'application/json5' => ['json5'],
'application/jsonml+json' => ['jsonml'],
'application/ld+json' => ['jsonld'],
'application/lgr+xml' => ['lgr'],
'application/lost+xml' => ['lostxml'],
'application/lotus123' => ['123', 'wk1', 'wk3', 'wk4', 'wks'],
'application/m3u' => ['m3u', 'm3u8', 'vlc'],
'application/mac-binhex40' => ['hqx'],
'application/mac-compactpro' => ['c
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论


















收起资源包目录





































































































共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论


稀稀落落987
- 粉丝: 1299
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 年产2000吨工业明胶自动化生产线项目可行性研究报告(1).doc
- 网站开发培训心得(1).docx
- 智能制造产线自动化协议(标准版)(1).docx
- 软件学院物业管理投标书(1).docx
- 基于改进区域生长自监督的立交桥点云提取方法研究
- 浅析互联网背景下财务会计与管理(1).docx
- 电力通信系统运行安全可靠性分析研究刘梁宝(1).docx
- 机械设计制造及其自动化-外文翻译-外文文献-英文文献-停车管理(1).doc
- 通信原理基础知识(1).ppt
- 基因工程制胰岛素(1).ppt
- 信息化教学策略在中职机械加工技术教学中的运用(1).docx
- 面向对象程序设计教程-c++语言描述-第二版-马石安(1).doc
- 通信管道、线路施工安全注意事项及风险防范(1).doc
- 简单的图书管理系统(1).docx
- 水产养殖信息化关键技术研究(1).docx
- 双河市--通信施工技术方案精品教案(1).doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
