diff --git a/core/includes/path.inc b/core/includes/path.inc index 616bb47..a050217 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 449ba2b..be55668 100644 --- a/core/lib/Drupal/Core/Path/PathMatcher.php +++ b/core/lib/Drupal/Core/Path/PathMatcher.php @@ -15,6 +15,13 @@ class PathMatcher implements PathMatcherInterface { /** + * Whether the current page is the front page. + * + * @var bool + */ + protected $isCurrentFrontPage = FALSE; + + /** * The default front page. * * @var string @@ -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 (!$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 8c0b800..114d078 100644 --- a/core/lib/Drupal/Core/Path/PathMatcherInterface.php +++ b/core/lib/Drupal/Core/Path/PathMatcherInterface.php @@ -25,4 +25,12 @@ */ 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(); + }