8000 [Mime] Cache finfo objects to reduce open file handles and optimize perf · symfony/symfony@e9c0b15 · GitHub
[go: up one dir, main page]

Skip to content

Commit e9c0b15

Browse files
[Mime] Cache finfo objects to reduce open file handles and optimize perf
1 parent 44395ab commit e9c0b15

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Symfony/Component/Mime/FileinfoMimeTypeGuesser.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
2323
{
24+
private static $finfoCache = [];
25+
2426
/**
2527
* @param string|null $magicFile A magic file to use with the finfo instance
2628
*
@@ -46,7 +48,9 @@ public function guessMimeType(string $path): ?string
4648
throw new LogicException(\sprintf('The "%s" guesser is not supported.', __CLASS__));
4749
}
4850

49-
if (false === $finfo = new \finfo(\FILEINFO_MIME_TYPE, $this->magicFile)) {
51+
try {
52+
$finfo = self::$finfoCache[$this->magicFile] ??= new \finfo(\FILEINFO_MIME_TYPE, $this->magicFile);
53+
} catch (\Exception) {
5054
return null;
5155
}
5256
$mimeType = $finfo->file($path);

0 commit comments

Comments
 (0)
0