Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
MimeTypeGuesser.php
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\AccessDeniedException;
015 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
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 = [];
055
056 /**
057 * Returns the singleton instance.
058 *
059 * @return self
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 $this->register(new FileBinaryMimeTypeGuesser());
084 $this->register(new FileinfoMimeTypeGuesser());
085 }
086
087 /**
088 * Registers a new mime type guesser.
089 *
090 * When guessing, this guesser is preferred over previously registered ones.
091 */
092 public function register(MimeTypeGuesserInterface $guesser)
093 {
094 array_unshift($this->guessers, $guesser);
095 }
096
097 /**
098 * Tries to guess the mime type of the given file.
099 *
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 }
136