You are here

class MimeTypeGuesser in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
  2. 8 core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php \Drupal\Core\File\MimeType\MimeTypeGuesser
  3. 8 core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser
Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser

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>

Hierarchy

Expanded class hierarchy of MimeTypeGuesser

5 files declare their use of MimeTypeGuesser
File.php in vendor/symfony/http-foundation/File/File.php
FileTest.php in vendor/symfony/http-foundation/Tests/File/FileTest.php
MimeTypeGuesser.php in core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php
Contains \Drupal\Core\File\MimeType\MimeTypeGuesser.
MimeTypeGuesserTest.php in core/tests/Drupal/Tests/Core/File/MimeTypeGuesserTest.php
Contains \Drupal\Tests\Core\File\MimeTypeGuesserTest.
MimeTypeTest.php in vendor/symfony/http-foundation/Tests/File/MimeType/MimeTypeTest.php

File

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

Namespace

Symfony\Component\HttpFoundation\File\MimeType
View source
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 MimeTypeGuesser
   */
  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.
   *
   * @param MimeTypeGuesserInterface $guesser
   */
  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;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MimeTypeGuesser::$guessers protected property All registered MimeTypeGuesserInterface instances.
MimeTypeGuesser::$instance private static property The singleton instance.
MimeTypeGuesser::getInstance public static function Returns the singleton instance.
MimeTypeGuesser::guess public function Tries to guess the mime type of the given file. Overrides MimeTypeGuesserInterface::guess
MimeTypeGuesser::register public function Registers a new mime type guesser.
MimeTypeGuesser::reset public static function Resets the singleton instance.
MimeTypeGuesser::__construct private function Registers all natively provided mime type guessers.