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 | |
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')
-rw-r--r-- | phpBB/phpbb/mimetype/content_guesser.php | 2 | ||||
-rw-r--r-- | phpBB/phpbb/mimetype/extension_guesser.php | 2 | ||||
-rw-r--r-- | phpBB/phpbb/mimetype/guesser.php | 35 | ||||
-rw-r--r-- | phpBB/phpbb/mimetype/guesser_base.php | 46 | ||||
-rw-r--r-- | phpBB/phpbb/mimetype/guesser_interface.php | 16 |
5 files changed, 98 insertions, 3 deletions
diff --git a/phpBB/phpbb/mimetype/content_guesser.php b/phpBB/phpbb/mimetype/content_guesser.php index 21631ae6d3..ffaed9136a 100644 --- a/phpBB/phpbb/mimetype/content_guesser.php +++ b/phpBB/phpbb/mimetype/content_guesser.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * @package mimetype */ -class content_guesser implements guesser_interface +class content_guesser extends guesser_base { /** * @inheritdoc diff --git a/phpBB/phpbb/mimetype/extension_guesser.php b/phpBB/phpbb/mimetype/extension_guesser.php index 8cca974efc..f9459c84ec 100644 --- a/phpBB/phpbb/mimetype/extension_guesser.php +++ b/phpBB/phpbb/mimetype/extension_guesser.php @@ -21,7 +21,7 @@ if (!defined('IN_PHPBB')) * @package mimetype */ -class extension_guesser implements guesser_interface +class extension_guesser extends guesser_base { /** * @var file extension map 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; } /** diff --git a/phpBB/phpbb/mimetype/guesser_base.php b/phpBB/phpbb/mimetype/guesser_base.php new file mode 100644 index 0000000000..b35badea54 --- /dev/null +++ b/phpBB/phpbb/mimetype/guesser_base.php @@ -0,0 +1,46 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\mimetype; + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* @package mimetype +*/ + +abstract class guesser_base implements guesser_interface +{ + /** + * @var int Guesser Priority + */ + protected $priority; + + /** + * @inheritdoc + */ + public function get_priority() + { + return $this->priority; + } + + /** + * @inheritdoc + */ + public function set_priority($priority) + { + $this->priority = $priority; + } +} diff --git a/phpBB/phpbb/mimetype/guesser_interface.php b/phpBB/phpbb/mimetype/guesser_interface.php index a9b238d7aa..defff90654 100644 --- a/phpBB/phpbb/mimetype/guesser_interface.php +++ b/phpBB/phpbb/mimetype/guesser_interface.php @@ -38,4 +38,20 @@ interface guesser_interface * @return string Guess for mimetype of file */ public function guess($file, $file_name = ''); + + /** + * Get the guesser priority + * + * @return int Guesser priority + */ + public function get_priority(); + + /** + * Set the guesser priority + * + * @param int Guesser priority + * + * @return void + */ + public function set_priority($priority); } |