dbfb801b7856de897ef08c887f79690da9612017
[yaffs-website] / web / core / lib / Drupal / Core / Routing / RequestFormatRouteFilter.php
1 <?php
2
3 namespace Drupal\Core\Routing;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
7 use Symfony\Component\Routing\RouteCollection;
8
9 /**
10  * Provides a route filter, which filters by the request format.
11  */
12 class RequestFormatRouteFilter implements FilterInterface {
13
14   /**
15    * {@inheritdoc}
16    */
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);
21
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;
29         continue;
30       }
31       else {
32         $routes_with_requirement[$name] = $route;
33       }
34     }
35
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);
41       }
42     }
43
44     foreach ($routes_without_requirement as $name => $route) {
45       $result_collection->add($name, $route);
46     }
47
48     if (count($result_collection)) {
49       return $result_collection;
50     }
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     throw new NotAcceptableHttpException("No route found for the specified format $format.");
56   }
57
58   /**
59    * Determines the default request format.
60    *
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.
64    *
65    * @param \Symfony\Component\Routing\RouteCollection $collection
66    *   The route collection to filter.
67    *
68    * @return string
69    *   The default format.
70    */
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;
78       }
79     }
80     return $default_format;
81   }
82
83 }