RequestFormatRouteFilter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Drupal\Core\Url;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  6. use Symfony\Component\Routing\Route;
  7. use Symfony\Component\Routing\RouteCollection;
  8. /**
  9. * Provides a route filter, which filters by the request format.
  10. */
  11. class RequestFormatRouteFilter implements FilterInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function filter(RouteCollection $collection, Request $request) {
  16. // Determine the request format.
  17. $default_format = static::getDefaultFormat($collection);
  18. // If the request does not specify a format then use the default.
  19. if (is_null($request->getRequestFormat(NULL))) {
  20. $format = $default_format;
  21. $request->setRequestFormat($default_format);
  22. }
  23. else {
  24. $format = $request->getRequestFormat($default_format);
  25. }
  26. $routes_with_requirement = [];
  27. $routes_without_requirement = [];
  28. $result_collection = new RouteCollection();
  29. /** @var \Symfony\Component\Routing\Route $route */
  30. foreach ($collection as $name => $route) {
  31. if (!$route->hasRequirement('_format')) {
  32. $routes_without_requirement[$name] = $route;
  33. continue;
  34. }
  35. else {
  36. $routes_with_requirement[$name] = $route;
  37. }
  38. }
  39. foreach ($routes_with_requirement as $name => $route) {
  40. // If the route has no _format specification, we move it to the end. If it
  41. // does, then no match means the route is removed entirely.
  42. if (($supported_formats = array_filter(explode('|', $route->getRequirement('_format')))) && in_array($format, $supported_formats, TRUE)) {
  43. $result_collection->add($name, $route);
  44. }
  45. }
  46. foreach ($routes_without_requirement as $name => $route) {
  47. $result_collection->add($name, $route);
  48. }
  49. if (count($result_collection)) {
  50. return $result_collection;
  51. }
  52. // We do not throw a
  53. // \Symfony\Component\Routing\Exception\ResourceNotFoundException here
  54. // because we don't want to return a 404 status code, but rather a 406.
  55. $available_formats = static::getAvailableFormats($collection);
  56. $not_acceptable = new NotAcceptableHttpException("No route found for the specified format $format. Supported formats: " . implode(', ', $available_formats) . '.');
  57. if ($available_formats) {
  58. $links = [];
  59. foreach ($available_formats as $available_format) {
  60. $url = Url::fromUri($request->getUri(), ['query' => ['_format' => $available_format]])->toString(TRUE)->getGeneratedUrl();
  61. $content_type = $request->getMimeType($available_format);
  62. $links[] = "<$url>; rel=\"alternate\"; type=\"$content_type\"";
  63. }
  64. $not_acceptable->setHeaders(['Link' => implode(', ', $links)]);
  65. }
  66. throw $not_acceptable;
  67. }
  68. /**
  69. * Determines the default request format.
  70. *
  71. * By default, use 'html' as the default format. But when there's only a
  72. * single route match, and that route specifies a '_format' requirement
  73. * listing a single format, then use that as the default format. Also, if
  74. * there are multiple routes which all require the same single format then
  75. * use it.
  76. *
  77. * @param \Symfony\Component\Routing\RouteCollection $collection
  78. * The route collection to filter.
  79. *
  80. * @return string
  81. * The default format.
  82. */
  83. protected static function getDefaultFormat(RouteCollection $collection) {
  84. $formats = static::getAvailableFormats($collection);
  85. // The default format is 'html' unless ALL routes require the same format.
  86. return count($formats) === 1
  87. ? reset($formats)
  88. : 'html';
  89. }
  90. /**
  91. * Gets the set of formats across all routes in the collection.
  92. *
  93. * @param \Symfony\Component\Routing\RouteCollection $collection
  94. * The route collection to filter.
  95. *
  96. * @return string[]
  97. * All available formats.
  98. */
  99. protected static function getAvailableFormats(RouteCollection $collection) {
  100. $all_formats = array_reduce($collection->all(), function (array $carry, Route $route) {
  101. // Routes without a '_format' requirement are assumed to require HTML.
  102. $route_formats = !$route->hasRequirement('_format')
  103. ? ['html']
  104. : explode('|', $route->getRequirement('_format'));
  105. return array_merge($carry, $route_formats);
  106. }, []);
  107. return array_unique(array_filter($all_formats));
  108. }
  109. }