3 namespace Drupal\Core\Routing;
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
7 use Symfony\Component\Routing\RouteCollection;
10 * Provides a route filter, which filters by the request format.
12 class RequestFormatRouteFilter implements FilterInterface {
17 public function filter(RouteCollection $collection, Request $request) {
18 // Determine the request format.
19 $default_format = static::getDefaultFormat($collection);
20 $format = $request->getRequestFormat($default_format);
22 $routes_with_requirement = [];
23 $routes_without_requirement = [];
24 $result_collection = new RouteCollection();
25 /** @var \Symfony\Component\Routing\Route $route */
26 foreach ($collection as $name => $route) {
27 if (!$route->hasRequirement('_format')) {
28 $routes_without_requirement[$name] = $route;
32 $routes_with_requirement[$name] = $route;
36 foreach ($routes_with_requirement as $name => $route) {
37 // If the route has no _format specification, we move it to the end. If it
38 // does, then no match means the route is removed entirely.
39 if (($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) && in_array($format, $supported_formats, TRUE)) {
40 $result_collection->add($name, $route);
44 foreach ($routes_without_requirement as $name => $route) {
45 $result_collection->add($name, $route);
48 if (count($result_collection)) {
49 return $result_collection;
53 // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
54 // because we don't want to return a 404 status code, but rather a 406.
55 throw new NotAcceptableHttpException("No route found for the specified format $format.");
59 * Determines the default request format.
61 * By default, use 'html' as the default format. But when there's only a
62 * single route match, and that route specifies a '_format' requirement
63 * listing a single format, then use that as the default format.
65 * @param \Symfony\Component\Routing\RouteCollection $collection
66 * The route collection to filter.
71 protected static function getDefaultFormat(RouteCollection $collection) {
72 $default_format = 'html';
73 if ($collection->count() === 1) {
74 $only_route = $collection->getIterator()->current();
75 $required_format = $only_route->getRequirement('_format');
76 if (strpos($required_format, '|') === FALSE) {
77 $default_format = $required_format;
80 return $default_format;