Skip to content

Commit

Permalink
[HttpFoundation] added a way to inject a custom magic file into Filei…
Browse files Browse the repository at this point in the history
…nfoMimeTypeGuesser (closes #6963)
  • Loading branch information
fabpot committed Apr 23, 2013
1 parent 29b5413 commit 1aa68da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Expand Up @@ -15,12 +15,26 @@
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>
*/
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
*
Expand Down Expand Up @@ -48,7 +62,7 @@ public function guess($path)
return null;
}

if (!$finfo = new \finfo(FILEINFO_MIME_TYPE)) {
if (!$finfo = new \finfo(FILEINFO_MIME_TYPE, $this->magicFile)) {
return null;
}

Expand Down
Expand Up @@ -23,15 +23,17 @@
* You can register custom guessers by calling the register() method on the
* 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
* preferred one as a custom one.
* preferred one as a custom one. The last registered guesser is preferred over
* previously registered ones.
*
* <code>
* $guesser = MimeTypeGuesser::getInstance();
* $guesser->register(new MyCustomMimeTypeGuesser());
* </code>
* Re-registering a built-in guesser also allows you to configure it:
*
* 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>
*/
Expand Down

0 comments on commit 1aa68da

Please sign in to comment.