[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/File/MimeType/ -> ExtensionGuesser.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpFoundation\File\MimeType;
  13  
  14  /**
  15   * A singleton mime type to file extension guesser.
  16   *
  17   * A default guesser is provided.
  18   * You can register custom guessers by calling the register()
  19   * method on the singleton instance:
  20   *
  21   *     $guesser = ExtensionGuesser::getInstance();
  22   *     $guesser->register(new MyCustomExtensionGuesser());
  23   *
  24   * The last registered guesser is preferred over previously registered ones.
  25   */
  26  class ExtensionGuesser implements ExtensionGuesserInterface
  27  {
  28      /**
  29       * The singleton instance.
  30       *
  31       * @var ExtensionGuesser
  32       */
  33      private static $instance = null;
  34  
  35      /**
  36       * All registered ExtensionGuesserInterface instances.
  37       *
  38       * @var array
  39       */
  40      protected $guessers = array();
  41  
  42      /**
  43       * Returns the singleton instance.
  44       *
  45       * @return self
  46       */
  47      public static function getInstance()
  48      {
  49          if (null === self::$instance) {
  50              self::$instance = new self();
  51          }
  52  
  53          return self::$instance;
  54      }
  55  
  56      /**
  57       * Registers all natively provided extension guessers.
  58       */
  59      private function __construct()
  60      {
  61          $this->register(new MimeTypeExtensionGuesser());
  62      }
  63  
  64      /**
  65       * Registers a new extension guesser.
  66       *
  67       * When guessing, this guesser is preferred over previously registered ones.
  68       */
  69      public function register(ExtensionGuesserInterface $guesser)
  70      {
  71          array_unshift($this->guessers, $guesser);
  72      }
  73  
  74      /**
  75       * Tries to guess the extension.
  76       *
  77       * The mime type is passed to each registered mime type guesser in reverse order
  78       * of their registration (last registered is queried first). Once a guesser
  79       * returns a value that is not NULL, this method terminates and returns the
  80       * value.
  81       *
  82       * @param string $mimeType The mime type
  83       *
  84       * @return string The guessed extension or NULL, if none could be guessed
  85       */
  86      public function guess($mimeType)
  87      {
  88          foreach ($this->guessers as $guesser) {
  89              if (null !== $extension = $guesser->guess($mimeType)) {
  90                  return $extension;
  91              }
  92          }
  93      }
  94  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1