RequestFormatRouteFilter.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  5. use Symfony\Component\Routing\RouteCollection;
  6. /**
  7. * Provides a route filter, which filters by the request format.
  8. */
  9. class RequestFormatRouteFilter implements FilterInterface {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public function filter(RouteCollection $collection, Request $request) {
  14. // Determine the request format.
  15. $default_format = static::getDefaultFormat($collection);
  16. $format = $request->getRequestFormat($default_format);
  17. $routes_with_requirement = [];
  18. $routes_without_requirement = [];
  19. $result_collection = new RouteCollection();
  20. /** @var \Symfony\Component\Routing\Route $route */
  21. foreach ($collection as $name => $route) {
  22. if (!$route->hasRequirement('_format')) {
  23. $routes_without_requirement[$name] = $route;
  24. continue;
  25. }
  26. else {
  27. $routes_with_requirement[$name] = $route;
  28. }
  29. }
  30. foreach ($routes_with_requirement as $name => $route) {
  31. // If the route has no _format specification, we move it to the end. If it
  32. // does, then no match means the route is removed entirely.
  33. if (($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) && in_array($format, $supported_formats, TRUE)) {
  34. $result_collection->add($name, $route);
  35. }
  36. }
  37. foreach ($routes_without_requirement as $name => $route) {
  38. $result_collection->add($name, $route);
  39. }
  40. if (count($result_collection)) {
  41. return $result_collection;
  42. }
  43. // We do not throw a
  44. // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
  45. // because we don't want to return a 404 status code, but rather a 406.
  46. throw new NotAcceptableHttpException("No route found for the specified format $format.");
  47. }
  48. /**
  49. * Determines the default request format.
  50. *
  51. * By default, use 'html' as the default format. But when there's only a
  52. * single route match, and that route specifies a '_format' requirement
  53. * listing a single format, then use that as the default format.
  54. *
  55. * @param \Symfony\Component\Routing\RouteCollection $collection
  56. * The route collection to filter.
  57. *
  58. * @return string
  59. * The default format.
  60. */
  61. protected static function getDefaultFormat(RouteCollection $collection) {
  62. $default_format = 'html';
  63. if ($collection->count() === 1) {
  64. $only_route = $collection->getIterator()->current();
  65. $required_format = $only_route->getRequirement('_format');
  66. if (strpos($required_format, '|') === FALSE) {
  67. $default_format = $required_format;
  68. }
  69. }
  70. return $default_format;
  71. }
  72. }