10 Commits

Author SHA1 Message Date
Lukas Kahwe Smith
02efd1e9f2 note about the code being deprecated 2013-10-12 22:34:05 +02:00
Lukas Kahwe Smith
ec9b1d9a88 preparing a stable release 2013-10-10 20:08:23 +02:00
Lukas Kahwe Smith
b773e2e270 added note about https://github.com/symfony/symfony/pull/8820 2013-08-23 17:34:55 +02:00
Lukas Kahwe Smith
9375f6cc8d go to 0.9.x dev 2013-07-28 17:11:31 +02:00
Lukas Kahwe Smith
e571ccbfc2 Merge pull request #17 from FriendsOfSymfony/remove_content_negotiation
Remove content negotiation
2013-07-28 01:10:50 -07:00
Lukas Kahwe Smith
1d610cc01b removed content negotiation in favor of willdurand/negotiation 2013-07-26 16:30:31 +02:00
Lukas Kahwe Smith
1d0a0d5b3c updated test setup 2013-07-26 16:00:12 +02:00
Lukas Kahwe Smith
b836af42da Merge pull request #15 from teohhanhui/patch-1
Do not return highest priority format immediately when catch-all is dete...
2013-07-24 11:23:56 -07:00
Teoh Han Hui
cf699ffe65 Update FormatNegotiator.php
Fixed undefined variable issue.
2013-07-16 19:09:48 +08:00
Teoh Han Hui
89306dd972 Do not return highest priority format immediately when catch-all is detected
Related to issue 372

From the HTTP/1.1 spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):

Media ranges can be overridden by more specific media ranges or specific media types. If more than one media range applies to a given type, the most specific reference has precedence. For example,

       Accept: text/*, text/html, text/html;level=1, */*
have the following precedence:

       1) text/html;level=1
       2) text/html
       3) text/*
       4) */*
2013-07-09 18:14:32 +08:00
8 changed files with 13 additions and 171 deletions

4
.gitignore vendored
View File

@@ -1,3 +1,5 @@
/phpunit.xml
composer.phar
composer.phar
/composer.lock
/vendor/

View File

@@ -3,10 +3,13 @@ language: php
php:
- 5.3
- 5.4
- 5.5
env:
- SYMFONY_VERSION=2.0.*
- SYMFONY_VERSION=2.1.*
- SYMFONY_VERSION=2.2.*
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=dev-master
before_script:

View File

@@ -8,3 +8,5 @@ The main goals for now are to provide ways for decoding request bodies
and request format negotiation (inspired by Apache mod_negotiation)
[![Build Status](https://secure.travis-ci.org/FriendsOfSymfony/FOSRest.png)](http://travis-ci.org/FriendsOfSymfony/FOSRest)
Deprecated in favor of https://github.com/willdurand/Negotiation and https://github.com/FriendsOfSymfony/FOSRestBundle

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

@@ -19,6 +19,9 @@ namespace FOS\Rest\Util;
* (last updated 2012-02-13).
*
* Unless otherwise noted, the status code is defined in RFC2616.
*
* Note: Symfony 2.4 will have the same constants in the Response class:
* https://github.com/symfony/symfony/pull/8820
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Markus Lanthaler <markus.lanthaler@gmx.net>

View File

@@ -1,99 +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();
foreach ($keys as $mimetype) {
unset($mimetypes[$mimetype]);
if ($mimetype === '*/*') {
return reset($priorities);
}
$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) && !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": "" }
@@ -25,7 +24,7 @@
"target-dir": "FOS/Rest",
"extra": {
"branch-alias": {
"dev-master": "0.8.x-dev"
"dev-master": "1.0-dev"
}
}
}