mirror of
https://github.com/FriendsOfSymfony/FOSRest.git
synced 2026-03-24 08:42:15 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02efd1e9f2 | ||
|
|
ec9b1d9a88 | ||
|
|
b773e2e270 | ||
|
|
9375f6cc8d | ||
|
|
e571ccbfc2 | ||
|
|
1d610cc01b | ||
|
|
1d0a0d5b3c | ||
|
|
b836af42da | ||
|
|
cf699ffe65 | ||
|
|
89306dd972 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
/phpunit.xml
|
||||
|
||||
composer.phar
|
||||
composer.phar
|
||||
/composer.lock
|
||||
/vendor/
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
[](http://travis-ci.org/FriendsOfSymfony/FOSRest)
|
||||
|
||||
Deprecated in favor of https://github.com/willdurand/Negotiation and https://github.com/FriendsOfSymfony/FOSRestBundle
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
}}
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user