Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4e70834

Browse filesBrowse files
minor #58233 [Mime] Cache finfo objects to reduce open file handles and optimize perf (nicolas-grekas)
This PR was merged into the 7.2 branch. Discussion ---------- [Mime] Cache finfo objects to reduce open file handles and optimize perf | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Inspired by #58163 Commits ------- 5a1be29 [Mime] Cache finfo objects to reduce open file handles and optimize perf
2 parents 44395ab + 5a1be29 commit 4e70834
Copy full SHA for 4e70834

File tree

Expand file treeCollapse file tree

1 file changed

+11
-3
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+11
-3
lines changed

‎src/Symfony/Component/Mime/FileinfoMimeTypeGuesser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mime/FileinfoMimeTypeGuesser.php
+11-3Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Mime\Exception\InvalidArgumentException;
1515
use Symfony\Component\Mime\Exception\LogicException;
16+
use Symfony\Component\Mime\Exception\RuntimeException;
1617

1718
/**
1819
* Guesses the MIME type using the PECL extension FileInfo.
@@ -21,6 +22,11 @@
2122
*/
2223
class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
2324
{
25+
/**
26+
* @var array<string, \finfo>
27+
*/
28+
private static $finfoCache = [];
29+
2430
/**
2531
* @param string|null $magicFile A magic file to use with the finfo instance
2632
*
@@ -46,10 +52,12 @@ public function guessMimeType(string $path): ?string
4652
throw new LogicException(\sprintf('The "%s" guesser is not supported.', __CLASS__));
4753
}
4854

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());
5159
}
52-
$mimeType = $finfo->file($path);
60+
$mimeType = $finfo->file($path) ?: null;
5361

5462
if ($mimeType && 0 === (\strlen($mimeType) % 2)) {
5563
$mimeStart = substr($mimeType, 0, \strlen($mimeType) >> 1);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.