class MimeTypeGuesser in Zircon Profile 8.0
Same name in this branch
- 8.0 vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser
- 8.0 core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php \Drupal\Core\File\MimeType\MimeTypeGuesser
- 8.0 core/lib/Drupal/Core/ProxyClass/File/MimeType/MimeTypeGuesser.php \Drupal\Core\ProxyClass\File\MimeType\MimeTypeGuesser
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/File/MimeType/MimeTypeGuesser.php \Drupal\Core\File\MimeType\MimeTypeGuesser
Defines a MIME type guesser that also supports stream wrapper paths.
Hierarchy
- class \Drupal\Core\File\MimeType\MimeTypeGuesser implements MimeTypeGuesserInterface
Expanded class hierarchy of MimeTypeGuesser
2 files declare their use of MimeTypeGuesser
- DrupalKernel.php in core/
lib/ Drupal/ Core/ DrupalKernel.php - Contains \Drupal\Core\DrupalKernel.
- MimeTypeGuesserTest.php in core/
tests/ Drupal/ Tests/ Core/ File/ MimeTypeGuesserTest.php - Contains \Drupal\Tests\Core\File\MimeTypeGuesserTest.
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 18 - Contains \Drupal\Core\File\MimeType\MimeTypeGuesser.
Namespace
Drupal\Core\File\MimeTypeView source
class MimeTypeGuesser implements MimeTypeGuesserInterface {
/**
* An array of arrays of registered guessers keyed by priority.
*
* @var array
*/
protected $guessers = array();
/**
* Holds the array of guessers sorted by priority.
*
* If this is NULL a rebuild will be triggered.
*
* @var \Symfony\Component\HttpFoundation\File\MimeType\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 StreamWrapperManagerInterface $streamWrapperManager
* The stream wrapper manager.
*/
public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager) {
$this->streamWrapperManager = $stream_wrapper_manager;
}
/**
* {@inheritdoc}
*/
public function guess($path) {
if ($wrapper = $this->streamWrapperManager
->getViaUri($path)) {
// Get the real path from the stream wrapper.
$path = $wrapper
->realpath();
}
if ($this->sortedGuessers === NULL) {
// Sort is not triggered yet.
$this->sortedGuessers = $this
->sortGuessers();
}
foreach ($this->sortedGuessers as $guesser) {
$mime_type = $guesser
->guess($path);
if ($mime_type !== NULL) {
return $mime_type;
}
}
}
/**
* Appends a MIME type guesser to the guessers chain.
*
* @param \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $guesser
* The guesser to be appended.
* @param int $priority
* The priority of the guesser being added.
*
* @return $this
*/
public function addGuesser(MimeTypeGuesserInterface $guesser, $priority = 0) {
$this->guessers[$priority][] = $guesser;
// Mark sorted guessers for rebuild.
$this->sortedGuessers = NULL;
return $this;
}
/**
* Sorts guessers according to priority.
*
* @return \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface[]
* A sorted array of MIME type guesser objects.
*/
protected function sortGuessers() {
$sorted = array();
krsort($this->guessers);
foreach ($this->guessers as $guesser) {
$sorted = array_merge($sorted, $guesser);
}
return $sorted;
}
/**
* 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\HttpFoundation\File\MimeType\MimeTypeGuesser
*/
public static function registerWithSymfonyGuesser(ContainerInterface $container) {
// Reset state, so we do not store more and more services during test runs.
SymfonyMimeTypeGuesser::reset();
$singleton = SymfonyMimeTypeGuesser::getInstance();
$singleton
->register($container
->get('file.mime_type.guesser'));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MimeTypeGuesser:: |
protected | property | An array of arrays of registered guessers keyed by priority. | |
MimeTypeGuesser:: |
protected | property | Holds the array of guessers sorted by priority. | |
MimeTypeGuesser:: |
protected | property | The stream wrapper manager. | |
MimeTypeGuesser:: |
public | function | Appends a MIME type guesser to the guessers chain. | |
MimeTypeGuesser:: |
public | function |
Guesses the mime type of the file with the given path. Overrides MimeTypeGuesserInterface:: |
|
MimeTypeGuesser:: |
public static | function | A helper function to register with Symfony's singleton mime type guesser. | |
MimeTypeGuesser:: |
protected | function | Sorts guessers according to priority. | |
MimeTypeGuesser:: |
public | function | Constructs a MimeTypeGuesser object. |