From 3cfff37376e4d3faf0f64ca12d0e11e774deb636 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sun, 13 Jul 2014 09:53:01 +0100 Subject: [PATCH] Issue #2301975 by kim.pepper: Move drupal_is_front_page to PathMatcher service. --- core/includes/path.inc | 16 ++------ core/lib/Drupal/Core/Path/PathMatcher.php | 39 ++++++++++++++++--- .../Drupal/Core/Path/PathMatcherInterface.php | 8 ++++ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/core/includes/path.inc b/core/includes/path.inc index 616bb475ae9d..a050217be407 100644 --- a/core/includes/path.inc +++ b/core/includes/path.inc @@ -15,20 +15,12 @@ * * @return * Boolean value: TRUE if the current page is the front page; FALSE if otherwise. + * + * @deprecated as of Drupal 8.0. Use + * \Drupal\Core\Path\PathMatcherInterface::isFrontPage() instead. */ function drupal_is_front_page() { - // Use the advanced drupal_static() pattern, since this is called very often. - static $drupal_static_fast; - if (!isset($drupal_static_fast)) { - $drupal_static_fast['is_front_page'] = &drupal_static(__FUNCTION__); - } - $is_front_page = &$drupal_static_fast['is_front_page']; - - if (!isset($is_front_page)) { - $is_front_page = (current_path() == \Drupal::config('system.site')->get('page.front')); - } - - return $is_front_page; + return \Drupal::service('path.matcher')->isFrontPage(); } /** diff --git a/core/lib/Drupal/Core/Path/PathMatcher.php b/core/lib/Drupal/Core/Path/PathMatcher.php index 449ba2b43326..95c56dded8b2 100644 --- a/core/lib/Drupal/Core/Path/PathMatcher.php +++ b/core/lib/Drupal/Core/Path/PathMatcher.php @@ -14,6 +14,13 @@ */ class PathMatcher implements PathMatcherInterface { + /** + * Whether the current page is the front page. + * + * @var bool + */ + protected $isCurrentFrontPage; + /** * The default front page. * @@ -51,10 +58,6 @@ public function __construct(ConfigFactoryInterface $config_factory) { public function matchPath($path, $patterns) { if (!isset($this->regexes[$patterns])) { - // Lazy-load front page config. - if (!isset($this->frontPage)) { - $this->frontPage = $this->configFactory->get('system.site')->get('page.front'); - } // Convert path settings to a regular expression. $to_replace = array( // Replace newlines with a logical 'or'. @@ -67,11 +70,37 @@ public function matchPath($path, $patterns) { $replacements = array( '|', '.*', - '\1' . preg_quote($this->frontPage, '/') . '\2', + '\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); } + + /** + * {@inheritdoc} + */ + public function isFrontPage() { + // Cache the result as this is called often. + if (!isset($this->isCurrentFrontPage)) { + $this->isCurrentFrontPage = (current_path() == $this->getFrontPagePath()); + } + 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; + } } diff --git a/core/lib/Drupal/Core/Path/PathMatcherInterface.php b/core/lib/Drupal/Core/Path/PathMatcherInterface.php index 8c0b8006756a..114d0781fb07 100644 --- a/core/lib/Drupal/Core/Path/PathMatcherInterface.php +++ b/core/lib/Drupal/Core/Path/PathMatcherInterface.php @@ -25,4 +25,12 @@ interface PathMatcherInterface { */ public function matchPath($path, $patterns); + /** + * Checks if the current page is the front page. + * + * @return bool + * TRUE if the current page is the front page. + */ + public function isFrontPage(); + } -- GitLab