Skip to content

Commit

Permalink
[HttpFoundation] Fix perf issue during MimeTypeGuesser intialization
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed May 23, 2018
1 parent eff0bf4 commit 92f35ae
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions File/MimeType/MimeTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,8 @@ public static function reset()
*/
private function __construct()
{
if (FileBinaryMimeTypeGuesser::isSupported()) {
$this->register(new FileBinaryMimeTypeGuesser());
}

if (FileinfoMimeTypeGuesser::isSupported()) {
$this->register(new FileinfoMimeTypeGuesser());
}
$this->register(new FileBinaryMimeTypeGuesser());
$this->register(new FileinfoMimeTypeGuesser());
}

/**
Expand Down Expand Up @@ -125,18 +120,14 @@ public function guess($path)
throw new AccessDeniedException($path);
}

if (!$this->guessers) {
$msg = 'Unable to guess the mime type as no guessers are available';
if (!FileinfoMimeTypeGuesser::isSupported()) {
$msg .= ' (Did you enable the php_fileinfo extension?)';
}
throw new \LogicException($msg);
}

foreach ($this->guessers as $guesser) {
if (null !== $mimeType = $guesser->guess($path)) {
return $mimeType;
}
}

if (2 === \count($this->guessers) && !FileBinaryMimeTypeGuesser::isSupported() && !FileinfoMimeTypeGuesser::isSupported()) {
throw new \LogicException('Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)');
}
}
}

0 comments on commit 92f35ae

Please sign in to comment.