ENHANCEMENT: Cached the value for RootURLController::get_homepage_link() between calls.

From: ajshort <andrewjshort@gmail.com>

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88504 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
ajshort 2009-10-11 00:07:22 +00:00 committed by Sam Minnee
parent ea906a50a8
commit 97ac0008b3
4 changed files with 41 additions and 14 deletions

View File

@ -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()}
*/

2
dev/SapphireTest.php Normal file → Executable file
View File

@ -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();

View File

@ -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());

View File

@ -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());