Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
- 9 core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
Namespace
Drupal\Core\Routing
File
core/lib/Drupal/Core/Routing/ContentTypeHeaderMatcher.php
View source
<?php
namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
use Symfony\Component\Routing\RouteCollection;
class ContentTypeHeaderMatcher implements FilterInterface {
public function filter(RouteCollection $collection, Request $request) {
if ($request
->isMethodSafe() || $request
->isMethod('DELETE')) {
return $collection;
}
$format = $request
->getContentTypeFormat();
foreach ($collection as $name => $route) {
$supported_formats = array_filter(explode('|', $route
->getRequirement('_content_type_format') ?? ''));
if (empty($supported_formats)) {
$collection
->add($name, $route);
}
elseif (!in_array($format, $supported_formats)) {
$collection
->remove($name);
}
}
if (count($collection)) {
return $collection;
}
if (!$request->headers
->get('Content-Type', FALSE)) {
throw new UnsupportedMediaTypeHttpException('No "Content-Type" request header specified');
}
else {
throw new UnsupportedMediaTypeHttpException('No route found that matches "Content-Type: ' . $request->headers
->get('Content-Type') . '"');
}
}
}
Classes