4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\File\MimeType;
14 use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
18 * Guesses the mime type using the PECL extension FileInfo.
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
27 * @param string $magicFile A magic file to use with the finfo instance
29 * @see http://www.php.net/manual/en/function.finfo-open.php
31 public function __construct($magicFile = null)
33 $this->magicFile = $magicFile;
37 * Returns whether this guesser is supported on the current OS/PHP setup.
41 public static function isSupported()
43 return \function_exists('finfo_open');
49 public function guess($path)
51 if (!is_file($path)) {
52 throw new FileNotFoundException($path);
55 if (!is_readable($path)) {
56 throw new AccessDeniedException($path);
59 if (!self::isSupported()) {
63 if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
67 return $finfo->file($path);