vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php line 18

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpFoundation\File\MimeType;
  11. use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
  12. use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  13. use Symfony\Component\Mime\MimeTypes;
  14. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.'MimeTypeGuesser::class, MimeTypes::class), \E_USER_DEPRECATED);
  15. /**
  16.  * A singleton mime type guesser.
  17.  *
  18.  * By default, all mime type guessers provided by the framework are installed
  19.  * (if available on the current OS/PHP setup).
  20.  *
  21.  * You can register custom guessers by calling the register() method on the
  22.  * singleton instance. Custom guessers are always called before any default ones.
  23.  *
  24.  *     $guesser = MimeTypeGuesser::getInstance();
  25.  *     $guesser->register(new MyCustomMimeTypeGuesser());
  26.  *
  27.  * If you want to change the order of the default guessers, just re-register your
  28.  * preferred one as a custom one. The last registered guesser is preferred over
  29.  * previously registered ones.
  30.  *
  31.  * Re-registering a built-in guesser also allows you to configure it:
  32.  *
  33.  *     $guesser = MimeTypeGuesser::getInstance();
  34.  *     $guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file'));
  35.  *
  36.  * @author Bernhard Schussek <bschussek@gmail.com>
  37.  */
  38. class MimeTypeGuesser implements MimeTypeGuesserInterface
  39. {
  40.     /**
  41.      * The singleton instance.
  42.      *
  43.      * @var MimeTypeGuesser
  44.      */
  45.     private static $instance null;
  46.     /**
  47.      * All registered MimeTypeGuesserInterface instances.
  48.      *
  49.      * @var array
  50.      */
  51.     protected $guessers = [];
  52.     /**
  53.      * Returns the singleton instance.
  54.      *
  55.      * @return self
  56.      */
  57.     public static function getInstance()
  58.     {
  59.         if (null === self::$instance) {
  60.             self::$instance = new self();
  61.         }
  62.         return self::$instance;
  63.     }
  64.     /**
  65.      * Resets the singleton instance.
  66.      */
  67.     public static function reset()
  68.     {
  69.         self::$instance null;
  70.     }
  71.     /**
  72.      * Registers all natively provided mime type guessers.
  73.      */
  74.     private function __construct()
  75.     {
  76.         $this->register(new FileBinaryMimeTypeGuesser());
  77.         $this->register(new FileinfoMimeTypeGuesser());
  78.     }
  79.     /**
  80.      * Registers a new mime type guesser.
  81.      *
  82.      * When guessing, this guesser is preferred over previously registered ones.
  83.      */
  84.     public function register(MimeTypeGuesserInterface $guesser)
  85.     {
  86.         array_unshift($this->guessers$guesser);
  87.     }
  88.     /**
  89.      * Tries to guess the mime type of the given file.
  90.      *
  91.      * The file is passed to each registered mime type guesser in reverse order
  92.      * of their registration (last registered is queried first). Once a guesser
  93.      * returns a value that is not NULL, this method terminates and returns the
  94.      * value.
  95.      *
  96.      * @param string $path The path to the file
  97.      *
  98.      * @return string The mime type or NULL, if none could be guessed
  99.      *
  100.      * @throws \LogicException
  101.      * @throws FileNotFoundException
  102.      * @throws AccessDeniedException
  103.      */
  104.     public function guess($path)
  105.     {
  106.         if (!is_file($path)) {
  107.             throw new FileNotFoundException($path);
  108.         }
  109.         if (!is_readable($path)) {
  110.             throw new AccessDeniedException($path);
  111.         }
  112.         foreach ($this->guessers as $guesser) {
  113.             if (null !== $mimeType $guesser->guess($path)) {
  114.                 return $mimeType;
  115.             }
  116.         }
  117.         if (=== \count($this->guessers) && !FileBinaryMimeTypeGuesser::isSupported() && !FileinfoMimeTypeGuesser::isSupported()) {
  118.             throw new \LogicException('Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?).');
  119.         }
  120.         return null;
  121.     }
  122. }