[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

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


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1