annotate core/modules/jsonapi/src/Routing/EarlyFormatSetter.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@18 1 <?php
Chris@18 2
Chris@18 3 namespace Drupal\jsonapi\Routing;
Chris@18 4
Chris@18 5 use Drupal\Core\Routing\RequestFormatRouteFilter;
Chris@18 6 use Symfony\Component\HttpFoundation\Request;
Chris@18 7 use Symfony\Component\Routing\RouteCollection;
Chris@18 8
Chris@18 9 /**
Chris@18 10 * Sets the 'api_json' format for requests to JSON:API resources.
Chris@18 11 *
Chris@18 12 * Because this module places all JSON:API resources at paths prefixed with
Chris@18 13 * /jsonapi, and therefore not shared with other formats,
Chris@18 14 * \Drupal\Core\Routing\RequestFormatRouteFilter does correctly set the request
Chris@18 15 * format for those requests. However, it does so after other filters, such as
Chris@18 16 * \Drupal\Core\Routing\ContentTypeHeaderMatcher, run. If those other filters
Chris@18 17 * throw exceptions, we'd like the error response to be in JSON:API format as
Chris@18 18 * well, so we set that format here, in a higher priority (earlier running)
Chris@18 19 * filter. This works so long as the resource format can be determined before
Chris@18 20 * running any other filters, which is the case for JSON:API resources per
Chris@18 21 * above.
Chris@18 22 *
Chris@18 23 * @internal
Chris@18 24 */
Chris@18 25 final class EarlyFormatSetter extends RequestFormatRouteFilter {
Chris@18 26
Chris@18 27 /**
Chris@18 28 * {@inheritdoc}
Chris@18 29 */
Chris@18 30 public function filter(RouteCollection $collection, Request $request) {
Chris@18 31 if (is_null($request->getRequestFormat(NULL))) {
Chris@18 32 $possible_formats = static::getAvailableFormats($collection);
Chris@18 33 if ($possible_formats === ['api_json']) {
Chris@18 34 $request->setRequestFormat('api_json');
Chris@18 35 }
Chris@18 36 }
Chris@18 37 return $collection;
Chris@18 38 }
Chris@18 39
Chris@18 40 }