RouteProvider::getCandidateOutlines

protected RouteProvider::getCandidateOutlines(array $parts)

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.

File

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

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 = array();
  $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 = array(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 = array();
  }
  else {
    // Get the actual patterns that exist out of state.
    $masks = (array) $this->state->get('routing.menu_masks.' . $this->tableName, array());
  }

  // 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;
}
doc_Drupal
2016-10-29 09:39:17
Comments
Leave a Comment

Please login to continue.