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 * 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 $this->register(new FileBinaryMimeTypeGuesser());
84 $this->register(new FileinfoMimeTypeGuesser());
88 * Registers a new mime type guesser.
90 * When guessing, this guesser is preferred over previously registered ones.
92 public function register(MimeTypeGuesserInterface $guesser)
94 array_unshift($this->guessers, $guesser);
98 * Tries to guess the mime type of the given file.
100 * The file is passed to each registered mime type guesser in reverse order
101 * of their registration (last registered is queried first). Once a guesser
102 * returns a value that is not NULL, this method terminates and returns the
105 * @param string $path The path to the file
107 * @return string The mime type or NULL, if none could be guessed
109 * @throws \LogicException
110 * @throws FileNotFoundException
111 * @throws AccessDeniedException
113 public function guess($path)
115 if (!is_file($path)) {
116 throw new FileNotFoundException($path);
119 if (!is_readable($path)) {
120 throw new AccessDeniedException($path);
123 foreach ($this->guessers as $guesser) {
124 if (null !== $mimeType = $guesser->guess($path)) {
129 if (2 === \count($this->guessers) && !FileBinaryMimeTypeGuesser::isSupported() && !FileinfoMimeTypeGuesser::isSupported()) {
130 throw new \LogicException('Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)');