PathMatcher.php 2.9 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Core\Path;

use Drupal\Core\Config\ConfigFactoryInterface;
6 7
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
8 9 10 11 12 13

/**
 * Provides a path matcher.
 */
class PathMatcher implements PathMatcherInterface {

14 15 16 17 18 19 20
  /**
   * Whether the current page is the front page.
   *
   * @var bool
   */
  protected $isCurrentFrontPage;

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  /**
   * The default front page.
   *
   * @var string
   */
  protected $frontPage;

  /**
   * The cache of regular expressions.
   *
   * @var array
   */
  protected $regexes;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

42 43 44 45 46 47 48
  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

49 50 51 52 53
  /**
   * Creates a new PathMatcher.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
54 55
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
56
   */
57
  public function __construct(ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match) {
58
    $this->configFactory = $config_factory;
59
    $this->routeMatch = $route_match;
60 61 62 63 64 65 66 67 68
  }

  /**
   * {@inheritdoc}
   */
  public function matchPath($path, $patterns) {

    if (!isset($this->regexes[$patterns])) {
      // Convert path settings to a regular expression.
69
      $to_replace = [
70 71 72 73 74 75
        // Replace newlines with a logical 'or'.
        '/(\r\n?|\n)/',
        // Quote asterisks.
        '/\\\\\*/',
        // Quote <front> keyword.
        '/(^|\|)\\\\<front\\\\>($|\|)/',
76 77
      ];
      $replacements = [
78 79
        '|',
        '.*',
80
        '\1' . preg_quote($this->getFrontPagePath(), '/') . '\2',
81
      ];
82 83 84 85 86
      $patterns_quoted = preg_quote($patterns, '/');
      $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
    }
    return (bool) preg_match($this->regexes[$patterns], $path);
  }
87 88 89 90 91 92 93

  /**
   * {@inheritdoc}
   */
  public function isFrontPage() {
    // Cache the result as this is called often.
    if (!isset($this->isCurrentFrontPage)) {
94 95 96 97 98
      $this->isCurrentFrontPage = FALSE;
      // Ensure that the code can also be executed when there is no active
      // route match, like on exception responses.
      if ($this->routeMatch->getRouteName()) {
        $url = Url::fromRouteMatch($this->routeMatch);
99
        $this->isCurrentFrontPage = ($url->getRouteName() && '/' . $url->getInternalPath() === $this->getFrontPagePath());
100
      }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }
    return $this->isCurrentFrontPage;
  }

  /**
   * Gets the current front page path.
   *
   * @return string
   *   The front page path.
   */
  protected function getFrontPagePath() {
    // Lazy-load front page config.
    if (!isset($this->frontPage)) {
      $this->frontPage = $this->configFactory->get('system.site')
        ->get('page.front');
    }
    return $this->frontPage;
  }
119

120
}