MethodFilter.php in Drupal 9
File
core/lib/Drupal/Core/Routing/MethodFilter.php
View source
<?php
namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\RouteCollection;
class MethodFilter implements FilterInterface {
public function filter(RouteCollection $collection, Request $request) {
$method = $request
->getMethod();
$all_supported_methods = [];
foreach ($collection
->all() as $name => $route) {
$supported_methods = $route
->getMethods();
if (empty($supported_methods)) {
continue;
}
if (in_array('GET', $supported_methods, TRUE)) {
$supported_methods[] = 'HEAD';
}
if (!in_array($method, $supported_methods, TRUE)) {
$all_supported_methods = array_merge($supported_methods, $all_supported_methods);
$collection
->remove($name);
}
}
if (count($collection)) {
return $collection;
}
throw new MethodNotAllowedException(array_unique($all_supported_methods));
}
}