Skip to content

Commit

Permalink
Fixing DatabaseSession handler to correctly close the session before …
Browse files Browse the repository at this point in the history
…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 a14abd6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
12 changes: 0 additions & 12 deletions lib/Cake/Model/ConnectionManager.php
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
}
48 changes: 34 additions & 14 deletions lib/Cake/Model/Datasource/Session/DatabaseSession.php
Expand Up @@ -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.
Expand All @@ -43,7 +57,8 @@ public function __construct() {
'alias' => 'Session',
);
}
ClassRegistry::init($settings);
$this->_model = ClassRegistry::init($settings);
$this->_timeout = Configure::read('Session.timeout') * 60;
}

/**
Expand Down Expand Up @@ -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'];
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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.