commit ea8b201539d1798652a83bbeb2d0d1eceee8a0d1 Author: Erik Stielstra Date: Mon Oct 10 09:28:31 2016 +0200 Fix comments 2810293-8 diff --git a/jsonapi.services.yml b/jsonapi.services.yml index 9747fa2..872d173 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -90,6 +90,6 @@ services: parent: default_plugin_manager jsonapi.exception_subscriber: class: Drupal\jsonapi\EventSubscriber\ExceptionSubscriber - arguments: ['@request_stack', '@serializer'] + arguments: ['@request_stack', '@current_route_match', '@serializer'] tags: - { name: event_subscriber } diff --git a/src/EventSubscriber/ExceptionSubscriber.php b/src/EventSubscriber/ExceptionSubscriber.php index 98a2929..7e274b8 100644 --- a/src/EventSubscriber/ExceptionSubscriber.php +++ b/src/EventSubscriber/ExceptionSubscriber.php @@ -2,6 +2,7 @@ namespace Drupal\jsonapi\EventSubscriber; +use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; @@ -29,6 +30,13 @@ class ExceptionSubscriber implements EventSubscriberInterface { protected $currentRequest; /** + * The currently active route match object. + * + * @var \Drupal\Core\Routing\RouteMatchInterface + */ + protected $routeMatch; + + /** * The serializer. * * @var \Symfony\Component\Serializer\Serializer @@ -39,9 +47,12 @@ class ExceptionSubscriber implements EventSubscriberInterface { * Constructs a new redirect subscriber. * * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * @param \Symfony\Component\Serializer\SerializerInterface $serializer */ - public function __construct(RequestStack $request_stack, SerializerInterface $serializer) { + public function __construct(RequestStack $request_stack, RouteMatchInterface $route_match, SerializerInterface $serializer) { $this->currentRequest = $request_stack->getCurrentRequest(); + $this->routeMatch = $route_match; $this->serializer = $serializer; } @@ -74,7 +85,7 @@ class ExceptionSubscriber implements EventSubscriberInterface { $content = $this->serializer->serialize($exception, $format); // Add the default content type, but only if the headers from the // exception have not specified it already. - $headers = $exception->getHeaders() + array('Content-Type' => $this->currentRequest->getMimeType($format)); + $headers = $exception->getHeaders() + ['Content-Type' => $this->currentRequest->getMimeType($format)]; $response = new Response($content, $status, $headers); $event->setResponse($response); } @@ -88,15 +99,18 @@ class ExceptionSubscriber implements EventSubscriberInterface { } /** - * Check if the the current route is a JSON API route. + * Check if the the current request is a JSON API request. * * @return bool - * Returns true of the route applies. + * Returns true if the exception applies to a JSON API route or request. */ protected function applies() { - // @todo Is this cheching for '_format' reliable? - // @todo Is this (kind of) check used in other places too? - return $this->currentRequest->query->get('_format') == 'api_json'; + // We check for both the requested format and whether the route is a jsonapi + // route. Checking the route only fails in case the exception occurs before + // the route enhancer added the '_is_jsonapi' option. + $is_json_format = $this->currentRequest->query->get('_format') == 'api_json'; + $is_jsonapi = $this->routeMatch->getRouteObject()->getOption('_is_jsonapi'); + return $is_json_format || $is_jsonapi; } }