Merge pull request #146 from majna/2.0-database-session

Refactored DatabaseSession to use only 'Session.handler.model' config
This commit is contained in:
José Lorenzo Rodríguez 2011-07-11 16:28:25 -07:00
commit 273125d2f5
3 changed files with 17 additions and 23 deletions

View file

@ -31,23 +31,18 @@ class DatabaseSession implements CakeSessionHandlerInterface {
*/ */
public function __construct() { public function __construct() {
$modelName = Configure::read('Session.handler.model'); $modelName = Configure::read('Session.handler.model');
$database = Configure::read('Session.handler.database');
$table = Configure::read('Session.handler.table');
if (empty($database)) { if (empty($modelName)) {
$database = 'default';
}
$settings = array( $settings = array(
'class' =>'Session', 'class' =>'Session',
'alias' => 'Session', 'alias' => 'Session',
'table' => 'cake_sessions', 'table' => 'cake_sessions',
'ds' => $database
); );
if (!empty($modelName)) { } else {
$settings['class'] = $modelName; $settings = array(
} 'class' =>$modelName,
if (!empty($table)) { 'alias' => 'Session',
$settings['table'] = $table; );
} }
ClassRegistry::init($settings); ClassRegistry::init($settings);
} }
@ -71,7 +66,7 @@ class DatabaseSession implements CakeSessionHandlerInterface {
public function close() { public function close() {
$probability = mt_rand(1, 150); $probability = mt_rand(1, 150);
if ($probability <= 3) { if ($probability <= 3) {
DatabaseSession::gc(); $this->gc();
} }
return true; return true;
} }

View file

@ -23,8 +23,8 @@ App::uses('DatabaseSession', 'Model/Datasource/Session');
class_exists('CakeSession'); class_exists('CakeSession');
class SessionTestModel extends Model { class SessionTestModel extends Model {
var $name = 'SessionTestModel'; public $name = 'SessionTestModel';
var $useTable = 'sessions'; public $useTable = 'sessions';
} }
/** /**
@ -52,8 +52,6 @@ class DatabaseSessionTest extends CakeTestCase {
self::$_sessionBackup = Configure::read('Session'); self::$_sessionBackup = Configure::read('Session');
Configure::write('Session.handler', array( Configure::write('Session.handler', array(
'model' => 'SessionTestModel', 'model' => 'SessionTestModel',
'database' => 'test',
'table' => 'sessions'
)); ));
Configure::write('Session.timeout', 100); Configure::write('Session.timeout', 100);
} }
@ -99,6 +97,7 @@ class DatabaseSessionTest extends CakeTestCase {
$this->assertInstanceOf('SessionTestModel', $session); $this->assertInstanceOf('SessionTestModel', $session);
$this->assertEquals('Session', $session->alias); $this->assertEquals('Session', $session->alias);
$this->assertEquals('test', $session->useDbConfig); $this->assertEquals('test', $session->useDbConfig);
$this->assertEquals('sessions', $session->useTable);
} }
/** /**

View file

@ -46,7 +46,7 @@ class SessionFixture extends CakeTestFixture {
* @access public * @access public
*/ */
public $fields = array( public $fields = array(
'id' => array('type' => 'string', 'length' => 255, 'key' => 'primary'), 'id' => array('type' => 'string', 'length' => 128, 'key' => 'primary'),
'data' => array('type' => 'text','null' => true), 'data' => array('type' => 'text','null' => true),
'expires' => array('type' => 'integer', 'length' => 11, 'null' => true) 'expires' => array('type' => 'integer', 'length' => 11, 'null' => true)
); );