Skip to content
Permalink
Browse files

Making session storage classes not static, there was no real reason t…

…hey were static before, and removing the static methods allows them to have constructors. This will allow moving database specific features from CakeSession to DatabaseSession.
  • Loading branch information...
markstory committed Sep 6, 2010
1 parent 5257a66 commit 27ae7f1f44f6bf63f4f61a188ad4d1f7a683f7ee
@@ -559,14 +559,14 @@ protected static function _configureSession() {
call_user_func_array('session_set_save_handler', $sessionConfig['handler']);
}
if (!empty($sessionConfig['handler']['engine'])) {
$class = self::_getHandler($sessionConfig['handler']['engine']);
$handler = self::_getHandler($sessionConfig['handler']['engine']);
session_set_save_handler(
array($class, 'open'),
array($class, 'close'),
array($class, 'read'),
array($class, 'write'),
array($class, 'destroy'),
array($class, 'gc')
array($handler, 'open'),
array($handler, 'close'),
array($handler, 'read'),
array($handler, 'write'),
array($handler, 'destroy'),
array($handler, 'gc')
);
}
Configure::write('Session', $sessionConfig);
@@ -587,11 +587,11 @@ protected static function _getHandler($handler) {
if (!class_exists($class)) {
throw new Exception(sprintf(__('Could not load %s to handle the session.'), $class));
}
$reflect = new ReflectionClass($class);
if (!$reflect->implementsInterface('CakeSessionHandlerInterface')) {
throw new Exception(__('Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.'));
$handler = new $class();
if ($handler instanceof CakeSessionHandlerInterface) {
return $handler;
}
return $class;
throw new Exception(__('Chosen SessionHandler does not implement CakeSessionHandlerInterface it cannot be used with an engine key.'));
}
/**
@@ -768,22 +768,22 @@ interface CakeSessionHandlerInterface {
*
* @return boolean Success
*/
public static function open();
public function open();
/**
* Method called on close of a session.
*
* @return boolean Success
*/
public static function close();
public function close();
/**
* Method used to read from a session.
*
* @param mixed $id The key of the value to read
* @return mixed The value of the key or false if it does not exist
*/
public static function read($id);
public function read($id);
/**
* Helper function called on write for sessions.
@@ -792,15 +792,15 @@ public static function read($id);
* @param mixed $data The value of the data to be saved.
* @return boolean True for successful write, false otherwise.
*/
public static function write($id, $data);
public function write($id, $data);
/**
* Method called on the destruction of a session.
*
* @param integer $id ID that uniquely identifies session in database
* @return boolean True for successful delete, false otherwise.
*/
public static function destroy($id);
public function destroy($id);
/**
* Run the Garbage collection on the session storage. This method should vacuum all
@@ -809,7 +809,7 @@ public static function destroy($id);
* @param integer $expires Timestamp (defaults to current time)
* @return boolean Success
*/
public static function gc($expires = null);
public function gc($expires = null);
}
@@ -29,7 +29,7 @@ class CacheSession implements CakeSessionHandlerInterface {
* @return boolean Success
* @access private
*/
public static function open() {
public function open() {
return true;
}
@@ -39,7 +39,7 @@ public static function open() {
* @return boolean Success
* @access private
*/
public static function close() {
public function close() {
$probability = mt_rand(1, 150);
if ($probability <= 3) {
Cache::gc();
@@ -54,7 +54,7 @@ public static function close() {
* @return mixed The value of the key or false if it does not exist
* @access private
*/
public static function read($id) {
public function read($id) {
return Cache::read($id, Configure::read('Session.handler.config'));
}
@@ -66,7 +66,7 @@ public static function read($id) {
* @return boolean True for successful write, false otherwise.
* @access private
*/
public static function write($id, $data) {
public function write($id, $data) {
return Cache::write($id, $data, Configure::read('Session.handler.config'));
}
@@ -77,7 +77,7 @@ public static function write($id, $data) {
* @return boolean True for successful delete, false otherwise.
* @access private
*/
public static function destroy($id) {
public function destroy($id) {
return Cache::delete($id, Configure::read('Session.handler.config'));
}
@@ -88,7 +88,7 @@ public static function destroy($id) {
* @return boolean Success
* @access private
*/
public static function gc($expires = null) {
public function gc($expires = null) {
return Cache::gc();
}
}
@@ -29,7 +29,7 @@ class DatabaseSession implements CakeSessionHandlerInterface {
* @return boolean Success
* @access private
*/
public static function open() {
public function open() {
return true;
}
@@ -39,7 +39,7 @@ public static function open() {
* @return boolean Success
* @access private
*/
public static function close() {
public function close() {
$probability = mt_rand(1, 150);
if ($probability <= 3) {
DatabaseSession::gc();
@@ -54,7 +54,7 @@ public static function close() {
* @return mixed The value of the key or false if it does not exist
* @access private
*/
public static function read($id) {
public function read($id) {
$model =& ClassRegistry::getObject('Session');
$row = $model->find('first', array(
@@ -76,7 +76,7 @@ public static function read($id) {
* @return boolean True for successful write, false otherwise.
* @access private
*/
public static function write($id, $data) {
public function write($id, $data) {
$expires = time() + (Configure::read('Session.timeout') * 60);
$model =& ClassRegistry::getObject('Session');
$return = $model->save(compact('id', 'data', 'expires'));
@@ -90,7 +90,7 @@ public static function write($id, $data) {
* @return boolean True for successful delete, false otherwise.
* @access private
*/
public static function destroy($id) {
public function destroy($id) {
$model =& ClassRegistry::getObject('Session');
$return = $model->delete($id);
@@ -104,7 +104,7 @@ public static function destroy($id) {
* @return boolean Success
* @access private
*/
public static function gc($expires = null) {
public function gc($expires = null) {
$model =& ClassRegistry::getObject('Session');
if (!$expires) {
@@ -52,13 +52,31 @@ public static function teardownAfterClass() {
Configure::write('Session', self::$_sessionBackup);
}
/**
* setup
*
* @return void
*/
function setup() {
$this->storage = new CacheSession();
}
/**
* teardown
*
* @return void
*/
function teardown() {
unset($this->storage);
}
/**
* test open
*
* @return void
*/
function testOpen() {
$this->assertTrue(CacheSession::open());
$this->assertTrue($this->storage->open());
}
/**
@@ -67,7 +85,7 @@ function testOpen() {
* @return void
*/
function testWrite() {
CacheSession::write('abc', 'Some value');
$this->storage->write('abc', 'Some value');
$this->assertEquals('Some value', Cache::read('abc', 'session_test'), 'Value was not written.');
$this->assertFalse(Cache::read('abc', 'default'), 'Cache should only write to the given config.');
}
@@ -78,8 +96,8 @@ function testWrite() {
* @return void
*/
function testRead() {
CacheSession::write('test_one', 'Some other value');
$this->assertEquals('Some other value', CacheSession::read('test_one'), 'Incorrect value.');
$this->storage->write('test_one', 'Some other value');
$this->assertEquals('Some other value', $this->storage->read('test_one'), 'Incorrect value.');
}
/**
@@ -88,8 +106,8 @@ function testRead() {
* @return void
*/
function testDestroy() {
CacheSession::write('test_one', 'Some other value');
$this->assertTrue(CacheSession::destroy('test_one'), 'Value was not deleted.');
$this->storage->write('test_one', 'Some other value');
$this->assertTrue($this->storage->destroy('test_one'), 'Value was not deleted.');
$this->assertFalse(Cache::read('test_one', 'session_test'), 'Value stuck around.');
}
@@ -68,13 +68,31 @@ public static function teardownAfterClass() {
Configure::write('Session', self::$_sessionBackup);
}
/**
* setup
*
* @return void
*/
function setup() {
$this->storage = new DatabaseSession();
}
/**
* teardown
*
* @return void
*/
function teardown() {
unset($this->storage);
}
/**
* test opening the session
*
* @return void
*/
function testOpen() {
$this->assertTrue(DatabaseSession::open());
$this->assertTrue($this->storage->open());
}
/**
@@ -83,7 +101,7 @@ function testOpen() {
* @return void
*/
function testWrite() {
$result = DatabaseSession::write('foo', 'Some value');
$result = $this->storage->write('foo', 'Some value');
$expected = array(
'Session' => array(
'id' => 'foo',
@@ -100,13 +118,13 @@ function testWrite() {
* @return void
*/
function testRead() {
DatabaseSession::write('foo', 'Some value');
$this->storage->write('foo', 'Some value');
$result = DatabaseSession::read('foo');
$result = $this->storage->read('foo');
$expected = 'Some value';
$this->assertEquals($expected, $result);
$result = DatabaseSession::read('made up value');
$result = $this->storage->read('made up value');
$this->assertFalse($result);
}
@@ -116,10 +134,10 @@ function testRead() {
* @return void
*/
function testDestroy() {
DatabaseSession::write('foo', 'Some value');
$this->storage->write('foo', 'Some value');
$this->assertTrue(DatabaseSession::destroy('foo'), 'Destroy failed');
$this->assertFalse(DatabaseSession::read('foo'), 'Value still present.');
$this->assertTrue($this->storage->destroy('foo'), 'Destroy failed');
$this->assertFalse($this->storage->read('foo'), 'Value still present.');
}
/**
@@ -129,10 +147,10 @@ function testDestroy() {
*/
function testGc() {
Configure::write('Session.timeout', 0);
DatabaseSession::write('foo', 'Some value');
$this->storage->write('foo', 'Some value');
sleep(1);
DatabaseSession::gc();
$this->assertFalse(DatabaseSession::read('foo'));
$this->storage->gc();
$this->assertFalse($this->storage->read('foo'));
}
}

0 comments on commit 27ae7f1

Please sign in to comment.
You can’t perform that action at this time.