Change in ContentNegotiationSubscriber

This commit is contained in:
Benoît Burnichon
2015-10-29 18:06:33 +01:00
parent 574377dea6
commit afd3fc56f7
4 changed files with 29 additions and 25 deletions

View File

@@ -52,6 +52,12 @@ return call_user_func(function ($environment = PhraseaApplication::ENV_PROD) {
return $monolog; return $monolog;
})); }));
$app['phraseanet.content-negotiation.priorities'] = array_merge(
['application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'],
V1::$extendedContentTypes['json'],
V1::$extendedContentTypes['yaml']
);
// handle API content negotiation // handle API content negotiation
$app->before(function(Request $request) use ($app) { $app->before(function(Request $request) use ($app) {
// register custom API format // register custom API format
@@ -60,24 +66,9 @@ 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'));
$format = $app['negotiator']->getBest(
$request->headers->get('accept', 'application/json'),
array_merge(
['application/json', 'application/yaml', 'text/yaml', 'text/javascript', 'application/javascript'],
V1::$extendedContentTypes['json'],
V1::$extendedContentTypes['yaml']
)
);
// throw unacceptable http error if API can not handle asked format
if (null === $format) {
$app->abort(406);
}
// set request format according to negotiated content or override format with JSONP if callback parameter is defined // set request format according to negotiated content or override format with JSONP if callback parameter is defined
if (trim($request->get('callback')) !== '') { if (trim($request->get('callback')) !== '') {
$request->setRequestFormat(Result::FORMAT_JSONP); $request->setRequestFormat(Result::FORMAT_JSONP);
} else {
$request->setRequestFormat($request->getFormat($format->getValue()));
} }
// tells whether asked format is extended or not // tells whether asked format is extended or not

View File

@@ -11,18 +11,25 @@
namespace Alchemy\Phrasea\Core\Event\Subscriber; namespace Alchemy\Phrasea\Core\Event\Subscriber;
use Alchemy\Phrasea\Application; use Negotiation\Accept;
use Negotiation\Negotiator;
use Silex\Application;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class ContentNegotiationSubscriber implements EventSubscriberInterface class ContentNegotiationSubscriber implements EventSubscriberInterface
{ {
private $app; /** @var Negotiator */
private $negotiator;
/** @var array */
private $priorities;
public function __construct(Application $app) public function __construct(Negotiator $negotiator, array $priorities)
{ {
$this->app = $app; $this->negotiator = $negotiator;
$this->priorities = $priorities;
} }
public static function getSubscribedEvents() public static function getSubscribedEvents()
@@ -34,11 +41,10 @@ class ContentNegotiationSubscriber implements EventSubscriberInterface
public function onKernelRequest(GetResponseEvent $event) public function onKernelRequest(GetResponseEvent $event)
{ {
$priorities = array('text/html', 'application/vnd.phraseanet.record-extended+json', 'application/json'); $format = $this->negotiator->getBest($event->getRequest()->headers->get('accept', '*/*'), $this->priorities);
$format = $this->app['negotiator']->getBest($event->getRequest()->headers->get('accept', '*/*'), $priorities);
if (null === $format) { if (!$format instanceof Accept) {
$this->app->abort(406, 'Not acceptable'); throw new HttpException(406);
} }
$event->getRequest()->setRequestFormat($event->getRequest()->getFormat($format->getValue())); $event->getRequest()->setRequestFormat($event->getRequest()->getFormat($format->getValue()));

View File

@@ -40,8 +40,15 @@ class PhraseaEventServiceProvider implements ServiceProviderInterface
$app['phraseanet.session-manager-subscriber'] = $app->share(function (Application $app) { $app['phraseanet.session-manager-subscriber'] = $app->share(function (Application $app) {
return new SessionManagerSubscriber($app); return new SessionManagerSubscriber($app);
}); });
$app['phraseanet.content-negotiation.priorities'] = [
'text/html',
'application/json',
];
$app['phraseanet.content-negotiation-subscriber'] = $app->share(function (Application $app) { $app['phraseanet.content-negotiation-subscriber'] = $app->share(function (Application $app) {
return new ContentNegotiationSubscriber($app); return new ContentNegotiationSubscriber(
$app['negotiator'],
$app['phraseanet.content-negotiation.priorities']
);
}); });
$app['phraseanet.record-edit-subscriber'] = $app->share(function (Application $app) { $app['phraseanet.record-edit-subscriber'] = $app->share(function (Application $app) {
return new RecordEditSubscriber(); return new RecordEditSubscriber();

View File

@@ -31,7 +31,7 @@ class ContentNegotiationSubscriberTest extends \PHPUnit_Framework_TestCase
private function request($accept) private function request($accept)
{ {
$app = new Application(Application::ENV_TEST); $app = new Application(Application::ENV_TEST);
$app['dispatcher']->addSubscriber(new ContentNegotiationSubscriber($app)); $app['dispatcher']->addSubscriber(new ContentNegotiationSubscriber($app['negotiator'], $app['phraseanet.content-negotiation.priorities']));
$app->get('/content/negociation', function () { $app->get('/content/negociation', function () {
return ''; return '';
}); });