You are here

protected function Router::doMatchCollection in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Routing/Router.php \Drupal\Core\Routing\Router::doMatchCollection()

Tries to match a URL with a set of routes.

This code is very similar to Symfony's UrlMatcher::matchCollection() but it supports case-insensitive matching. The static prefix optimization is removed as this duplicates work done by the query in RouteProvider::getRoutesByPath().

Parameters

string $pathinfo: The path info to be parsed

\Symfony\Component\Routing\RouteCollection $routes: The set of routes.

bool $case_sensitive: Determines if the match should be case-sensitive of not.

Return value

array|null An array of parameters. NULL when there is no match.

See also

\Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection()

\Drupal\Core\Routing\RouteProvider::getRoutesByPath()

1 call to Router::doMatchCollection()
Router::matchCollection in core/lib/Drupal/Core/Routing/Router.php
Tries to match a URL with a set of routes.

File

core/lib/Drupal/Core/Routing/Router.php, line 180

Class

Router
Router implementation in Drupal.

Namespace

Drupal\Core\Routing

Code

protected function doMatchCollection($pathinfo, RouteCollection $routes, $case_sensitive) {
  foreach ($routes as $name => $route) {
    $compiledRoute = $route
      ->compile();

    // Set the regex to use UTF-8.
    $regex = $compiledRoute
      ->getRegex() . 'u';
    if (!$case_sensitive) {
      $regex = $regex . 'i';
    }
    if (!preg_match($regex, $pathinfo, $matches)) {
      continue;
    }
    $hostMatches = [];
    if ($compiledRoute
      ->getHostRegex() && !preg_match($compiledRoute
      ->getHostRegex(), $this->context
      ->getHost(), $hostMatches)) {
      $routes
        ->remove($name);
      continue;
    }

    // Check HTTP method requirement.
    if ($requiredMethods = $route
      ->getMethods()) {

      // HEAD and GET are equivalent as per RFC.
      if ('HEAD' === ($method = $this->context
        ->getMethod())) {
        $method = 'GET';
      }
      if (!in_array($method, $requiredMethods)) {
        $this->allow = array_merge($this->allow, $requiredMethods);
        $routes
          ->remove($name);
        continue;
      }
    }
    $status = $this
      ->handleRouteRequirements($pathinfo, $name, $route);
    if (self::ROUTE_MATCH === $status[0]) {
      return $status[1];
    }
    if (self::REQUIREMENT_MISMATCH === $status[0]) {
      $routes
        ->remove($name);
      continue;
    }
    return $this
      ->getAttributes($route, $name, array_replace($matches, $hostMatches));
  }
}