class MimeTypeGuesser
Same name in this branch
- 10 core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser
Same name in other branches
- 9 core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php \Drupal\Core\File\MimeType\MimeTypeGuesser
- 9 core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser
- 8.9.x core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php \Drupal\Core\File\MimeType\MimeTypeGuesser
- 8.9.x core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser
- 11.x core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php \Drupal\Core\File\MimeType\MimeTypeGuesser
- 11.x core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser
Defines a MIME type guesser that also supports stream wrapper paths.
Hierarchy
- class \Drupal\Core\File\MimeType\MimeTypeGuesser implements \Symfony\Component\Mime\MimeTypeGuesserInterface
Expanded class hierarchy of MimeTypeGuesser
2 files declare their use of MimeTypeGuesser
- DrupalKernel.php in core/
lib/ Drupal/ Core/ DrupalKernel.php - FileUploadResource.php in core/
modules/ file/ src/ Plugin/ rest/ resource/ FileUploadResource.php
1 string reference to 'MimeTypeGuesser'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses MimeTypeGuesser
File
-
core/
lib/ Drupal/ Core/ File/ MimeType/ MimeTypeGuesser.php, line 13
Namespace
Drupal\Core\File\MimeTypeView source
class MimeTypeGuesser implements MimeTypeGuesserInterface {
/**
* An array of arrays of registered guessers keyed by priority.
*
* @var array
*/
protected $guessers = [];
/**
* Holds the array of guessers sorted by priority.
*
* If this is NULL a rebuild will be triggered.
*
* @var \Symfony\Component\Mime\MimeTypeGuesserInterface[]
*
* @see \Drupal\Core\File\MimeType\MimeTypeGuesser::addGuesser()
* @see \Drupal\Core\File\MimeType\MimeTypeGuesser::sortGuessers()
*/
protected $sortedGuessers = NULL;
/**
* The stream wrapper manager.
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
/**
* Constructs a MimeTypeGuesser object.
*
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
* The stream wrapper manager.
*/
public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) {
$this->streamWrapperManager = $stream_wrapper_manager;
}
/**
* {@inheritdoc}
*/
public function guessMimeType(string $path) : ?string {
if ($wrapper = $this->streamWrapperManager
->getViaUri($path)) {
// Get the real path from the stream wrapper, if available. Files stored
// in remote file systems will not have one.
$real_path = $wrapper->realpath();
if ($real_path !== FALSE) {
$path = $real_path;
}
}
if ($this->sortedGuessers === NULL) {
// Sort is not triggered yet.
$this->sortedGuessers = $this->sortGuessers();
}
foreach ($this->sortedGuessers as $guesser) {
$mime_type = $guesser->guessMimeType($path);
if ($mime_type !== NULL) {
return $mime_type;
}
}
return 'application/octet-stream';
}
/**
* Appends a MIME type guesser to the guessers chain.
*
* @param \Symfony\Component\Mime\MimeTypeGuesserInterface $guesser
* The guesser to be appended.
* @param int $priority
* The priority of the guesser being added.
*
* @return $this
*/
public function addMimeTypeGuesser(MimeTypeGuesserInterface $guesser, $priority = 0) {
if ($guesser->isGuesserSupported()) {
$this->guessers[$priority][] = $guesser;
// Mark sorted guessers for rebuild.
$this->sortedGuessers = NULL;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function isGuesserSupported() : bool {
return TRUE;
}
/**
* Sorts guessers according to priority.
*
* @return \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface[]
* A sorted array of MIME type guesser objects.
*/
protected function sortGuessers() {
krsort($this->guessers);
return array_merge(...$this->guessers);
}
/**
* A helper function to register with Symfony's singleton MIME type guesser.
*
* Symfony's default mimetype guessers have dependencies on PHP's fileinfo
* extension or being able to run the system command file. Drupal's guesser
* does not have these dependencies.
*
* @see \Symfony\Component\Mime\MimeTypes
*/
public static function registerWithSymfonyGuesser(ContainerInterface $container) {
$guesser = new MimeTypes();
$guesser->registerGuesser($container->get('file.mime_type.guesser'));
MimeTypes::setDefault($guesser);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
MimeTypeGuesser::$guessers | protected | property | An array of arrays of registered guessers keyed by priority. |
MimeTypeGuesser::$sortedGuessers | protected | property | Holds the array of guessers sorted by priority. |
MimeTypeGuesser::$streamWrapperManager | protected | property | The stream wrapper manager. |
MimeTypeGuesser::addMimeTypeGuesser | public | function | Appends a MIME type guesser to the guessers chain. |
MimeTypeGuesser::guessMimeType | public | function | |
MimeTypeGuesser::isGuesserSupported | public | function | |
MimeTypeGuesser::registerWithSymfonyGuesser | public static | function | A helper function to register with Symfony's singleton MIME type guesser. |
MimeTypeGuesser::sortGuessers | protected | function | Sorts guessers according to priority. |
MimeTypeGuesser::__construct | public | function | Constructs a MimeTypeGuesser object. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.