Skip to content
Permalink
Browse files

Fixing DatabaseSession handler to correctly close the session before …

…the model object is destroyed by php.

This allows to remove an old hack in ConnectionManager
  • Loading branch information...
lorenzo committed Oct 7, 2011
1 parent 5eb4c5c commit a14abd6281bf71c1319fe3f3e95e0d7b0a16e878
Showing with 34 additions and 26 deletions.
  1. +0 −12 lib/Cake/Model/ConnectionManager.php
  2. +34 −14 lib/Cake/Model/Datasource/Session/DatabaseSession.php
@@ -66,7 +66,6 @@ protected static function _init() {
if (class_exists('DATABASE_CONFIG')) {
self::$config = new DATABASE_CONFIG();
}
register_shutdown_function('ConnectionManager::shutdown');
self::$_init = true;
}
@@ -257,15 +256,4 @@ protected static function _connectionData($config) {
}
return compact('package', 'classname', 'plugin');
}
/**
* Destructor.
*
* @return void
*/
public static function shutdown() {
if (Configure::read('Session.defaults') == 'database' && function_exists('session_write_close')) {
session_write_close();
}
}
}
@@ -23,6 +23,20 @@
*/
class DatabaseSession implements CakeSessionHandlerInterface {
/**
* Reference to the model handling the session data
*
* @var Model
*/
protected $_model;
/**
* Number of seconds to mark the session as expired
*
* @var int
*/
protected $_timeout;
/**
* Constructor. Looks at Session configuration information and
* sets up the session model.
@@ -43,7 +57,8 @@ public function __construct() {
'alias' => 'Session',
);
}
ClassRegistry::init($settings);
$this->_model = ClassRegistry::init($settings);
$this->_timeout = Configure::read('Session.timeout') * 60;
}
/**
@@ -75,17 +90,15 @@ public function close() {
* @return mixed The value of the key or false if it does not exist
*/
public function read($id) {
$model = ClassRegistry::getObject('Session');
$row = $model->find('first', array(
'conditions' => array($model->primaryKey => $id)
$row = $this->_model->find('first', array(
'conditions' => array($this->_model->primaryKey => $id)
));
if (empty($row[$model->alias]['data'])) {
if (empty($row[$this->_model->alias]['data'])) {
return false;
}
return $row[$model->alias]['data'];
return $row[$this->_model->alias]['data'];
}
/**
@@ -99,11 +112,10 @@ public function write($id, $data) {
if (!$id) {
return false;
}
$expires = time() + (Configure::read('Session.timeout') * 60);
$Session = ClassRegistry::getObject('Session');
$expires = time() + $this->_timeout;
$record = compact('id', 'data', 'expires');
$record[$Session->primaryKey] = $id;
return $Session->save($record);
$record[$this->_model->primaryKey] = $id;
return $this->_model->save($record);
}
/**
@@ -113,7 +125,7 @@ public function write($id, $data) {
* @return boolean True for successful delete, false otherwise.
*/
public function destroy($id) {
return ClassRegistry::getObject('Session')->delete($id);
return $this->_model->delete($id);
}
/**
@@ -126,7 +138,15 @@ public function gc($expires = null) {
if (!$expires) {
$expires = time();
}
$model = ClassRegistry::getObject('Session');
return $model->deleteAll(array($model->alias . ".expires <" => $expires), false, false);
return $this->_model->deleteAll(array($model->alias . ".expires <" => $expires), false, false);
}
/**
* Closes the session before the objects handling it become unavailable
*
* @return void
*/
public function __destruct() {
session_write_close();
}
}

0 comments on commit a14abd6

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