You are here

public function PathMatcher::matchPath in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Path/PathMatcher.php \Drupal\Core\Path\PathMatcher::matchPath()

Checks if a path matches any pattern in a set of patterns.

Parameters

string $path: The path to match.

string $patterns: A set of patterns separated by a newline.

Return value

bool TRUE if the path matches a pattern, FALSE otherwise.

Overrides PathMatcherInterface::matchPath

File

core/lib/Drupal/Core/Path/PathMatcher.php, line 65

Class

PathMatcher
Provides a path matcher.

Namespace

Drupal\Core\Path

Code

public function matchPath($path, $patterns) {
  if (!isset($this->regexes[$patterns])) {

    // Convert path settings to a regular expression.
    $to_replace = [
      // Replace newlines with a logical 'or'.
      '/(\\r\\n?|\\n)/',
      // Quote asterisks.
      '/\\\\\\*/',
      // Quote <front> keyword.
      '/(^|\\|)\\\\<front\\\\>($|\\|)/',
    ];
    $replacements = [
      '|',
      '.*',
      '\\1' . preg_quote($this
        ->getFrontPagePath(), '/') . '\\2',
    ];
    $patterns_quoted = preg_quote($patterns, '/');
    $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
  }
  return (bool) preg_match($this->regexes[$patterns], $path);
}