Permalink
Browse files

More changes to make database sessions work cleaner

  • Loading branch information...
1 parent d451419 commit c9c16282466a61a86422dee5ebf5cfc5a2492d08 @rikh42 committed Mar 4, 2013
Showing with 47 additions and 19 deletions.
  1. +17 −0 http/SessionStorage.php
  2. +29 −19 http/SessionStorageDb.php
  3. +1 −0 http/SessionStorageInterface.php
@@ -18,6 +18,7 @@
class SessionStorage extends ContainerAware implements SessionStorageInterface
{
protected $started;
+ protected $writeEnabled;
protected $flashMessages;
const FLASH_KEY = '_flashes';
@@ -30,6 +31,7 @@ public function __construct()
{
// default values
$this->started = false;
+ $this->writeEnabled = true;
}
//==============================
@@ -69,17 +71,32 @@ 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.
*/
public function closeAndWrite()
{
+ // Allow writing, briefly
+ $old = $this->writeEnabled;
+ $this->setWriteEnable(true);
+
// Stop if we were started
if ($this->started) {
session_write_close();
}
// make sure we are stopped
$this->started = false;
+ $this->setWriteEnable($old);
}
@@ -18,9 +18,10 @@ class SessionStorageDb extends SessionStorage
{
protected $database;
- //==============================
- // __construct
- //==============================
+
+ /**
+ * @param \snb\core\DatabaseInterface $database
+ */
public function __construct(DatabaseInterface $database)
{
parent::__construct();
@@ -29,10 +30,11 @@ public function __construct(DatabaseInterface $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()
{
// If we have already started the session, don't do it again
@@ -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)
{
return true;
@@ -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()
{
return true;
@@ -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)
{
// try and find the session data in the database
@@ -121,6 +126,11 @@ public function write($sessionID, $data)
return true;
}
+ // If writing has been disabled, don't write to the database
+ if (!$this->writeEnabled) {
+ return true;
+ }
+
// prepare the data for the query
$param = array(
'id' => $sessionID,
@@ -11,6 +11,7 @@
interface SessionStorageInterface
{
public function start();
+ public function setWriteEnable($writeEnable);
public function closeAndWrite();
public function get($key, $default=null);
public function set($key, $value);

0 comments on commit c9c1628

Please sign in to comment.