Versioned no longer sets redundant session data

This commit is contained in:
Damian Mooyman 2014-06-11 16:14:56 +12:00
parent 8c10e12529
commit 4c5de82625
2 changed files with 31 additions and 10 deletions

View File

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

View File

@ -567,6 +567,13 @@ class VersionedTest extends SapphireTest {
'Check that subsequent requests in the same session remain in Live mode' '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 // Test choose_site_stage
Session::set('readingMode', 'Stage.Stage'); Session::set('readingMode', 'Stage.Stage');
Versioned::choose_site_stage(); Versioned::choose_site_stage();