Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

MimeTypeGuesser.php

Zuletzt modifiziert: 09.10.2024, 12:57 - Dateigröße: 3.97 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\HttpFoundation\File\MimeType;
013   
014  use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
015  use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
016   
017  /**
018   * A singleton mime type guesser.
019   *
020   * By default, all mime type guessers provided by the framework are installed
021   * (if available on the current OS/PHP setup).
022   *
023   * You can register custom guessers by calling the register() method on the
024   * singleton instance. Custom guessers are always called before any default ones.
025   *
026   *     $guesser = MimeTypeGuesser::getInstance();
027   *     $guesser->register(new MyCustomMimeTypeGuesser());
028   *
029   * If you want to change the order of the default guessers, just re-register your
030   * preferred one as a custom one. The last registered guesser is preferred over
031   * previously registered ones.
032   *
033   * Re-registering a built-in guesser also allows you to configure it:
034   *
035   *     $guesser = MimeTypeGuesser::getInstance();
036   *     $guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file'));
037   *
038   * @author Bernhard Schussek <bschussek@gmail.com>
039   */
040  class MimeTypeGuesser implements MimeTypeGuesserInterface
041  {
042      /**
043       * The singleton instance.
044       *
045       * @var MimeTypeGuesser
046       */
047      private static $instance = null;
048   
049      /**
050       * All registered MimeTypeGuesserInterface instances.
051       *
052       * @var array
053       */
054      protected $guessers = array();
055   
056      /**
057       * Returns the singleton instance.
058       *
059       * @return MimeTypeGuesser
060       */
061      public static function getInstance()
062      {
063          if (null === self::$instance) {
064              self::$instance = new self();
065          }
066   
067          return self::$instance;
068      }
069   
070      /**
071       * Resets the singleton instance.
072       */
073      public static function reset()
074      {
075          self::$instance = null;
076      }
077   
078      /**
079       * Registers all natively provided mime type guessers.
080       */
081      private function __construct()
082      {
083          if (FileBinaryMimeTypeGuesser::isSupported()) {
084              $this->register(new FileBinaryMimeTypeGuesser());
085          }
086   
087          if (FileinfoMimeTypeGuesser::isSupported()) {
088              $this->register(new FileinfoMimeTypeGuesser());
089          }
090      }
091   
092      /**
093       * Registers a new mime type guesser.
094       *
095       * When guessing, this guesser is preferred over previously registered ones.
096       *
097       * @param MimeTypeGuesserInterface $guesser
098       */
099      public function register(MimeTypeGuesserInterface $guesser)
100      {
101          array_unshift($this->guessers, $guesser);
102      }
103   
104      /**
105       * Tries to guess the mime type of the given file.
106       *
107       * The file is passed to each registered mime type guesser in reverse order
108       * of their registration (last registered is queried first). Once a guesser
109       * returns a value that is not NULL, this method terminates and returns the
110       * value.
111       *
112       * @param string $path The path to the file
113       *
114       * @return string The mime type or NULL, if none could be guessed
115       *
116       * @throws \LogicException
117       * @throws FileNotFoundException
118       * @throws AccessDeniedException
119       */
120      public function guess($path)
121      {
122          if (!is_file($path)) {
123              throw new FileNotFoundException($path);
124          }
125   
126          if (!is_readable($path)) {
127              throw new AccessDeniedException($path);
128          }
129   
130          if (!$this->guessers) {
131              $msg = 'Unable to guess the mime type as no guessers are available';
132              if (!FileinfoMimeTypeGuesser::isSupported()) {
133                  $msg .= ' (Did you enable the php_fileinfo extension?)';
134              }
135              throw new \LogicException($msg);
136          }
137   
138          foreach ($this->guessers as $guesser) {
139              if (null !== $mimeType = $guesser->guess($path)) {
140                  return $mimeType;
141              }
142          }
143      }
144  }
145