You are here

protected function RouteProvider::getRoutesByPath in Drupal 9

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

Get all routes which match a certain pattern.

Parameters

string $path: The route pattern to search for.

Return value

\Symfony\Component\Routing\RouteCollection Returns a route collection of matching routes. The collection may be empty and will be sorted from highest to lowest fit (match of path parts) and then in ascending order by route name for routes with the same fit.

2 calls to RouteProvider::getRoutesByPath()
RouteProvider::getRouteCollectionForRequest in core/lib/Drupal/Core/Routing/RouteProvider.php
Finds routes that may potentially match the request.
RouteProvider::getRoutesByPattern in core/lib/Drupal/Core/Routing/RouteProvider.php
Get all routes which match a certain pattern.

File

core/lib/Drupal/Core/Routing/RouteProvider.php, line 346

Class

RouteProvider
A Route Provider front-end for all Drupal-stored routes.

Namespace

Drupal\Core\Routing

Code

protected function getRoutesByPath($path) {

  // Split the path up on the slashes, ignoring multiple slashes in a row
  // or leading or trailing slashes. Convert to lower case here so we can
  // have a case-insensitive match from the incoming path to the lower case
  // pattern outlines from \Drupal\Core\Routing\RouteCompiler::compile().
  // @see \Drupal\Core\Routing\CompiledRoute::__construct()
  $parts = preg_split('@/+@', mb_strtolower($path), NULL, PREG_SPLIT_NO_EMPTY);
  $collection = new RouteCollection();
  $ancestors = $this
    ->getCandidateOutlines($parts);
  if (empty($ancestors)) {
    return $collection;
  }

  // The >= check on number_parts allows us to match routes with optional
  // trailing wildcard parts as long as the pattern matches, since we
  // dump the route pattern without those optional parts.
  try {
    $routes = $this->connection
      ->query("SELECT [name], [route], [fit] FROM {" . $this->connection
      ->escapeTable($this->tableName) . "} WHERE [pattern_outline] IN ( :patterns[] ) AND [number_parts] >= :count_parts", [
      ':patterns[]' => $ancestors,
      ':count_parts' => count($parts),
    ])
      ->fetchAll(\PDO::FETCH_ASSOC);
  } catch (\Exception $e) {
    $routes = [];
  }

  // We sort by fit and name in PHP to avoid a SQL filesort and avoid any
  // difference in the sorting behavior of SQL back-ends.
  usort($routes, [
    $this,
    'routeProviderRouteCompare',
  ]);
  foreach ($routes as $row) {
    $collection
      ->add($row['name'], unserialize($row['route']));
  }
  return $collection;
}