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\FileNotFoundException;
15 use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
18 * Guesses the mime type using the PECL extension FileInfo.
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
29 * @param string $magicFile A magic file to use with the finfo instance
31 * @see http://www.php.net/manual/en/function.finfo-open.php
33 public function __construct($magicFile = null)
35 $this->magicFile = $magicFile;
39 * Returns whether this guesser is supported on the current OS/PHP setup.
43 public static function isSupported()
45 return function_exists('finfo_open');
51 public function guess($path)
53 if (!is_file($path)) {
54 throw new FileNotFoundException($path);
57 if (!is_readable($path)) {
58 throw new AccessDeniedException($path);
61 if (!self::isSupported()) {
65 if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
69 return $finfo->file($path);