diff --git a/core/includes/path.inc b/core/includes/path.inc index 616bb475ae9d45c7674b9c8af65e5ead5f76099b..a050217be407e21143974d0ef90434c1b2b0bf21 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 449ba2b43326d6235812ee1b5ee5379012691600..95c56dded8b2065cf49859d4125ffb40ada1534b 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 8c0b8006756a4d52d2a6cb458dc2b3a8b3936ea4..114d0781fb07ee4c4512dc291131763010963871 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(); + }