diff --git a/core/Session.php b/core/Session.php index 37d8f86ec..b3c49c947 100644 --- a/core/Session.php +++ b/core/Session.php @@ -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. * This class is currently really basic and could do with a more well-thought-out implementation + * * @package sapphire * @subpackage control */ + class Session { /** @@ -23,6 +25,23 @@ class Session { 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 array 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 @@ -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) { 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) { 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() { return Controller::curr()->getSession()->inst_getAll(); } - public static function save() { - return Controller::curr()->getSession()->inst_save(); - } - - /** - * 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. + * Clear a given session key, value pair. + * + * @param string $name Key to lookup */ - function __construct($data) { - if($data instanceof Session) $data = $data->inst_getAll(); - - $this->data = $data; + public static function clear($name) { + return Controller::curr()->getSession()->inst_clear($name); + } + + /** + * Clear all the values + */ + public static function clearAll() { + return Controller::curr()->getSession()->inst_clearAll(); + } + + /** + * Save all the values in our session to $_SESSION + */ + public static function save() { + return Controller::curr()->getSession()->inst_save(); } public function inst_set($name, $val) { @@ -157,11 +194,19 @@ class Session { $var = &$var[$n]; $diffVar = &$diffVar[$n]; } - + $var = 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() { return $this->data; } @@ -204,6 +249,7 @@ class Session { /** * Initialize session. + * * @param string $sid Start the session with a specific ID */ public static function start($sid = null) { @@ -220,6 +266,7 @@ class Session { /** * Destroy the active session. + * * @param bool $removeCookie If set to TRUE, removes the user's cookie, FALSE does not remove */ 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. */ @@ -269,5 +316,4 @@ class Session { public static function get_timeout() { return self::$timeout; } -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/tests/SessionTest.php b/tests/SessionTest.php new file mode 100644 index 000000000..fd85fb007 --- /dev/null +++ b/tests/SessionTest.php @@ -0,0 +1,45 @@ +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')); + } +} \ No newline at end of file