Merge pull request #3198 from tractorcow/pulls/3.1/lazy-session

Versioned no longer sets redundant session data
This commit is contained in:
Mateusz U 2014-06-11 16:55:20 +12:00
commit a584c8c83a
2 changed files with 31 additions and 10 deletions

View File

@ -27,6 +27,11 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
*/
protected $liveStage;
/**
* The default reading mode
*/
const DEFAULT_MODE = 'Stage.Live';
/**
* A version that a DataObject should be when it is 'migrating',
* that is, when it is in the process of moving from one stage to another.
@ -924,6 +929,7 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
//-----------------------------------------------------------------------------------------------//
/**
* Choose the stage the site is currently on.
*
@ -939,29 +945,37 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
* @param Session $session Optional session within which to store the resulting stage
*/
public static function choose_site_stage($session = null) {
// Default to Live
$mode = 'Stage.Live';
// Check any pre-existing session mode
$preexistingMode = $session
? $session->inst_get('readingMode')
: Session::get('readingMode');
// Get reading mode
// Determine the reading mode
if(isset($_GET['stage'])) {
$stage = ucfirst(strtolower($_GET['stage']));
if(!in_array($stage, array('Stage', 'Live'))) $stage = 'Live';
$mode = 'Stage.' . $stage;
} elseif (isset($_GET['archiveDate']) && strtotime($_GET['archiveDate'])) {
$mode = 'Archive.' . $_GET['archiveDate'];
} elseif($session) {
$mode = $session->inst_get('readingMode') ?: $mode;
} elseif($preexistingMode) {
$mode = $preexistingMode;
} else {
$mode = Session::get('readingMode') ?: $mode;
$mode = self::DEFAULT_MODE;
}
// Save reading mode
Versioned::set_reading_mode($mode);
// Try not to store the mode in the session if not needed
if(($preexistingMode && $preexistingMode !== $mode)
|| (!$preexistingMode && $mode !== self::DEFAULT_MODE)
) {
if($session) {
$session->inst_set('readingMode', $mode);
} else {
Session::set('readingMode', $mode);
}
}
if(!headers_sent() && !Director::is_cli()) {
if(Versioned::current_stage() == 'Live') {

View File

@ -567,6 +567,13 @@ class VersionedTest extends SapphireTest {
'Check that subsequent requests in the same session remain in Live mode'
);
// Test that session doesn't redundantly store the default stage if it doesn't need to
$session2 = new Session(array());
Director::test('/', null, $session2);
$this->assertEmpty($session2->inst_changedData());
Director::test('/?stage=Live', null, $session2);
$this->assertEmpty($session2->inst_changedData());
// Test choose_site_stage
Session::set('readingMode', 'Stage.Stage');
Versioned::choose_site_stage();