diff options
author | Marc Alexander <admin@m-a-styles.de> | 2013-11-11 21:18:23 +0100 |
---|---|---|
committer | Marc Alexander <admin@m-a-styles.de> | 2013-11-11 21:18:23 +0100 |
commit | bef6a5a6401314da7e5688907f4ebfc06ef83f2b (patch) | |
tree | 70605732b1549dea255cf4b2f95ca5b3d2fc2f7c /phpBB/phpbb/mimetype/guesser.php | |
parent | b1719db47df4f3089f90bbfac2ca0bec24dcf027 (diff) | |
download | forums-bef6a5a6401314da7e5688907f4ebfc06ef83f2b.tar forums-bef6a5a6401314da7e5688907f4ebfc06ef83f2b.tar.gz forums-bef6a5a6401314da7e5688907f4ebfc06ef83f2b.tar.bz2 forums-bef6a5a6401314da7e5688907f4ebfc06ef83f2b.tar.xz forums-bef6a5a6401314da7e5688907f4ebfc06ef83f2b.zip |
[ticket/11912] Introduce guesser priority to mimetype guessers
The mimetype guesser priority can now be set through the service definition.
Mimetypes will be guessed from the guesser with the highest priority to
the one with the lowest priority. Standard priority types have been added
to the service definition file. Any integer value can be used though.
Standard mimetype guessers that do not have the methods get_priority
and set_priority implemented, like the standard MimeTypeGuessers of symfony,
will have the default priority with the value of 0. Lower priority guessers
have values lower than 0 while high priority ones can be added with values
higher than 0.
PHPBB3-11912
Diffstat (limited to 'phpBB/phpbb/mimetype/guesser.php')
-rw-r--r-- | phpBB/phpbb/mimetype/guesser.php | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/phpBB/phpbb/mimetype/guesser.php b/phpBB/phpbb/mimetype/guesser.php index 231b75f604..753fd65b0d 100644 --- a/phpBB/phpbb/mimetype/guesser.php +++ b/phpBB/phpbb/mimetype/guesser.php @@ -24,6 +24,11 @@ if (!defined('IN_PHPBB')) class guesser { /** + * @const Default priority for mimetype guessers + */ + const PRIORITY_DEFAULT = 0; + + /** * @var mimetype guessers */ protected $guessers; @@ -39,7 +44,7 @@ class guesser } /** - * Register MimeTypeGuessers + * Register MimeTypeGuessers and sort them by priority * * @param array $mimetype_guessers Mimetype guesser service collection * @@ -68,6 +73,34 @@ class guesser { throw new \LogicException('No mimetype guesser supplied.'); } + + // Sort guessers by priority + usort($this->guessers, array($this, 'sort_priority')); + } + + /** + * Sort the priority of supplied guessers + * This is a compare function for usort. A guesser with higher priority + * should be used first and vice versa. usort() orders the array values + * from low to high depending on what the comparison function returns + * to it. Return value should be smaller than 0 if value a is smaller + * than value b. This has been reversed in the comparision function in + * order to sort the guessers from high to low. + * Method has been set to public in order to allow proper testing. + * + * @param object $guesser_a Mimetype guesser a + * @param object $guesser_b Mimetype guesser b + * + * @return int If both guessers have the same priority 0, bigger + * than 0 if first guesser has lower priority, and lower + * than 0 if first guesser has higher priority + */ + public function sort_priority($guesser_a, $guesser_b) + { + $priority_a = (int) (method_exists($guesser_a, 'get_priority')) ? $guesser_a->get_priority() : self::PRIORITY_DEFAULT; + $priority_b = (int) (method_exists($guesser_b, 'get_priority')) ? $guesser_b->get_priority() : self::PRIORITY_DEFAULT; + + return $priority_b - $priority_a; } /** |