Classes List

Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser

 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 
<?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;

/** * 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. */
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 self */
    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. */
    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;
            }
        }
    }
}