Пример #1
0
 /**
  * Registers all natively provided mime type guessers.
  */
 private function __construct()
 {
     $this->guessers = [];
     if (FileInfoMimeType::isSupported()) {
         $this->guessers[] = new FileInfoMimeType();
     }
     if (BinaryMimeType::isSupported()) {
         $this->guessers[] = new BinaryMimeType();
     }
 }
Пример #2
0
 /**
  * @param string $path The path to the file
  *
  * @return string The mime type or NULL, if none could be guessed
  *
  * @throws \LogicException
  * @throws FileNotFoundException
  * @throws AccessDeniedException
  */
 public function guess($path)
 {
     if (!is_file($path)) {
         throw new FileNotFoundException($path);
     }
     if (!is_readable($path)) {
         throw new AccessDeniedException($path);
     }
     if (0 == count($this->guessers)) {
         $msg = 'Unable to guess the mime type as no guessers are available';
         if (!FileInfoMimeType::isSupported()) {
             $msg .= ' (Did you enable the php_fileinfo extension?)';
         }
         throw new \LogicException($msg);
     }
     /** @var FileInfoMimeType|BinaryMimeType $guesser */
     foreach ($this->guessers as $guesser) {
         if (null !== ($mimeType = $guesser->guess($path))) {
             return $mimeType;
         }
     }
     return null;
 }