Change content type negotiation in API.

Fix typo in negotiator word.
This commit is contained in:
Benoît Burnichon
2015-03-27 13:42:31 +01:00
parent bf27dcff20
commit ad51a350b7
4 changed files with 15 additions and 12 deletions

View File

@@ -51,11 +51,14 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
$request->setFormat(Result::FORMAT_JSONP_EXTENDED, V1::$extendedContentTypes['jsonp']); $request->setFormat(Result::FORMAT_JSONP_EXTENDED, V1::$extendedContentTypes['jsonp']);
$request->setFormat(Result::FORMAT_JSONP, array('text/javascript', 'application/javascript')); $request->setFormat(Result::FORMAT_JSONP, array('text/javascript', 'application/javascript'));
// handle content negociation $format = $app['format.negotiator']->getBest(
$priorities = array('application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'); $request->headers->get('accept', 'application/json'),
foreach (V1::$extendedContentTypes['json'] as $priorities[]); array_merge(
foreach (V1::$extendedContentTypes['yaml'] as $priorities[]); ['application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'],
$format = $app['format.negociator']->getBest($request->headers->get('accept', 'application/json') ,$priorities); V1::$extendedContentTypes['json'],
V1::$extendedContentTypes['yaml']
)
);
// throw unacceptable http error if API can not handle asked format // throw unacceptable http error if API can not handle asked format
if (null === $format) { if (null === $format) {

View File

@@ -39,7 +39,7 @@ class ContentNegotiationSubscriber implements EventSubscriberInterface
public function onKernelRequest(GetResponseEvent $event) public function onKernelRequest(GetResponseEvent $event)
{ {
$priorities = array('text/html', 'application/json', '*/*'); $priorities = array('text/html', 'application/json', '*/*');
$format = $this->app['format.negociator']->getBest($event->getRequest()->headers->get('accept', '*/*'), $priorities); $format = $this->app['format.negotiator']->getBest($event->getRequest()->headers->get('accept', '*/*'), $priorities);
if (null === $format) { if (null === $format) {
$this->app->abort(406, 'Not acceptable'); $this->app->abort(406, 'Not acceptable');

View File

@@ -21,15 +21,15 @@ class ContentNegotiationServiceProvider implements ServiceProviderInterface
{ {
public function register(Application $app) public function register(Application $app)
{ {
$app['negociator'] = $app->share(function ($app) { $app['negotiator'] = $app->share(function ($app) {
return new Negotiator(); return new Negotiator();
}); });
$app['format.negociator'] = $app->share(function ($app) { $app['format.negotiator'] = $app->share(function ($app) {
return new FormatNegotiator(); return new FormatNegotiator();
}); });
$app['langage.negociator'] = $app->share(function ($app) { $app['langage.negotiator'] = $app->share(function ($app) {
return new LanguageNegotiator(); return new LanguageNegotiator();
}); });
} }

View File

@@ -12,17 +12,17 @@ class classContentNegotiationServiceProviderTest extends ServiceProviderTestCase
return array( return array(
array( array(
'Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider', 'Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider',
'negociator', 'negotiator',
'Negotiation\Negotiator', 'Negotiation\Negotiator',
), ),
array( array(
'Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider', 'Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider',
'format.negociator', 'format.negotiator',
'Negotiation\FormatNegotiator' 'Negotiation\FormatNegotiator'
), ),
array( array(
'Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider', 'Alchemy\Phrasea\Core\Provider\ContentNegotiationServiceProvider',
'langage.negociator', 'langage.negotiator',
'Negotiation\LanguageNegotiator' 'Negotiation\LanguageNegotiator'
) )
); );