PathMatcher.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Drupal\Core\Path;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Routing\RouteMatchInterface;
  5. use Drupal\Core\Url;
  6. /**
  7. * Provides a path matcher.
  8. */
  9. class PathMatcher implements PathMatcherInterface {
  10. /**
  11. * Whether the current page is the front page.
  12. *
  13. * @var bool
  14. */
  15. protected $isCurrentFrontPage;
  16. /**
  17. * The default front page.
  18. *
  19. * @var string
  20. */
  21. protected $frontPage;
  22. /**
  23. * The cache of regular expressions.
  24. *
  25. * @var array
  26. */
  27. protected $regexes;
  28. /**
  29. * The config factory service.
  30. *
  31. * @var \Drupal\Core\Config\ConfigFactoryInterface
  32. */
  33. protected $configFactory;
  34. /**
  35. * The current route match.
  36. *
  37. * @var \Drupal\Core\Routing\RouteMatchInterface
  38. */
  39. protected $routeMatch;
  40. /**
  41. * Creates a new PathMatcher.
  42. *
  43. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  44. * The config factory.
  45. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  46. * The current route match.
  47. */
  48. public function __construct(ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match) {
  49. $this->configFactory = $config_factory;
  50. $this->routeMatch = $route_match;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function matchPath($path, $patterns) {
  56. if (!isset($this->regexes[$patterns])) {
  57. // Convert path settings to a regular expression.
  58. $to_replace = [
  59. // Replace newlines with a logical 'or'.
  60. '/(\r\n?|\n)/',
  61. // Quote asterisks.
  62. '/\\\\\*/',
  63. // Quote <front> keyword.
  64. '/(^|\|)\\\\<front\\\\>($|\|)/',
  65. ];
  66. $replacements = [
  67. '|',
  68. '.*',
  69. '\1' . preg_quote($this->getFrontPagePath(), '/') . '\2',
  70. ];
  71. $patterns_quoted = preg_quote($patterns, '/');
  72. $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
  73. }
  74. return (bool) preg_match($this->regexes[$patterns], $path);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function isFrontPage() {
  80. // Cache the result as this is called often.
  81. if (!isset($this->isCurrentFrontPage)) {
  82. $this->isCurrentFrontPage = FALSE;
  83. // Ensure that the code can also be executed when there is no active
  84. // route match, like on exception responses.
  85. if ($this->routeMatch->getRouteName()) {
  86. $url = Url::fromRouteMatch($this->routeMatch);
  87. $this->isCurrentFrontPage = ($url->getRouteName() && '/' . $url->getInternalPath() === $this->getFrontPagePath());
  88. }
  89. }
  90. return $this->isCurrentFrontPage;
  91. }
  92. /**
  93. * Gets the current front page path.
  94. *
  95. * @return string
  96. * The front page path.
  97. */
  98. protected function getFrontPagePath() {
  99. // Lazy-load front page config.
  100. if (!isset($this->frontPage)) {
  101. $this->frontPage = $this->configFactory->get('system.site')
  102. ->get('page.front');
  103. }
  104. return $this->frontPage;
  105. }
  106. }