Chris@0: configFactory = $config_factory; Chris@0: $this->routeMatch = $route_match; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function matchPath($path, $patterns) { Chris@0: Chris@0: if (!isset($this->regexes[$patterns])) { Chris@0: // Convert path settings to a regular expression. Chris@0: $to_replace = [ Chris@0: // Replace newlines with a logical 'or'. Chris@0: '/(\r\n?|\n)/', Chris@0: // Quote asterisks. Chris@0: '/\\\\\*/', Chris@0: // Quote keyword. Chris@0: '/(^|\|)\\\\($|\|)/', Chris@0: ]; Chris@0: $replacements = [ Chris@0: '|', Chris@0: '.*', Chris@0: '\1' . preg_quote($this->getFrontPagePath(), '/') . '\2', Chris@0: ]; Chris@0: $patterns_quoted = preg_quote($patterns, '/'); Chris@0: $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/'; Chris@0: } Chris@0: return (bool) preg_match($this->regexes[$patterns], $path); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isFrontPage() { Chris@0: // Cache the result as this is called often. Chris@0: if (!isset($this->isCurrentFrontPage)) { Chris@0: $this->isCurrentFrontPage = FALSE; Chris@0: // Ensure that the code can also be executed when there is no active Chris@0: // route match, like on exception responses. Chris@0: if ($this->routeMatch->getRouteName()) { Chris@0: $url = Url::fromRouteMatch($this->routeMatch); Chris@0: $this->isCurrentFrontPage = ($url->getRouteName() && '/' . $url->getInternalPath() === $this->getFrontPagePath()); Chris@0: } Chris@0: } Chris@0: return $this->isCurrentFrontPage; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current front page path. Chris@0: * Chris@0: * @return string Chris@0: * The front page path. Chris@0: */ Chris@0: protected function getFrontPagePath() { Chris@0: // Lazy-load front page config. Chris@0: if (!isset($this->frontPage)) { Chris@0: $this->frontPage = $this->configFactory->get('system.site') Chris@0: ->get('page.front'); Chris@0: } Chris@0: return $this->frontPage; Chris@0: } Chris@0: Chris@0: }