Refactoring Session class. Trimmed comment bloat. Added support for closures on Session::get default value.
This commit is contained in:
parent
881d7f78a1
commit
607b23b742
|
@ -43,20 +43,13 @@ public static function load()
|
||||||
static::$session = static::driver()->load($id);
|
static::$session = static::driver()->load($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// If the session is invalid or expired, start a new one.
|
// If the session is invalid or expired, start a new one.
|
||||||
// ---------------------------------------------------------
|
|
||||||
if (is_null($id) or is_null(static::$session) or static::expired(static::$session['last_activity']))
|
if (is_null($id) or is_null(static::$session) or static::expired(static::$session['last_activity']))
|
||||||
{
|
{
|
||||||
static::$session['id'] = Str::random(40);
|
static::$session['id'] = Str::random(40);
|
||||||
static::$session['data'] = array();
|
static::$session['data'] = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// Create a CSRF token for the session if necessary. This
|
|
||||||
// token is used by the Form class and filters to protect
|
|
||||||
// against cross-site request forgeries.
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
if ( ! static::has('csrf_token'))
|
if ( ! static::has('csrf_token'))
|
||||||
{
|
{
|
||||||
static::put('csrf_token', Str::random(16));
|
static::put('csrf_token', Str::random(16));
|
||||||
|
@ -110,7 +103,7 @@ public static function get($key, $default = null)
|
||||||
return static::$session['data'][':new:'.$key];
|
return static::$session['data'][':new:'.$key];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $default;
|
return is_callable($default) ? call_user_func($default) : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,13 +158,6 @@ public static function flush()
|
||||||
*/
|
*/
|
||||||
public static function regenerate()
|
public static function regenerate()
|
||||||
{
|
{
|
||||||
// ---------------------------------------------------------
|
|
||||||
// When regenerating the session ID, we go ahead and delete
|
|
||||||
// the session data from storage. Then, we assign a new ID.
|
|
||||||
//
|
|
||||||
// The session will be re-written to storage at the end
|
|
||||||
// of the request to the application.
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
static::driver()->delete(static::$session['id']);
|
static::driver()->delete(static::$session['id']);
|
||||||
|
|
||||||
static::$session['id'] = Str::random(40);
|
static::$session['id'] = Str::random(40);
|
||||||
|
@ -184,21 +170,14 @@ public static function regenerate()
|
||||||
*/
|
*/
|
||||||
public static function close()
|
public static function close()
|
||||||
{
|
{
|
||||||
// ---------------------------------------------------------
|
// Flash the old input data to the session. This allows the Input::old method to
|
||||||
// Flash the old input data to the session. This allows
|
// retrieve the input from the previous request made by the user.
|
||||||
// the Input::old method to retrieve input from the
|
|
||||||
// previous request made by the user.
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
static::flash('laravel_old_input', Input::get());
|
static::flash('laravel_old_input', Input::get());
|
||||||
|
|
||||||
static::age_flash();
|
static::age_flash();
|
||||||
|
|
||||||
static::driver()->save(static::$session);
|
static::driver()->save(static::$session);
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
// Send the session cookie the browser so we can remember
|
|
||||||
// who the session belongs to on subsequent requests.
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
if ( ! headers_sent())
|
if ( ! headers_sent())
|
||||||
{
|
{
|
||||||
$minutes = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
|
$minutes = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
|
||||||
|
@ -206,10 +185,7 @@ public static function close()
|
||||||
Cookie::put('laravel_session', static::$session['id'], $minutes, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https'));
|
Cookie::put('laravel_session', static::$session['id'], $minutes, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------
|
// 2% chance of performing session garbage collection...
|
||||||
// Perform session garbage collection (2% chance).
|
|
||||||
// Session garbage collection removes all expired sessions.
|
|
||||||
// ---------------------------------------------------------
|
|
||||||
if (mt_rand(1, 100) <= 2)
|
if (mt_rand(1, 100) <= 2)
|
||||||
{
|
{
|
||||||
static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
|
static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
|
||||||
|
@ -223,9 +199,6 @@ public static function close()
|
||||||
*/
|
*/
|
||||||
private static function age_flash()
|
private static function age_flash()
|
||||||
{
|
{
|
||||||
// -----------------------------------------------------
|
|
||||||
// Remove all of the :old: items from the session.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
foreach (static::$session['data'] as $key => $value)
|
foreach (static::$session['data'] as $key => $value)
|
||||||
{
|
{
|
||||||
if (strpos($key, ':old:') === 0)
|
if (strpos($key, ':old:') === 0)
|
||||||
|
@ -234,10 +207,6 @@ private static function age_flash()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Copy all of the :new: items to :old: items and then
|
|
||||||
// remove the :new: items from the session.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
foreach (static::$session['data'] as $key => $value)
|
foreach (static::$session['data'] as $key => $value)
|
||||||
{
|
{
|
||||||
if (strpos($key, ':new:') === 0)
|
if (strpos($key, ':new:') === 0)
|
||||||
|
|
Loading…
Reference in New Issue