mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-07 20:08:53 +00:00
[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
This commit is contained in:
parent
b1719db47d
commit
bef6a5a640
7 changed files with 124 additions and 3 deletions
|
@ -1,3 +1,10 @@
|
|||
parameters:
|
||||
mimetype.guesser.priority.lowest: -2
|
||||
mimetype.guesser.priority.low: -1
|
||||
mimetype.guesser.priority.default: 0
|
||||
mimetype.guesser.priority.high: 1
|
||||
mimetype.guesser.priority.highest: 2
|
||||
|
||||
services:
|
||||
mimetype.fileinfo_mimetype_guesser:
|
||||
class: Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser
|
||||
|
@ -11,11 +18,15 @@ services:
|
|||
|
||||
mimetype.content_guesser:
|
||||
class: phpbb\mimetype\content_guesser
|
||||
calls:
|
||||
- [set_priority, [%mimetype.guesser.priority.low%]]
|
||||
tags:
|
||||
- { name: mimetype.guessers }
|
||||
|
||||
mimetype.extension_guesser:
|
||||
class: phpbb\mimetype\extension_guesser
|
||||
calls:
|
||||
- [set_priority, [%mimetype.guesser.priority.lowest%]]
|
||||
tags:
|
||||
- { name: mimetype.guessers }
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ if (!defined('IN_PHPBB'))
|
|||
* @package mimetype
|
||||
*/
|
||||
|
||||
class content_guesser implements guesser_interface
|
||||
class content_guesser extends guesser_base
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -23,6 +23,11 @@ if (!defined('IN_PHPBB'))
|
|||
|
||||
class guesser
|
||||
{
|
||||
/**
|
||||
* @const Default priority for mimetype guessers
|
||||
*/
|
||||
const PRIORITY_DEFAULT = 0;
|
||||
|
||||
/**
|
||||
* @var mimetype 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
46
phpBB/phpbb/mimetype/guesser_base.php
Normal file
46
phpBB/phpbb/mimetype/guesser_base.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -135,4 +135,19 @@ class guesser_test extends \phpbb_test_case
|
|||
$this->assertEquals($expected[1], $guesser->guess($this->jpg_file . '.jpg'));
|
||||
@unlink($this->jpg_file . '.jpg');
|
||||
}
|
||||
|
||||
public function test_sort_priority()
|
||||
{
|
||||
$guessers = array(
|
||||
'FileinfoMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser,
|
||||
'extension_guesser' => new \phpbb\mimetype\extension_guesser,
|
||||
'FileBinaryMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser,
|
||||
'content_guesser' => new \phpbb\mimetype\content_guesser,
|
||||
);
|
||||
$guessers['content_guesser']->set_priority(5);
|
||||
$guessers['extension_guesser']->set_priority(-5);
|
||||
usort($guessers, array($this->guesser, 'sort_priority'));
|
||||
$this->assertInstanceOf('\phpbb\mimetype\content_guesser', $guessers[0]);
|
||||
$this->assertInstanceOf('\phpbb\mimetype\extension_guesser', $guessers[3]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue