[HttpFoundation] added a way to inject a custom magic file into FileinfoMimeTypeGuesser (closes #6963)

This commit is contained in:
Fabien Potencier 2013-04-23 09:31:45 +02:00
parent 29b5413134
commit 1aa68dab07
2 changed files with 24 additions and 8 deletions

View File

@ -15,12 +15,26 @@ use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException; use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
/** /**
* Guesses the mime type using the PECL extension FileInfo * Guesses the mime type using the PECL extension FileInfo.
* *
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
*/ */
class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
{ {
private $magicFile;
/**
* Constructor.
*
* @param string $magicFile A magic file to use with the finfo instance
*
* @link http://www.php.net/manual/en/function.finfo-open.php
*/
public function __construct($magicFile = null)
{
$this->magicFile = $magicFile;
}
/** /**
* Returns whether this guesser is supported on the current OS/PHP setup * Returns whether this guesser is supported on the current OS/PHP setup
* *
@ -48,7 +62,7 @@ class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
return null; return null;
} }
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE)) { if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
return null; return null;
} }

View File

@ -23,15 +23,17 @@ use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
* You can register custom guessers by calling the register() method on the * You can register custom guessers by calling the register() method on the
* singleton instance. Custom guessers are always called before any default ones. * singleton instance. Custom guessers are always called before any default ones.
* *
* $guesser = MimeTypeGuesser::getInstance();
* $guesser->register(new MyCustomMimeTypeGuesser());
*
* If you want to change the order of the default guessers, just re-register your * If you want to change the order of the default guessers, just re-register your
* preferred one as a custom one. * preferred one as a custom one. The last registered guesser is preferred over
* previously registered ones.
* *
* <code> * Re-registering a built-in guesser also allows you to configure it:
* $guesser = MimeTypeGuesser::getInstance();
* $guesser->register(new MyCustomMimeTypeGuesser());
* </code>
* *
* The last registered guesser is preferred over previously registered ones. * $guesser = MimeTypeGuesser::getInstance();
* $guesser->register(new FileinfoMimeTypeGuesser('/path/to/magic/file'));
* *
* @author Bernhard Schussek <bschussek@gmail.com> * @author Bernhard Schussek <bschussek@gmail.com>
*/ */