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 * A singleton mime type guesser.
20 * By default, all mime type guessers provided by the framework are installed
21 * (if available on the current OS/PHP setup).
23 * You can register custom guessers by calling the register() method on the
24 * singleton instance. Custom guessers are always called before any default ones.
26 * $guesser = MimeTypeGuesser::getInstance();
27 * $guesser->register(new MyCustomMimeTypeGuesser());
29 * If you want to change the order of the default guessers, just re-register your
30 * preferred one as a custom one. The last registered guesser is preferred over
31 * previously registered ones.
33 * Re-registering a built-in guesser also allows you to configure it:
35 * $guesser = MimeTypeGuesser::getInstance();
36 * $guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file'));
38 * @author Bernhard Schussek <bschussek@gmail.com>
40 class MimeTypeGuesser implements MimeTypeGuesserInterface
43 * The singleton instance.
45 * @var MimeTypeGuesser
47 private static $instance = null;
50 * All registered MimeTypeGuesserInterface instances.
54 protected $guessers = array();
57 * Returns the singleton instance.
61 public static function getInstance()
63 if (null === self::$instance) {
64 self::$instance = new self();
67 return self::$instance;
71 * Resets the singleton instance.
73 public static function reset()
75 self::$instance = null;
79 * Registers all natively provided mime type guessers.
81 private function __construct()
83 if (FileBinaryMimeTypeGuesser::isSupported()) {
84 $this->register(new FileBinaryMimeTypeGuesser());
87 if (FileinfoMimeTypeGuesser::isSupported()) {
88 $this->register(new FileinfoMimeTypeGuesser());
93 * Registers a new mime type guesser.
95 * When guessing, this guesser is preferred over previously registered ones.
97 public function register(MimeTypeGuesserInterface $guesser)
99 array_unshift($this->guessers, $guesser);
103 * Tries to guess the mime type of the given file.
105 * The file is passed to each registered mime type guesser in reverse order
106 * of their registration (last registered is queried first). Once a guesser
107 * returns a value that is not NULL, this method terminates and returns the
110 * @param string $path The path to the file
112 * @return string The mime type or NULL, if none could be guessed
114 * @throws \LogicException
115 * @throws FileNotFoundException
116 * @throws AccessDeniedException
118 public function guess($path)
120 if (!is_file($path)) {
121 throw new FileNotFoundException($path);
124 if (!is_readable($path)) {
125 throw new AccessDeniedException($path);
128 if (!$this->guessers) {
129 $msg = 'Unable to guess the mime type as no guessers are available';
130 if (!FileinfoMimeTypeGuesser::isSupported()) {
131 $msg .= ' (Did you enable the php_fileinfo extension?)';
133 throw new \LogicException($msg);
136 foreach ($this->guessers as $guesser) {
137 if (null !== $mimeType = $guesser->guess($path)) {