AcceptHeaderMatcher.php in Zircon Profile 8.0
File
core/modules/system/tests/modules/accept_header_routing_test/src/Routing/AcceptHeaderMatcher.php
View source
<?php
namespace Drupal\accept_header_routing_test\Routing;
use Drupal\Core\Routing\RouteFilterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class AcceptHeaderMatcher implements RouteFilterInterface {
public function filter(RouteCollection $collection, Request $request) {
$acceptable_mime_types = $request
->getAcceptableContentTypes();
$acceptable_formats = array_filter(array_map(array(
$request,
'getFormat',
), $acceptable_mime_types));
$primary_format = $request
->getRequestFormat();
foreach ($collection as $name => $route) {
$supported_formats = array_filter(explode('|', $route
->getRequirement('_format')));
if (empty($supported_formats)) {
$collection
->add($name, $route);
}
elseif (in_array($primary_format, $supported_formats)) {
}
elseif (in_array('*/*', $acceptable_mime_types) || array_intersect($acceptable_formats, $supported_formats)) {
$collection
->add($name, $route);
}
else {
$collection
->remove($name);
}
}
if (count($collection)) {
return $collection;
}
throw new NotAcceptableHttpException('No route found for the specified formats ' . implode(' ', $acceptable_mime_types));
}
public function applies(Route $route) {
return TRUE;
}
}