Making CakeSession::write() able to write hashes of data.

This commit is contained in:
mark_story 2010-07-15 23:54:01 -04:00
parent 59ea917c23
commit e660416545
2 changed files with 32 additions and 5 deletions

View file

@ -417,15 +417,24 @@ class CakeSession {
* @param string $value Value to write
* @return boolean True if the write was successful, false if the write failed
*/
public static function write($name, $value) {
public static function write($name, $value = null) {
if (empty($name)) {
return false;
}
if (in_array($name, self::$watchKeys)) {
trigger_error(sprintf(__('Writing session key {%s}: %s'), $name, Debugger::exportVar($value)), E_USER_NOTICE);
$write = $name;
if (!is_array($name)) {
$write = array($name => $value);
}
self::__overwrite($_SESSION, Set::insert($_SESSION, $name, $value));
return (Set::classicExtract($_SESSION, $name) === $value);
foreach ($write as $key => $val) {
if (in_array($key, self::$watchKeys)) {
trigger_error(sprintf(__('Writing session key {%s}: %s'), $key, Debugger::exportVar($val)), E_USER_NOTICE);
}
self::__overwrite($_SESSION, Set::insert($_SESSION, $key, $val));
if (Set::classicExtract($_SESSION, $key) !== $val) {
return false;
}
}
return true;
}
/**

View file

@ -148,6 +148,24 @@ class CakeSessionTest extends CakeTestCase {
$this->assertEqual('value', $result);
}
/**
* test writing a hash of values/
*
* @return void
*/
function testWriteArray() {
$result = TestCakeSession::write(array(
'one' => 1,
'two' => 2,
'three' => array('something'),
'null' => null
));
$this->assertTrue($result);
$this->assertEquals(1, TestCakeSession::read('one'));
$this->assertEquals(array('something'), TestCakeSession::read('three'));
$this->assertEquals(null, TestCakeSession::read('null'));
}
/**
* testId method
*