refactoring and bug fixes.

This commit is contained in:
Taylor Otwell
2011-09-29 21:22:48 -05:00
parent 2eeb636198
commit 14186a00e0
22 changed files with 192 additions and 132 deletions

View File

@@ -42,9 +42,10 @@ class APC implements Driver {
*
* @param array $session
* @param array $config
* @param bool $exists
* @return void
*/
public function save($session, $config)
public function save($session, $config, $exists)
{
$this->apc->put($session['id'], $session, $config['lifetime']);
}

View File

@@ -56,9 +56,10 @@ class Cookie implements Driver {
*
* @param array $session
* @param array $config
* @param bool $exists
* @return void
*/
public function save($session, $config)
public function save($session, $config, $exists)
{
extract($config);

View File

@@ -50,17 +50,26 @@ class Database implements Driver, Sweeper {
*
* @param array $session
* @param array $config
* @param bool $exists
* @return void
*/
public function save($session, $config)
public function save($session, $config, $exists)
{
$this->delete($session['id']);
$this->table()->insert(array(
'id' => $session['id'],
'last_activity' => $session['last_activity'],
'data' => serialize($session['data'])
));
if ($exists)
{
$this->table()->where('id', '=', $session['id'])->update(array(
'last_activity' => $session['last_activity'],
'data' => serialize($session['data']),
));
}
else
{
$this->table()->insert(array(
'id' => $session['id'],
'last_activity' => $session['last_activity'],
'data' => serialize($session['data'])
));
}
}
/**

View File

@@ -17,9 +17,10 @@ interface Driver {
*
* @param array $session
* @param array $config
* @param bool $exists
* @return void
*/
public function save($session, $config);
public function save($session, $config, $exists);
/**
* Delete a session from storage by a given ID.

View File

@@ -40,9 +40,10 @@ class File implements Driver, Sweeper {
*
* @param array $session
* @param array $config
* @param bool $exists
* @return void
*/
public function save($session, $config)
public function save($session, $config, $exists)
{
F::put($this->path.$session['id'], serialize($session), LOCK_EX);
}

View File

@@ -38,9 +38,10 @@ class Memcached implements Driver {
*
* @param array $session
* @param array $config
* @param bool $exists
* @return void
*/
public function save($session, $config)
public function save($session, $config, $exists)
{
$this->memcached->put($session['id'], $session, $config['lifetime']);
}

View File

@@ -28,6 +28,13 @@ class Manager {
*/
private $payload;
/**
* Indicates if the session exists in persistent storage.
*
* @var bool
*/
private $exists = true;
/**
* Create a new session manager instance.
*
@@ -56,6 +63,8 @@ class Manager {
// string ID to uniquely identify it among the application's current users.
if (is_null($session) or $this->expired($session, $config))
{
$this->exists = false;
$session = array('id' => Str::random(40), 'data' => array());
}
@@ -96,12 +105,19 @@ class Manager {
*/
public function close(Payload $payload, $config, $flash = array())
{
foreach ($flash as $key => $value)
// If the session ID has been regenerated, we will need to inform the session driver
// that the session will need to be persisted to the data store as a new session.
if ($payload->regenerated)
{
$this->driver->flash($key, $value);
$this->exists = false;
}
$this->driver->save($payload->age(), $config);
foreach ($flash as $key => $value)
{
$payload->flash($key, $value);
}
$this->driver->save($payload->age(), $config, $this->exists);
$this->transporter->put($payload->session['id'], $config);

View File

@@ -12,6 +12,13 @@ class Payload {
*/
public $session = array();
/**
* Indicates if the session ID has been regenerated.
*
* @var bool
*/
public $regenerated = false;
/**
* Create a new session container instance.
*
@@ -144,6 +151,8 @@ class Payload {
public function regenerate()
{
$this->session['id'] = Str::random(40);
$this->regenerated = true;
}
/**