mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FEATURE: added Session::clearAll() functionality. ENHANCEMENT: Added Unit Tests covering Session API. MINOR: Tided up formatting in session class and included doc comments for API level documentation (from r97024)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102422 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
8a8501c854
commit
46a5670aed
@ -11,9 +11,11 @@
|
|||||||
*
|
*
|
||||||
* The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session data.
|
* The instance object is basically just a way of manipulating a set of nested maps, and isn't specific to session data.
|
||||||
* This class is currently really basic and could do with a more well-thought-out implementation
|
* This class is currently really basic and could do with a more well-thought-out implementation
|
||||||
|
*
|
||||||
* @package sapphire
|
* @package sapphire
|
||||||
* @subpackage control
|
* @subpackage control
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Session {
|
class Session {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -23,6 +25,23 @@ class Session {
|
|||||||
|
|
||||||
static protected $session_ips = array();
|
static protected $session_ips = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Session data
|
||||||
|
*/
|
||||||
|
protected $data = array();
|
||||||
|
protected $changedData = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new session object, with the given starting data
|
||||||
|
*
|
||||||
|
* @param $data Can be an array of data (such as $_SESSION) or another Session object to clone.
|
||||||
|
*/
|
||||||
|
function __construct($data) {
|
||||||
|
if($data instanceof Session) $data = $data->inst_getAll();
|
||||||
|
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or
|
* Provide an <code>array</code> of rules specifing timeouts for IPv4 address ranges or
|
||||||
* individual IPv4 addresses. The key is an IP address or range and the value is the time
|
* individual IPv4 addresses. The key is an IP address or range and the value is the time
|
||||||
@ -47,39 +66,57 @@ class Session {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function set($name, $val) {
|
|
||||||
return Controller::curr()->getSession()->inst_set($name, $val);
|
|
||||||
}
|
|
||||||
public static function addToArray($name, $val) {
|
public static function addToArray($name, $val) {
|
||||||
return Controller::curr()->getSession()->inst_addToArray($name, $val);
|
return Controller::curr()->getSession()->inst_addToArray($name, $val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a key/value pair in the session
|
||||||
|
*
|
||||||
|
* @param string $name Key
|
||||||
|
* @param string $val Value
|
||||||
|
*/
|
||||||
|
public static function set($name, $val) {
|
||||||
|
return Controller::curr()->getSession()->inst_set($name, $val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a specific value by session key
|
||||||
|
*
|
||||||
|
* @param string $name Key to lookup
|
||||||
|
*/
|
||||||
public static function get($name) {
|
public static function get($name) {
|
||||||
return Controller::curr()->getSession()->inst_get($name);
|
return Controller::curr()->getSession()->inst_get($name);
|
||||||
}
|
}
|
||||||
public static function clear($name) {
|
|
||||||
return Controller::curr()->getSession()->inst_clear($name);
|
/**
|
||||||
}
|
* Return all the values in session
|
||||||
|
*/
|
||||||
public static function getAll() {
|
public static function getAll() {
|
||||||
return Controller::curr()->getSession()->inst_getAll();
|
return Controller::curr()->getSession()->inst_getAll();
|
||||||
}
|
}
|
||||||
public static function save() {
|
|
||||||
return Controller::curr()->getSession()->inst_save();
|
/**
|
||||||
|
* Clear a given session key, value pair.
|
||||||
|
*
|
||||||
|
* @param string $name Key to lookup
|
||||||
|
*/
|
||||||
|
public static function clear($name) {
|
||||||
|
return Controller::curr()->getSession()->inst_clear($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Session data
|
* Clear all the values
|
||||||
*/
|
*/
|
||||||
protected $data = array();
|
public static function clearAll() {
|
||||||
protected $changedData = array();
|
return Controller::curr()->getSession()->inst_clearAll();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new session object, with the given starting data
|
* Save all the values in our session to $_SESSION
|
||||||
* @param $data Can be an array of data (such as $_SESSION) or another Session object to clone.
|
|
||||||
*/
|
*/
|
||||||
function __construct($data) {
|
public static function save() {
|
||||||
if($data instanceof Session) $data = $data->inst_getAll();
|
return Controller::curr()->getSession()->inst_save();
|
||||||
|
|
||||||
$this->data = $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function inst_set($name, $val) {
|
public function inst_set($name, $val) {
|
||||||
@ -162,6 +199,14 @@ class Session {
|
|||||||
$diffVar = null;
|
$diffVar = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function inst_clearAll() {
|
||||||
|
if($this->data && is_array($this->data)) {
|
||||||
|
foreach(array_keys($this->data) as $key) {
|
||||||
|
$this->inst_clear($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function inst_getAll() {
|
public function inst_getAll() {
|
||||||
return $this->data;
|
return $this->data;
|
||||||
}
|
}
|
||||||
@ -204,6 +249,7 @@ class Session {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize session.
|
* Initialize session.
|
||||||
|
*
|
||||||
* @param string $sid Start the session with a specific ID
|
* @param string $sid Start the session with a specific ID
|
||||||
*/
|
*/
|
||||||
public static function start($sid = null) {
|
public static function start($sid = null) {
|
||||||
@ -220,6 +266,7 @@ class Session {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroy the active session.
|
* Destroy the active session.
|
||||||
|
*
|
||||||
* @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove
|
* @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove
|
||||||
*/
|
*/
|
||||||
public static function destroy($removeCookie = true) {
|
public static function destroy($removeCookie = true) {
|
||||||
@ -258,7 +305,7 @@ class Session {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enter description here...
|
* Set the timeout of a Session value
|
||||||
*
|
*
|
||||||
* @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed.
|
* @param int $timeout Time until a session expires in seconds. Defaults to expire when browser is closed.
|
||||||
*/
|
*/
|
||||||
@ -270,4 +317,3 @@ class Session {
|
|||||||
return self::$timeout;
|
return self::$timeout;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
45
tests/SessionTest.php
Normal file
45
tests/SessionTest.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests to cover the {@link Session} class
|
||||||
|
*
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
|
|
||||||
|
class SessionTest extends SapphireTest {
|
||||||
|
|
||||||
|
function testGetSetBasics() {
|
||||||
|
Session::set('Test', 'Test');
|
||||||
|
|
||||||
|
$this->assertEquals(Session::get('Test'), 'Test');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testClearElement() {
|
||||||
|
Session::set('Test', 'Test');
|
||||||
|
Session::clear('Test');
|
||||||
|
|
||||||
|
$this->assertEquals(Session::get('Test'), '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testClearAllElements() {
|
||||||
|
Session::set('Test', 'Test');
|
||||||
|
Session::set('Test-1', 'Test-1');
|
||||||
|
|
||||||
|
Session::clearAll();
|
||||||
|
|
||||||
|
// should session get return null? The array key should probably be
|
||||||
|
// unset from the data array
|
||||||
|
$this->assertEquals(Session::get('Test'), '');
|
||||||
|
$this->assertEquals(Session::get('Test-1'), '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function testGetAllElements() {
|
||||||
|
Session::set('Test', 'Test');
|
||||||
|
Session::set('Test-2', 'Test-2');
|
||||||
|
|
||||||
|
$session = Session::getAll();
|
||||||
|
|
||||||
|
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user