overall code refactoring, comment improvement.

This commit is contained in:
Taylor Otwell
2011-06-17 00:02:24 -05:00
parent 5c275db6f4
commit 3408bb8492
10 changed files with 108 additions and 150 deletions

View File

@@ -17,8 +17,7 @@ class Session {
private static $session = array();
/**
* Get the session driver. If the driver has already been instantiated, that
* instance will be returned.
* Get the session driver.
*
* @return Session\Driver
*/
@@ -39,26 +38,25 @@ class Session {
*/
public static function load()
{
// -----------------------------------------------------
// If a valid ID is present, load the session.
// -----------------------------------------------------
if ( ! is_null($id = Cookie::get('laravel_session')))
{
static::$session = static::driver()->load($id);
}
// -----------------------------------------------------
// If the session is invalid, start a new one.
// -----------------------------------------------------
if (is_null($id) or is_null(static::$session) or (time() - static::$session['last_activity']) > (Config::get('session.lifetime') * 60))
// ---------------------------------------------------------
// 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']))
{
static::$session['id'] = Str::random(40);
static::$session['data'] = array();
}
// -----------------------------------------------------
// Create a CSRF token for the session if necessary.
// -----------------------------------------------------
// ---------------------------------------------------------
// 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'))
{
static::put('csrf_token', Str::random(16));
@@ -67,6 +65,17 @@ class Session {
static::$session['last_activity'] = time();
}
/**
* Determine if a session has expired based on the last activity.
*
* @param int $last_activity
* @return bool
*/
private static function expired($last_activity)
{
return (time() - $last_activity) > (Config::get('session.lifetime') * 60);
}
/**
* Determine if the session or flash data contains an item.
*
@@ -88,20 +97,17 @@ class Session {
*/
public static function get($key, $default = null)
{
if (static::has($key))
if (array_key_exists($key, static::$session['data']))
{
if (array_key_exists($key, static::$session['data']))
{
return static::$session['data'][$key];
}
elseif (array_key_exists(':old:'.$key, static::$session['data']))
{
return static::$session['data'][':old:'.$key];
}
elseif (array_key_exists(':new:'.$key, static::$session['data']))
{
return static::$session['data'][':new:'.$key];
}
return static::$session['data'][$key];
}
elseif (array_key_exists(':old:'.$key, static::$session['data']))
{
return static::$session['data'][':old:'.$key];
}
elseif (array_key_exists(':new:'.$key, static::$session['data']))
{
return static::$session['data'][':new:'.$key];
}
return $default;
@@ -159,7 +165,15 @@ class Session {
*/
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::$session['id'] = Str::random(40);
}
@@ -170,30 +184,26 @@ class Session {
*/
public static function close()
{
// -----------------------------------------------------
// Flash the old input to the session and age the flash.
// -----------------------------------------------------
// ---------------------------------------------------------
// Flash the old input data to the session. This allows
// the Input::old method to retrieve input from the
// previous request made by the user.
// ---------------------------------------------------------
static::flash('laravel_old_input', Input::get());
static::age_flash();
// -----------------------------------------------------
// Write the session data to storage.
// -----------------------------------------------------
static::driver()->save(static::$session);
// -----------------------------------------------------
// Set the session cookie.
// -----------------------------------------------------
// ---------------------------------------------------------
// Send the session cookie the browser so we can remember
// who the session belongs to on subsequent requests.
// ---------------------------------------------------------
if ( ! headers_sent())
{
$cookie = new Cookie('laravel_session', static::$session['id']);
if ( ! Config::get('session.expire_on_close'))
{
$cookie->lifetime = Config::get('session.lifetime');
}
$cookie->lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
$cookie->path = Config::get('session.path');
$cookie->domain = Config::get('session.domain');
$cookie->secure = Config::get('session.https');
@@ -201,9 +211,10 @@ class Session {
$cookie->send();
}
// -----------------------------------------------------
// ---------------------------------------------------------
// Perform session garbage collection (2% chance).
// -----------------------------------------------------
// Session garbage collection removes all expired sessions.
// ---------------------------------------------------------
if (mt_rand(1, 100) <= 2)
{
static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
@@ -218,7 +229,7 @@ class Session {
private static function age_flash()
{
// -----------------------------------------------------
// Expire all of the old flash data.
// Remove all of the :old: items from the session.
// -----------------------------------------------------
foreach (static::$session['data'] as $key => $value)
{
@@ -229,20 +240,15 @@ class Session {
}
// -----------------------------------------------------
// Age all of the new flash data.
// 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)
{
if (strpos($key, ':new:') === 0)
{
// -----------------------------------------------------
// Create an :old: item for the :new: item.
// -----------------------------------------------------
static::put(':old:'.substr($key, 5), $value);
// -----------------------------------------------------
// Forget the :new: item.
// -----------------------------------------------------
static::forget($key);
}
}