You are here

protected function RouteProvider::getCandidateOutlines in Drupal 9

Same name in this branch
  1. 9 core/tests/Drupal/KernelTests/RouteProvider.php \Drupal\KernelTests\RouteProvider::getCandidateOutlines()
  2. 9 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getCandidateOutlines()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Routing/RouteProvider.php \Drupal\Core\Routing\RouteProvider::getCandidateOutlines()

Returns an array of path pattern outlines that could match the path parts.

Parameters

array $parts: The parts of the path for which we want candidates.

Return value

array An array of outlines that could match the specified path parts.

2 calls to RouteProvider::getCandidateOutlines()
RouteProvider::getRoutesByPath in core/lib/Drupal/Core/Routing/RouteProvider.php
Get all routes which match a certain pattern.
TestRouteProvider::getCandidateOutlines in core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php
Returns an array of path pattern outlines that could match the path parts.
1 method overrides RouteProvider::getCandidateOutlines()
TestRouteProvider::getCandidateOutlines in core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php
Returns an array of path pattern outlines that could match the path parts.

File

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

Class

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

Namespace

Drupal\Core\Routing

Code

protected function getCandidateOutlines(array $parts) {
  $number_parts = count($parts);
  $ancestors = [];
  $length = $number_parts - 1;
  $end = (1 << $number_parts) - 1;

  // The highest possible mask is a 1 bit for every part of the path. We will
  // check every value down from there to generate a possible outline.
  if ($number_parts == 1) {
    $masks = [
      1,
    ];
  }
  elseif ($number_parts <= 3 && $number_parts > 0) {

    // Optimization - don't query the state system for short paths. This also
    // insulates against the state entry for masks going missing for common
    // user-facing paths since we generate all values without checking state.
    $masks = range($end, 1);
  }
  elseif ($number_parts <= 0) {

    // No path can match, short-circuit the process.
    $masks = [];
  }
  else {

    // Get the actual patterns that exist out of state.
    $masks = (array) $this->state
      ->get('routing.menu_masks.' . $this->tableName, []);
  }

  // Only examine patterns that actually exist as router items (the masks).
  foreach ($masks as $i) {
    if ($i > $end) {

      // Only look at masks that are not longer than the path of interest.
      continue;
    }
    elseif ($i < 1 << $length) {

      // We have exhausted the masks of a given length, so decrease the length.
      --$length;
    }
    $current = '';
    for ($j = $length; $j >= 0; $j--) {

      // Check the bit on the $j offset.
      if ($i & 1 << $j) {

        // Bit one means the original value.
        $current .= $parts[$length - $j];
      }
      else {

        // Bit zero means means wildcard.
        $current .= '%';
      }

      // Unless we are at offset 0, add a slash.
      if ($j) {
        $current .= '/';
      }
    }
    $ancestors[] = '/' . $current;
  }
  return $ancestors;
}