Skip to content

Commit bd25312

Browse files
committed
added docs for the Mime component
1 parent ccca18b commit bd25312

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

components/mime.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. index::
2+
single: MIME
3+
single: Components; MIME
4+
5+
The MIME Component
6+
==================
7+
8+
The MIME component allows manipulating MIME types.
9+
10+
Installation
11+
------------
12+
13+
.. code-block:: terminal
14+
15+
$ composer require symfony/mime
16+
17+
Alternatively, you can clone the `<https://github.com/symfony/mime>`_ repository.
18+
19+
.. include:: /components/require_autoload.rst.inc
20+
21+
.. tip::
22+
23+
You should have the ``fileinfo`` PHP extension installed for greater performance.
24+
25+
MIME Types
26+
----------
27+
28+
The :class:`Symfony\\Component\\Mime\\MimeTypes` class manipulates the
29+
relationships between MIME types and file name extensions::
30+
31+
use Symfony\Component\Mime\MimeTypes;
32+
33+
$mimeTypes = new MimeTypes();
34+
$exts = $mimeTypes->getExtensions('application/mbox');
35+
// $exts contains an array of extensions: ['mbox']
36+
37+
$mimeTypes = $mimeTypes->getMimeTypes('mbox');
38+
// $mimeTypes contains an array of MIME types: ['application/mbox']
39+
40+
// guess a mime type for a file
41+
$mimeType = $mimeTypes->guessMimeType('/some/path/to/image.gif');
42+
43+
Adding a MIME Type Guesser
44+
~~~~~~~~~~~~~~~~~~~~~~~~~~
45+
46+
You can register your own MIME type guesser by creating a class that extends
47+
:class:`Symfony\\Component\\Mime\\MimeTypeGuesserInterface`::
48+
49+
namespace App;
50+
51+
use Symfony\Component\Mime\MimeTypeGuesserInterface;
52+
53+
class SomeMimeTypeGuesser implements MimeTypeGuesserInterface
54+
{
55+
public function isGuesserSupported(): bool
56+
{
57+
// return true when the guesser is supported (might depend on the OS for instance)
58+
return true;
59+
}
60+
61+
public function guessMimeType(string $path): ?string
62+
{
63+
// return a MIME type based on the content of the file stored in $path
64+
// you MUST not use the filename to determine the MIME type.
65+
return 'text/plain';
66+
}
67+
}

reference/dic_tags.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Tag Name Usage
2525
`kernel.event_listener`_ Listen to different events/hooks in Symfony
2626
`kernel.event_subscriber`_ To subscribe to a set of different events/hooks in Symfony
2727
`kernel.fragment_renderer`_ Add new HTTP content rendering strategies
28+
`mime.mime_type_guesser`_ Add a custom mime type guesser to MimeTypes
2829
`monolog.logger`_ Logging with a custom logging channel
2930
`monolog.processor`_ Add a custom processor for logging
3031
`routing.loader`_ Register a custom service that loads routes
@@ -450,6 +451,15 @@ To add a new rendering strategy - in addition to the core strategies like
450451
:class:`Symfony\\Component\\HttpKernel\\Fragment\\FragmentRendererInterface`,
451452
register it as a service, then tag it with ``kernel.fragment_renderer``.
452453

454+
mime.mime_type_guesser
455+
----------------------
456+
457+
**Purpose**: Add a custom MIME type guesser to MimeTypes
458+
459+
To add a custom MIME type guesser - in addition to the core guessers - create a
460+
class that implements :class:`Symfony\\Component\\Mime\\MimeTypes`, register it
461+
as a service, then tag it with ``mime.mime_type_guesser``.
462+
453463
.. _dic_tags-monolog:
454464

455465
monolog.logger

0 commit comments

Comments
 (0)