refactored session and added unit tests for manager and driver.
This commit is contained in:
4
laravel/cache/drivers/apc.php
vendored
4
laravel/cache/drivers/apc.php
vendored
@@ -9,7 +9,7 @@ class APC extends Driver {
|
||||
*
|
||||
* @var Proxy
|
||||
*/
|
||||
private $apc;
|
||||
private $proxy;
|
||||
|
||||
/**
|
||||
* The cache key from the cache configuration file.
|
||||
@@ -25,7 +25,7 @@ class APC extends Driver {
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Proxy $apc, $key)
|
||||
public function __construct(Proxy $proxy, $key)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->proxy = $proxy;
|
||||
|
||||
@@ -74,11 +74,15 @@ else
|
||||
$response->content = $response->render();
|
||||
|
||||
// --------------------------------------------------------------
|
||||
// Close the session.
|
||||
// Close the session and write the session cookie.
|
||||
// --------------------------------------------------------------
|
||||
if ($config->get('session.driver') !== '')
|
||||
{
|
||||
$container->resolve('laravel.session')->close($container->resolve('laravel.input'));
|
||||
$session = $container->resolve('laravel.session');
|
||||
|
||||
$session->close($container->resolve('laravel.input'), time());
|
||||
|
||||
$session->cookie($container->resolve('laravel.cookie'));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------
|
||||
|
||||
@@ -5,6 +5,10 @@ class APC extends Driver {
|
||||
/**
|
||||
* The APC cache driver instance.
|
||||
*
|
||||
* This session driver relies on the APC cache driver to provide an interface for
|
||||
* working with an APC equipped server. The cache driver will provide all of the
|
||||
* functionality for retrieving and storing items in APC.
|
||||
*
|
||||
* @var Cache\Drivers\APC
|
||||
*/
|
||||
protected $apc;
|
||||
@@ -23,6 +27,10 @@ class APC extends Driver {
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* This method is responsible for retrieving the session from persistant storage. If the
|
||||
* session does not exist in storage, nothing should be returned from the method, in which
|
||||
* case a new session will be created by the base driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
@@ -34,21 +42,23 @@ class APC extends Driver {
|
||||
/**
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
protected function save()
|
||||
protected function save($session)
|
||||
{
|
||||
$this->apc->put($this->session['id'], $this->session, $this->config->get('session.lifetime'));
|
||||
$this->apc->put($session['id'], $session, $this->config->get('session.lifetime'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
protected function delete()
|
||||
protected function delete($id)
|
||||
{
|
||||
$this->apc->forget($this->session['id']);
|
||||
$this->apc->forget($id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,6 +14,10 @@ class Cookie extends Driver {
|
||||
/**
|
||||
* The crypter instance.
|
||||
*
|
||||
* All session cookies have an encrypted payload. Since the session contains sensitive
|
||||
* data that cannot be compromised, it is important that the payload be encrypted using
|
||||
* the strong encryption provided by the Crypter class.
|
||||
*
|
||||
* @var Crypter
|
||||
*/
|
||||
private $crypter;
|
||||
@@ -34,6 +38,10 @@ class Cookie extends Driver {
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* This method is responsible for retrieving the session from persistant storage. If the
|
||||
* session does not exist in storage, nothing should be returned from the method, in which
|
||||
* case a new session will be created by the base driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
@@ -48,9 +56,10 @@ class Cookie extends Driver {
|
||||
/**
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
protected function save()
|
||||
protected function save($session)
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
@@ -58,7 +67,7 @@ class Cookie extends Driver {
|
||||
|
||||
extract($config);
|
||||
|
||||
$payload = $this->crypter->encrypt(serialize($this->session));
|
||||
$payload = $this->crypter->encrypt(serialize($session));
|
||||
|
||||
$this->cookie->put('session_payload', $payload, $lifetime, $path, $domain);
|
||||
}
|
||||
@@ -67,9 +76,10 @@ class Cookie extends Driver {
|
||||
/**
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
protected function delete()
|
||||
protected function delete($id)
|
||||
{
|
||||
$this->cookie->forget('session_payload');
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ class Database extends Driver implements Sweeper {
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* This method is responsible for retrieving the session from persistant storage. If the
|
||||
* session does not exist in storage, nothing should be returned from the method, in which
|
||||
* case a new session will be created by the base driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
@@ -45,27 +49,29 @@ class Database extends Driver implements Sweeper {
|
||||
/**
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
protected function save()
|
||||
protected function save($session)
|
||||
{
|
||||
$this->delete($this->session['id']);
|
||||
$this->delete($session['id']);
|
||||
|
||||
$this->table()->insert(array(
|
||||
'id' => $this->session['id'],
|
||||
'last_activity' => $this->session['last_activity'],
|
||||
'data' => serialize($this->session['data'])
|
||||
'id' => $session['id'],
|
||||
'last_activity' => $session['last_activity'],
|
||||
'data' => serialize($session['data'])
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
protected function delete()
|
||||
protected function delete($id)
|
||||
{
|
||||
$this->table()->delete($this->session['id']);
|
||||
$this->table()->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,7 +42,7 @@ abstract class Driver {
|
||||
// string ID to uniquely identify it among the application's current users.
|
||||
if (is_null($this->session) or $this->expired())
|
||||
{
|
||||
$this->session = array('id' => Str::random(40), 'data' => array());
|
||||
$this->session = array('id' => Str::random(40), 'last_activity' => time(), 'data' => array());
|
||||
}
|
||||
|
||||
// If a CSRF token is not present in the session, we will generate one. These tokens
|
||||
@@ -69,6 +69,10 @@ abstract class Driver {
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* This method is responsible for retrieving the session from persistant storage. If the
|
||||
* session does not exist in storage, nothing should be returned from the method, in which
|
||||
* case a new session will be created by the base driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
@@ -77,16 +81,18 @@ abstract class Driver {
|
||||
/**
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function delete();
|
||||
abstract protected function delete($id);
|
||||
|
||||
/**
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function save();
|
||||
abstract protected function save($session);
|
||||
|
||||
/**
|
||||
* Determine if the session or flash data contains an item.
|
||||
@@ -205,7 +211,7 @@ abstract class Driver {
|
||||
*/
|
||||
public final function regenerate()
|
||||
{
|
||||
$this->delete();
|
||||
$this->delete($this->session['id']);
|
||||
|
||||
$this->session['id'] = Str::random(40);
|
||||
}
|
||||
@@ -227,20 +233,19 @@ abstract class Driver {
|
||||
* Close the session and store the session payload in persistant storage.
|
||||
*
|
||||
* @param Laravel\Input $input
|
||||
* @param int $time
|
||||
* @return void
|
||||
*/
|
||||
public function close(Input $input)
|
||||
public final function close(Input $input, $time)
|
||||
{
|
||||
// The input for the current request will be flashed to the session for
|
||||
// convenient access through the "old" method of the input class. This
|
||||
// allows the easy repopulation of forms.
|
||||
$this->flash('laravel_old_input', $input->get())->age();
|
||||
|
||||
$this->session['last_activity'] = time();
|
||||
$this->session['last_activity'] = $time;
|
||||
|
||||
$this->save();
|
||||
|
||||
$this->cookie($input->cookies);
|
||||
$this->save($this->session);
|
||||
|
||||
// Some session drivers implement the "Sweeper" interface, which specifies
|
||||
// that the driver needs to manually clean up its expired sessions. If the
|
||||
@@ -248,7 +253,28 @@ abstract class Driver {
|
||||
// sweep method on the driver.
|
||||
if ($this instanceof Sweeper and mt_rand(1, 100) <= 2)
|
||||
{
|
||||
$this->sweep(time() - ($this->config['lifetime'] * 60));
|
||||
$this->sweep($time - ($this->config->get('session.lifetime') * 60));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the session cookie.
|
||||
*
|
||||
* @param Laravel\Cookie $cookie
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
public final function cookie(Cookie $cookies)
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
$config = $this->config->get('session');
|
||||
|
||||
extract($config);
|
||||
|
||||
$minutes = ($expire_on_close) ? 0 : $lifetime;
|
||||
|
||||
$cookies->put('laravel_session', $this->session['id'], $minutes, $path, $domain);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,27 +295,6 @@ abstract class Driver {
|
||||
$this->readdress(':new:', ':old:', array_keys($this->session['data']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the session cookie.
|
||||
*
|
||||
* @param Laravel\Cookie $cookie
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
private function cookie(Cookie $cookies)
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
$config = $this->config->get('session');
|
||||
|
||||
extract($config);
|
||||
|
||||
$minutes = ($expire_on_close) ? 0 : $lifetime;
|
||||
|
||||
$cookies->put('laravel_session', $this->session['id'], $minutes, $path, $domain);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method for retrieving items from the session.
|
||||
*/
|
||||
|
||||
@@ -32,6 +32,10 @@ class File extends Driver implements Sweeper {
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* This method is responsible for retrieving the session from persistant storage. If the
|
||||
* session does not exist in storage, nothing should be returned from the method, in which
|
||||
* case a new session will be created by the base driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
@@ -43,21 +47,23 @@ class File extends Driver implements Sweeper {
|
||||
/**
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
protected function save()
|
||||
protected function save($session)
|
||||
{
|
||||
$this->file->put($this->path.$this->session['id'], serialize($this->session), LOCK_EX);
|
||||
$this->file->put($this->path.$session['id'], serialize($session), LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
protected function delete()
|
||||
protected function delete($id)
|
||||
{
|
||||
$this->file->delete($this->path.$this->session['id']);
|
||||
$this->file->delete($this->path.$id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,10 @@ class Memcached extends Driver {
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* This method is responsible for retrieving the session from persistant storage. If the
|
||||
* session does not exist in storage, nothing should be returned from the method, in which
|
||||
* case a new session will be created by the base driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
@@ -34,21 +38,23 @@ class Memcached extends Driver {
|
||||
/**
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
protected function save()
|
||||
protected function save($session)
|
||||
{
|
||||
$this->memcached->put($this->session['id'], $this->session, $this->config->get('session.lifetime'));
|
||||
$this->memcached->put($session['id'], $session, $this->config->get('session.lifetime'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
protected function delete()
|
||||
protected function delete($id)
|
||||
{
|
||||
$this->memcached->forget($this->session['id']);
|
||||
$this->memcached->forget($id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -96,7 +96,7 @@ class Str {
|
||||
|
||||
$pool = ($type == 'alpha_num') ? '0123456789'.$alpha : $alpha;
|
||||
|
||||
return implode('', array_map(function() use ($pool) { return $pool[mt_rand(0, strlen($pool) - 1)]; }, range(0, $length)));
|
||||
return implode('', array_map(function() use ($pool) { return $pool[mt_rand(0, strlen($pool) - 1)]; }, range(0, $length - 1)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user