Merge pull request #5470 from cakephp/2.7-session

Backport Session consume() to 2.x
This commit is contained in:
Mark Story 2014-12-26 12:22:20 -05:00
commit 7bbc3dfd90
4 changed files with 70 additions and 6 deletions

View file

@ -41,7 +41,7 @@ class SessionComponent extends Component {
}
/**
* Used to write a value to a session key.
* Writes a value to a session key.
*
* In your controller: $this->Session->write('Controller.sessKey', 'session value');
*
@ -56,7 +56,7 @@ class SessionComponent extends Component {
}
/**
* Used to read a session values for a key or return values for all keys.
* Reads a session value for a key or returns values for all keys.
*
* In your controller: $this->Session->read('Controller.sessKey');
* Calling the method without a param will return all session vars
@ -70,7 +70,7 @@ class SessionComponent extends Component {
}
/**
* Wrapper for SessionComponent::del();
* Deletes a session value for a key.
*
* In your controller: $this->Session->delete('Controller.sessKey');
*
@ -83,7 +83,19 @@ class SessionComponent extends Component {
}
/**
* Used to check if a session variable is set
* Reads and deletes a session value for a key.
*
* In your controller: `$this->Session->consume('Controller.sessKey');`
*
* @param string $name the name of the session key you want to read
* @return mixed values from the session vars
*/
public function consume($name) {
return CakeSession::consume($name);
}
/**
* Checks if a session variable is set.
*
* In your controller: $this->Session->check('Controller.sessKey');
*

View file

@ -432,6 +432,24 @@ class CakeSession {
return true;
}
/**
* Reads and deletes a variable from session.
*
* @param string $name The key to read and remove (or a path as sent to Hash.extract).
* @return mixed The value of the session variable, null if session not available,
* session not started, or provided name not found in the session.
*/
public static function consume($name) {
if (empty($name)) {
return null;
}
$value = self::read($name);
if ($value !== null) {
self::_overwrite($_SESSION, Hash::remove($_SESSION, $name));
}
return $value;
}
/**
* Helper method to destroy invalid sessions.
*

View file

@ -328,6 +328,28 @@ class CakeSessionTest extends CakeTestCase {
);
}
/**
* Test consuming session data.
*
* @return void
*/
public function testConsume() {
TestCakeSession::write('Some.string', 'value');
TestCakeSession::write('Some.array', array('key1' => 'value1', 'key2' => 'value2'));
$this->assertEquals('value', TestCakeSession::read('Some.string'));
$value = TestCakeSession::consume('Some.string');
$this->assertEquals('value', $value);
$this->assertFalse(TestCakeSession::check('Some.string'));
$value = TestCakeSession::consume('');
$this->assertNull($value);
$value = TestCakeSession::consume(null);
$this->assertNull($value);
$value = TestCakeSession::consume('Some.array');
$expected = array('key1' => 'value1', 'key2' => 'value2');
$this->assertEquals($expected, $value);
$this->assertFalse(TestCakeSession::check('Some.array'));
}
/**
* testId method
*

View file

@ -30,7 +30,7 @@ App::uses('CakeSession', 'Model/Datasource');
class SessionHelper extends AppHelper {
/**
* Used to read a session values set in a controller for a key or return values for all keys.
* Reads a session value for a key or returns values for all keys.
*
* In your view: `$this->Session->read('Controller.sessKey');`
* Calling the method without a param will return all session vars
@ -44,7 +44,19 @@ class SessionHelper extends AppHelper {
}
/**
* Used to check is a session key has been set
* Reads and deletes a session value for a key.
*
* In your view: `$this->Session->consume('Controller.sessKey');`
*
* @param string $name the name of the session key you want to read
* @return mixed values from the session vars
*/
public function consume($name) {
return CakeSession::consume($name);
}
/**
* Checks if a session key has been set.
*
* In your view: `$this->Session->check('Controller.sessKey');`
*