Classes List

Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 
 108 
 109 
 110 
 111 
 112 
 113 
 114 
 115 
 116 
 117 
 118 
 119 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 130 
 131 
 132 
 133 
 134 
 135 
 136 
 137 
 138 
 139 
 140 
 141 
 142 
 143 
<?php

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\HttpFoundation\File\MimeType;

use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;

/** * A singleton mime type guesser. * * By default, all mime type guessers provided by the framework are installed * (if available on the current OS/PHP setup). * * You can register custom guessers by calling the register() method on the * singleton instance. Custom guessers are always called before any default ones. * * $guesser = MimeTypeGuesser::getInstance(); * $guesser->register(new MyCustomMimeTypeGuesser()); * * If you want to change the order of the default guessers, just re-register your * preferred one as a custom one. The last registered guesser is preferred over * previously registered ones. * * Re-registering a built-in guesser also allows you to configure it: * * $guesser = MimeTypeGuesser::getInstance(); * $guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file')); * * @author Bernhard Schussek <bschussek@gmail.com> */
class MimeTypeGuesser implements MimeTypeGuesserInterface
{
    /** * The singleton instance. * * @var MimeTypeGuesser */
    private static $instance = null;

    /** * All registered MimeTypeGuesserInterface instances. * * @var array */
    protected $guessers = array();

    /** * Returns the singleton instance. * * @return self */
    public static function getInstance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /** * Resets the singleton instance. */
    public static function reset()
    {
        self::$instance = null;
    }

    /** * Registers all natively provided mime type guessers. */
    private function __construct()
    {
        if (FileBinaryMimeTypeGuesser::isSupported()) {
            $this->register(new FileBinaryMimeTypeGuesser());
        }

        if (FileinfoMimeTypeGuesser::isSupported()) {
            $this->register(new FileinfoMimeTypeGuesser());
        }
    }

    /** * Registers a new mime type guesser. * * When guessing, this guesser is preferred over previously registered ones. */
    public function register(MimeTypeGuesserInterface $guesser)
    {
        array_unshift($this->guessers$guesser);
    }

    /** * Tries to guess the mime type of the given file. * * The file is passed to each registered mime type guesser in reverse order * of their registration (last registered is queried first). Once a guesser * returns a value that is not NULL, this method terminates and returns the * value. * * @param string $path The path to the file * * @return string The mime type or NULL, if none could be guessed * * @throws \LogicException * @throws FileNotFoundException * @throws AccessDeniedException */
    public function guess($path)
    {
        if (!is_file($path)) {
            throw new FileNotFoundException($path);
        }

        if (!is_readable($path)) {
            throw new AccessDeniedException($path);
        }

        if (!$this->guessers) {
            $msg = 'Unable to guess the mime type as no guessers are available';
            if (!FileinfoMimeTypeGuesser::isSupported()) {
                $msg .= ' (Did you enable the php_fileinfo extension?)';
            }
            throw new \LogicException($msg);
        }

        foreach ($this->guessers as $guesser) {
            if (null !== $mimeType = $guesser->guess($path)) {
                return $mimeType;
            }
        }
    }
}