You are here

class ExtensionGuesser in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-foundation/File/MimeType/ExtensionGuesser.php \Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser

A singleton mime type to file extension guesser.

A default guesser is provided. You can register custom guessers by calling the register() method on the singleton instance:

$guesser = ExtensionGuesser::getInstance(); $guesser->register(new MyCustomExtensionGuesser());

The last registered guesser is preferred over previously registered ones.

Hierarchy

Expanded class hierarchy of ExtensionGuesser

2 files declare their use of ExtensionGuesser
File.php in vendor/symfony/http-foundation/File/File.php
UploadedFile.php in vendor/symfony/http-foundation/File/UploadedFile.php

File

vendor/symfony/http-foundation/File/MimeType/ExtensionGuesser.php, line 26

Namespace

Symfony\Component\HttpFoundation\File\MimeType
View source
class ExtensionGuesser implements ExtensionGuesserInterface {

  /**
   * The singleton instance.
   *
   * @var ExtensionGuesser
   */
  private static $instance = null;

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

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

  /**
   * Registers all natively provided extension guessers.
   */
  private function __construct() {
    $this
      ->register(new MimeTypeExtensionGuesser());
  }

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

  /**
   * Tries to guess the extension.
   *
   * The mime type 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 $mimeType The mime type
   *
   * @return string The guessed extension or NULL, if none could be guessed
   */
  public function guess($mimeType) {
    foreach ($this->guessers as $guesser) {
      if (null !== ($extension = $guesser
        ->guess($mimeType))) {
        return $extension;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExtensionGuesser::$guessers protected property All registered ExtensionGuesserInterface instances.
ExtensionGuesser::$instance private static property The singleton instance.
ExtensionGuesser::getInstance public static function Returns the singleton instance.
ExtensionGuesser::guess public function Tries to guess the extension. Overrides ExtensionGuesserInterface::guess
ExtensionGuesser::register public function Registers a new extension guesser.
ExtensionGuesser::__construct private function Registers all natively provided extension guessers.