Browse files

Sessions now start themselves the first time someone tries to read or…

… write to the session to avoid race conditions during start up. Also RouteCollection is now loaded as part of the service creation, instead of manually in kernel->handle() which means it works in exception handlers now
  • Loading branch information...
1 parent 1b855d9 commit 73d07d259bd1e4805b6caf3e348ffc36f25831bb @rikh42 committed Mar 26, 2013
Showing with 32 additions and 22 deletions.
  1. +3 −6 core/Kernel.php
  2. +5 −0 http/SessionStorage.php
  3. +24 −16 http/SessionStorageDb.php
View
9 core/Kernel.php
@@ -169,13 +169,13 @@ protected function initServices()
// Add some services that are part of the system
$this->addService('kernel', $this);
$this->addService('config', 'snb\config\ConfigSettings')->setArguments(array('::service::kernel'))->addCall('load', array($this->getConfigName()));
- $this->addService('routes', 'snb\routing\RouteCollection');
+ $this->addService('routes', 'snb\routing\RouteCollection')->addCall('load');;
$this->addService('event-dispatcher', new EventDispatcher);
$this->addService('logger', $this->logger);
$this->addService('view', 'snb\view\TwigView');
$this->addServiceAlias('template.engine', 'view');
$this->addService('database', 'snb\core\Database')->addCall('init', array());
- $this->addService('session', 'snb\http\SessionStorage')->addCall('start');
+ $this->addService('session', 'snb\http\SessionStorage');
$this->addService('auth', 'snb\security\Auth')->setArguments(array('::service::auth.token', '::service::auth.context', '::service::event-dispatcher'));
$this->addService('auth.token', 'snb\security\SecurityToken');
$this->addService('auth.context', 'snb\security\SecurityContext');
@@ -501,12 +501,9 @@ public function handle(Request $request)
return $this->postProcessResponse($requestEvent->getResponse());
}
- // load routes and find the route
+ // load routes and find the route that matches the request
/** @var $routes \snb\routing\RouteCollection */
$routes = $this->container->get('routes');
- $routes->load();
-
- // Try and find a route that matches the request
$route = $routes->findMatchingRoute($request);
// If we have no valid route, then try something else.
View
5 http/SessionStorage.php
@@ -107,6 +107,7 @@ public function closeAndWrite()
//==============================
public function get($key, $default=null)
{
+ $this->start();
if (array_key_exists($key, $_SESSION)) {
return $_SESSION[$key];
}
@@ -120,6 +121,7 @@ public function get($key, $default=null)
//==============================
public function set($key, $value)
{
+ $this->start();
$_SESSION[$key] = $value;
}
@@ -129,6 +131,7 @@ public function set($key, $value)
//==============================
public function remove($key)
{
+ $this->start();
if (array_key_exists($key, $_SESSION)) {
unset($_SESSION[$key]);
}
@@ -141,6 +144,7 @@ public function remove($key)
*/
public function setFlash($name, $msg)
{
+ $this->start();
$_SESSION[self::FLASH_KEY][$name] = $msg;
}
@@ -151,6 +155,7 @@ public function setFlash($name, $msg)
*/
public function removeFlash($name)
{
+ $this->start();
unset($_SESSION[self::FLASH_KEY][$name]);
}
View
40 http/SessionStorageDb.php
@@ -106,7 +106,6 @@ public function read($sessionID)
// nope, no data, so start a new session and return an empty data string
$this->startNewSession($sessionID);
-
return '';
}
@@ -131,23 +130,32 @@ public function write($sessionID, $data)
return true;
}
- // prepare the data for the query
- $param = array(
- 'id' => $sessionID,
- 'data' => base64_encode($data),
- 'time' => time()
- );
-
- // duplicate some of the data for PDO
- $param['udata'] = $param['data'];
- $param['utime'] = $param['time'];
+ try
+ {
+ // prepare the data for the query
+ $param = array(
+ 'id' => $sessionID,
+ 'data' => base64_encode($data),
+ 'time' => time()
+ );
+
+ // duplicate some of the data for PDO
+ $param['udata'] = $param['data'];
+ $param['utime'] = $param['time'];
+
+ // Try and insert or update the data
+ $sql = "INSERT INTO sessions (sSessionId, sData, iLastTouched) VALUES (:id, :data, :time) "
+ . "ON DUPLICATE KEY UPDATE sData=:udata, iLastTouched=:utime ";
+
+ // do it.
+ $this->database->query($sql, $param);
+ } catch (\Exception $e)
+ {
+ // Something went wrong writing the session to the database.
+ // There is nothing we can really do about this..
+ }
- // Try and insert or update the data
- $sql = "INSERT INTO sessions (sSessionId, sData, iLastTouched) VALUES (:id, :data, :time) "
- . "ON DUPLICATE KEY UPDATE sData=:udata, iLastTouched=:utime ";
- // do it.
- $this->database->query($sql, $param);
return true;
}

0 comments on commit 73d07d2

Please sign in to comment.