File tree Expand file tree Collapse file tree 1 file changed +11
-3
lines changed
Filter options
src/Symfony/Component/Mime Expand file tree Collapse file tree 1 file changed +11
-3
lines changed
Original file line number Diff line number Diff line change 13
13
14
14
use Symfony \Component \Mime \Exception \InvalidArgumentException ;
15
15
use Symfony \Component \Mime \Exception \LogicException ;
16
+ use Symfony \Component \Mime \Exception \RuntimeException ;
16
17
17
18
/**
18
19
* Guesses the MIME type using the PECL extension FileInfo.
21
22
*/
22
23
class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
23
24
{
25
+ /**
26
+ * @var array<string, \finfo>
27
+ */
28
+ private static $ finfoCache = [];
29
+
24
30
/**
25
31
* @param string|null $magicFile A magic file to use with the finfo instance
26
32
*
@@ -46,10 +52,12 @@ public function guessMimeType(string $path): ?string
46
52
throw new LogicException (\sprintf ('The "%s" guesser is not supported. ' , __CLASS__ ));
47
53
}
48
54
49
- if (false === $ finfo = new \finfo (\FILEINFO_MIME_TYPE , $ this ->magicFile )) {
50
- return null ;
55
+ try {
56
+ $ finfo = self ::$ finfoCache [$ this ->magicFile ] ??= new \finfo (\FILEINFO_MIME_TYPE , $ this ->magicFile );
57
+ } catch (\Exception $ e ) {
58
+ throw new RuntimeException ($ e ->getMessage ());
51
59
}
52
- $ mimeType = $ finfo ->file ($ path );
60
+ $ mimeType = $ finfo ->file ($ path ) ?: null ;
53
61
54
62
if ($ mimeType && 0 === (\strlen ($ mimeType ) % 2 )) {
55
63
$ mimeStart = substr ($ mimeType , 0 , \strlen ($ mimeType ) >> 1 );
You can’t perform that action at this time.
0 commit comments