removed content negotiation in favor of willdurand/negotiation

This commit is contained in:
Lukas Kahwe Smith
2013-07-26 16:30:31 +02:00
parent 1d0a0d5b3c
commit 1d610cc01b
4 changed files with 1 additions and 175 deletions

View File

@@ -1,49 +0,0 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Tests\Util;
use FOS\Rest\Util\FormatNegotiator;
use Symfony\Component\HttpFoundation\Request;
class FormatNegotiatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getData
*/
public function testGetBestFormat($acceptHeader, $format, $priorities, $preferExtension, $expected)
{
$request = new Request();
$request->headers->set('Accept', $acceptHeader);
$request->attributes->set('_format', $format);
$formatNegotiator = new FormatNegotiator();
$this->assertEquals($expected, $formatNegotiator->getBestFormat($request, $priorities, $preferExtension));
}
public function getData()
{
return array(
array(null, null, array('html', 'json', '*/*'), false, null),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', null, array('html', 'json', '*/*'), false, 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('html', 'json', '*/*'), false, 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('html', 'json', '*/*'), true, 'json'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('rss', '*/*'), false, 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('xml'), false, 'xml'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('json', 'xml'), false, 'xml'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('json'), false, 'json'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*', 'json', array('json'), false, 'json'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*', null, array('json'), false, 'json'),
array('text/html,application/xhtml+xml,application/xml', null, array('json'), false, null),
);
}}

View File

@@ -1,105 +0,0 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Util;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\AcceptHeader;
class FormatNegotiator implements FormatNegotiatorInterface
{
/**
* Detect the request format based on the priorities and the Accept header
*
* Note: Request "_format" parameter is considered the preferred Accept header
*
* @param Request $request The request
* @param array $priorities Ordered array of formats (highest priority first)
* @param Boolean $preferExtension If to consider the extension last or first
*
* @return void|string The format string
*/
public function getBestFormat(Request $request, array $priorities, $preferExtension = false)
{
// BC - Maintain this while 2.0 and 2.1 dont reach their end of life
// Note: Request::splitHttpAcceptHeader is deprecated since version 2.2, to be removed in 2.3.
if (class_exists('Symfony\Component\HttpFoundation\AcceptHeader')) {
$mimetypes = array();
foreach (AcceptHeader::fromString($request->headers->get('Accept'))->all() as $item) {
$mimetypes[$item->getValue()] = $item->getQuality();
}
} else {
$mimetypes = $request->splitHttpAcceptHeader($request->headers->get('Accept'));
}
$extension = $request->get('_format');
if (null !== $extension && $request->getMimeType($extension)) {
$mimetypes[$request->getMimeType($extension)] = $preferExtension
? reset($mimetypes)+1
: end($mimetypes)-1;
arsort($mimetypes);
}
if (empty($mimetypes)) {
return null;
}
$catchAllEnabled = in_array('*/*', $priorities);
return $this->getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled);
}
/**
* Get the format applying the supplied priorities to the mime types
*
* @param Request $request The request
* @param array $mimetypes Ordered array of mimetypes as keys with priroties s values
* @param array $priorities Ordered array of formats (highest priority first)
* @param Boolean $catchAllEnabled If there is a catch all priority
*
* @return void|string The format string
*/
protected function getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled = false)
{
$max = reset($mimetypes);
$keys = array_keys($mimetypes, $max);
$formats = array();
$catchAllMatched = false;
foreach ($keys as $mimetype) {
unset($mimetypes[$mimetype]);
if ($mimetype === '*/*') {
$catchAllMatched = true;
continue;
}
$format = $request->getFormat($mimetype);
if ($format) {
$priority = array_search($format, $priorities);
if (false !== $priority) {
$formats[$format] = $priority;
} elseif ($catchAllEnabled) {
$formats[$format] = count($priorities);
}
}
}
if (empty($formats) && $catchAllMatched) {
return reset($priorities);
}
if (empty($formats) && !empty($mimetypes)) {
return $this->getFormatByPriorities($request, $mimetypes, $priorities);
}
asort($formats);
return key($formats);
}
}

View File

@@ -1,19 +0,0 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Util;
use Symfony\Component\HttpFoundation\Request;
interface FormatNegotiatorInterface
{
function getBestFormat(Request $request, array $availableTypes);
}

View File

@@ -16,8 +16,7 @@
}
],
"require": {
"php": ">=5.3.2",
"symfony/http-foundation": "~2.0"
"php": ">=5.3.3"
},
"autoload": {
"psr-0": { "FOS\\Rest": "" }