Skip to content

Commit

Permalink
More changes to make database sessions work cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
rikh42 committed Mar 4, 2013
1 parent d451419 commit c9c1628
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
17 changes: 17 additions & 0 deletions http/SessionStorage.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class SessionStorage extends ContainerAware implements SessionStorageInterface class SessionStorage extends ContainerAware implements SessionStorageInterface
{ {
protected $started; protected $started;
protected $writeEnabled;
protected $flashMessages; protected $flashMessages;
const FLASH_KEY = '_flashes'; const FLASH_KEY = '_flashes';


Expand All @@ -30,6 +31,7 @@ public function __construct()
{ {
// default values // default values
$this->started = false; $this->started = false;
$this->writeEnabled = true;
} }


//============================== //==============================
Expand Down Expand Up @@ -68,18 +70,33 @@ public function start()
} }




/**
* Enable or disable writing to the session, where supported by the driver (SessionStorageDB only at present)
* @param $writeEnable
*/
public function setWriteEnable($writeEnable)
{
$this->writeEnabled = $writeEnable;
}


/** /**
* Close the session before the end of the script. * Close the session before the end of the script.
*/ */
public function closeAndWrite() public function closeAndWrite()
{ {
// Allow writing, briefly
$old = $this->writeEnabled;
$this->setWriteEnable(true);

// Stop if we were started // Stop if we were started
if ($this->started) { if ($this->started) {
session_write_close(); session_write_close();
} }


// make sure we are stopped // make sure we are stopped
$this->started = false; $this->started = false;
$this->setWriteEnable($old);
} }




Expand Down
48 changes: 29 additions & 19 deletions http/SessionStorageDb.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class SessionStorageDb extends SessionStorage
{ {
protected $database; protected $database;


//==============================
// __construct /**
//============================== * @param \snb\core\DatabaseInterface $database
*/
public function __construct(DatabaseInterface $database) public function __construct(DatabaseInterface $database)
{ {
parent::__construct(); parent::__construct();
Expand All @@ -29,10 +30,11 @@ public function __construct(DatabaseInterface $database)
$this->database = $database; $this->database = $database;
} }


//==============================
// start /**
// Starts a session * Starts the session. This sets up all the Session handler functions to
//============================== * use our overrides, allowing us to write to a database instead
*/
public function start() public function start()
{ {
// If we have already started the session, don't do it again // If we have already started the session, don't do it again
Expand All @@ -57,10 +59,12 @@ public function start()






//============================== /**
// open * Called by PHP when the session is first opened
// Called by PHP when the session is first opened * @param $path
//============================== * @param $name
* @return bool
*/
public function open($path, $name) public function open($path, $name)
{ {
return true; return true;
Expand All @@ -69,10 +73,10 @@ public function open($path, $name)






//============================== /**
// close * Called by PHP when the session finally closed
// Called by PHP when the session finally closed * @return bool
//============================== */
public function close() public function close()
{ {
return true; return true;
Expand All @@ -81,10 +85,11 @@ public function close()






//============================== /**
// read * called by PHP to read the session data. All the data is read in one go
// called by PHP to read the session data. All the data is read in one go * @param $sessionID
//============================== * @return string
*/
public function read($sessionID) public function read($sessionID)
{ {
// try and find the session data in the database // try and find the session data in the database
Expand Down Expand Up @@ -121,6 +126,11 @@ public function write($sessionID, $data)
return true; return true;
} }


// If writing has been disabled, don't write to the database
if (!$this->writeEnabled) {
return true;
}

// prepare the data for the query // prepare the data for the query
$param = array( $param = array(
'id' => $sessionID, 'id' => $sessionID,
Expand Down
1 change: 1 addition & 0 deletions http/SessionStorageInterface.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
interface SessionStorageInterface interface SessionStorageInterface
{ {
public function start(); public function start();
public function setWriteEnable($writeEnable);
public function closeAndWrite(); public function closeAndWrite();
public function get($key, $default=null); public function get($key, $default=null);
public function set($key, $value); public function set($key, $value);
Expand Down

0 comments on commit c9c1628

Please sign in to comment.