From 97ac0008b30572a7958ab101aab195d623018f93 Mon Sep 17 00:00:00 2001 From: ajshort Date: Sun, 11 Oct 2009 00:07:22 +0000 Subject: [PATCH] ENHANCEMENT: Cached the value for RootURLController::get_homepage_link() between calls. From: ajshort git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88504 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/RootURLController.php | 46 ++++++++++++++++++------- dev/SapphireTest.php | 2 +- tests/control/ContentControllerTest.php | 2 ++ tests/control/RootURLControllerTest.php | 5 +++ 4 files changed, 41 insertions(+), 14 deletions(-) mode change 100644 => 100755 dev/SapphireTest.php diff --git a/core/control/RootURLController.php b/core/control/RootURLController.php index db4521895..31f88fcc9 100755 --- a/core/control/RootURLController.php +++ b/core/control/RootURLController.php @@ -15,6 +15,11 @@ class RootURLController extends Controller { */ protected static $default_homepage_link = 'home'; + /** + * @var string + */ + protected static $cached_homepage_link; + /** * Get the full form (e.g. /home/) relative link to the home page for the current HTTP_HOST value. Note that the * link is trimmed of leading and trailing slashes before returning to ensure consistency. @@ -22,22 +27,30 @@ class RootURLController extends Controller { * @return string */ public static function get_homepage_link() { - $host = str_replace('www.', null, $_SERVER['HTTP_HOST']); - $SQL_host = Convert::raw2sql($host); - - $candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'"); - - if($candidates) foreach($candidates as $candidate) { - if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) { - return trim($candidate->RelativeLink(true), '/'); + if(!self::$cached_homepage_link) { + $host = str_replace('www.', null, $_SERVER['HTTP_HOST']); + $SQL_host = Convert::raw2sql($host); + $candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'"); + + if($candidates) foreach($candidates as $candidate) { + if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) { + self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/'); + } + } + + if(!self::$cached_homepage_link) { + if ( + Object::has_extension('SiteTree', 'Translatable') + && $link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale()) + ) { + self::$cached_homepage_link = $link; + } else { + self::$cached_homepage_link = self::get_default_homepage_link(); + } } } - if(Object::has_extension('SiteTree', 'Translatable')) { - if($link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale())) return $link; - } - - return self::get_default_homepage_link(); + return self::$cached_homepage_link; } /** @@ -66,6 +79,13 @@ class RootURLController extends Controller { return false; } + /** + * Resets the cached homepage link value - useful for testing. + */ + public static function reset() { + self::$cached_homepage_link = null; + } + /** * @deprecated 2.4 Use {@link RootURLController::get_homepage_link()} */ diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php old mode 100644 new mode 100755 index 0c680b33d..501e43795 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -59,7 +59,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase { Member::set_password_validator(null); Cookie::set_report_errors(false); - // Reset Translatable and Versioned + RootURLController::reset(); Translatable::reset(); Versioned::reset(); DataObject::reset(); diff --git a/tests/control/ContentControllerTest.php b/tests/control/ContentControllerTest.php index d8ae9cfb2..5f45301b6 100755 --- a/tests/control/ContentControllerTest.php +++ b/tests/control/ContentControllerTest.php @@ -13,6 +13,7 @@ class ContentControllerTest extends FunctionalTest { * Test that nested pages, basic actions, and nested/non-nested URL switching works properly */ public function testNestedPages() { + RootURLController::reset(); SiteTree::enable_nested_urls(); $this->assertEquals('Home Page', $this->get('/')->getBody()); @@ -27,6 +28,7 @@ class ContentControllerTest extends FunctionalTest { $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/index/')->getBody()); $this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody()); + RootURLController::reset(); SiteTree::disable_nested_urls(); $this->assertEquals('Home Page', $this->get('/')->getBody()); diff --git a/tests/control/RootURLControllerTest.php b/tests/control/RootURLControllerTest.php index ef62aaf67..921277f49 100755 --- a/tests/control/RootURLControllerTest.php +++ b/tests/control/RootURLControllerTest.php @@ -36,7 +36,9 @@ class RootURLControllerTest extends SapphireTest { ); foreach($tests as $domain => $urlSegment) { + RootURLController::reset(); $_SERVER['HTTP_HOST'] = $domain; + $this->assertEquals( $urlSegment, RootURLController::get_homepage_link(), @@ -59,8 +61,11 @@ class RootURLControllerTest extends SapphireTest { $nested->HomepageForDomain = str_replace('www.', null, $_SERVER['HTTP_HOST']); $nested->write(); + RootURLController::reset(); SiteTree::disable_nested_urls(); $this->assertEquals('nested-home', RootURLController::get_homepage_link()); + + RootURLController::reset(); SiteTree::enable_nested_urls(); $this->assertEquals('home/nested-home', RootURLController::get_homepage_link());