1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\HttpFoundation\File\MimeType;
13:
14: use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
15: use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
16:
17: /**
18: * Guesses the mime type using the PECL extension FileInfo
19: *
20: * @author Bernhard Schussek <bschussek@gmail.com>
21: */
22: class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
23: {
24: /**
25: * Returns whether this guesser is supported on the current OS/PHP setup
26: *
27: * @return Boolean
28: */
29: public static function isSupported()
30: {
31: return function_exists('finfo_open');
32: }
33:
34: /**
35: * {@inheritdoc}
36: */
37: public function guess($path)
38: {
39: if (!is_file($path)) {
40: throw new FileNotFoundException($path);
41: }
42:
43: if (!is_readable($path)) {
44: throw new AccessDeniedException($path);
45: }
46:
47: if (!self::isSupported()) {
48: return null;
49: }
50:
51: if (!$finfo = new \finfo(FILEINFO_MIME_TYPE)) {
52: return null;
53: }
54:
55: return $finfo->file($path);
56: }
57: }
58: