mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
ea906a50a8
commit
97ac0008b3
@ -15,6 +15,11 @@ class RootURLController extends Controller {
|
|||||||
*/
|
*/
|
||||||
protected static $default_homepage_link = 'home';
|
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
|
* 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.
|
* link is trimmed of leading and trailing slashes before returning to ensure consistency.
|
||||||
@ -22,22 +27,30 @@ class RootURLController extends Controller {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function get_homepage_link() {
|
public static function get_homepage_link() {
|
||||||
|
if(!self::$cached_homepage_link) {
|
||||||
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
|
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
|
||||||
$SQL_host = Convert::raw2sql($host);
|
$SQL_host = Convert::raw2sql($host);
|
||||||
|
|
||||||
$candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'");
|
$candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'");
|
||||||
|
|
||||||
if($candidates) foreach($candidates as $candidate) {
|
if($candidates) foreach($candidates as $candidate) {
|
||||||
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
|
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
|
||||||
return trim($candidate->RelativeLink(true), '/');
|
self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Object::has_extension('SiteTree', 'Translatable')) {
|
if(!self::$cached_homepage_link) {
|
||||||
if($link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale())) return $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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::get_default_homepage_link();
|
return self::$cached_homepage_link;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,6 +79,13 @@ class RootURLController extends Controller {
|
|||||||
return false;
|
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()}
|
* @deprecated 2.4 Use {@link RootURLController::get_homepage_link()}
|
||||||
*/
|
*/
|
||||||
|
2
dev/SapphireTest.php
Normal file → Executable file
2
dev/SapphireTest.php
Normal file → Executable file
@ -59,7 +59,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
|||||||
Member::set_password_validator(null);
|
Member::set_password_validator(null);
|
||||||
Cookie::set_report_errors(false);
|
Cookie::set_report_errors(false);
|
||||||
|
|
||||||
// Reset Translatable and Versioned
|
RootURLController::reset();
|
||||||
Translatable::reset();
|
Translatable::reset();
|
||||||
Versioned::reset();
|
Versioned::reset();
|
||||||
DataObject::reset();
|
DataObject::reset();
|
||||||
|
@ -13,6 +13,7 @@ class ContentControllerTest extends FunctionalTest {
|
|||||||
* Test that nested pages, basic actions, and nested/non-nested URL switching works properly
|
* Test that nested pages, basic actions, and nested/non-nested URL switching works properly
|
||||||
*/
|
*/
|
||||||
public function testNestedPages() {
|
public function testNestedPages() {
|
||||||
|
RootURLController::reset();
|
||||||
SiteTree::enable_nested_urls();
|
SiteTree::enable_nested_urls();
|
||||||
|
|
||||||
$this->assertEquals('Home Page', $this->get('/')->getBody());
|
$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/index/')->getBody());
|
||||||
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody());
|
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody());
|
||||||
|
|
||||||
|
RootURLController::reset();
|
||||||
SiteTree::disable_nested_urls();
|
SiteTree::disable_nested_urls();
|
||||||
|
|
||||||
$this->assertEquals('Home Page', $this->get('/')->getBody());
|
$this->assertEquals('Home Page', $this->get('/')->getBody());
|
||||||
|
@ -36,7 +36,9 @@ class RootURLControllerTest extends SapphireTest {
|
|||||||
);
|
);
|
||||||
|
|
||||||
foreach($tests as $domain => $urlSegment) {
|
foreach($tests as $domain => $urlSegment) {
|
||||||
|
RootURLController::reset();
|
||||||
$_SERVER['HTTP_HOST'] = $domain;
|
$_SERVER['HTTP_HOST'] = $domain;
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$urlSegment,
|
$urlSegment,
|
||||||
RootURLController::get_homepage_link(),
|
RootURLController::get_homepage_link(),
|
||||||
@ -59,8 +61,11 @@ class RootURLControllerTest extends SapphireTest {
|
|||||||
$nested->HomepageForDomain = str_replace('www.', null, $_SERVER['HTTP_HOST']);
|
$nested->HomepageForDomain = str_replace('www.', null, $_SERVER['HTTP_HOST']);
|
||||||
$nested->write();
|
$nested->write();
|
||||||
|
|
||||||
|
RootURLController::reset();
|
||||||
SiteTree::disable_nested_urls();
|
SiteTree::disable_nested_urls();
|
||||||
$this->assertEquals('nested-home', RootURLController::get_homepage_link());
|
$this->assertEquals('nested-home', RootURLController::get_homepage_link());
|
||||||
|
|
||||||
|
RootURLController::reset();
|
||||||
SiteTree::enable_nested_urls();
|
SiteTree::enable_nested_urls();
|
||||||
$this->assertEquals('home/nested-home', RootURLController::get_homepage_link());
|
$this->assertEquals('home/nested-home', RootURLController::get_homepage_link());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user