From 607b23b74213a63f6cc363be39794faee78c5160 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 6 Jul 2011 15:07:18 -0700 Subject: [PATCH] Refactoring Session class. Trimmed comment bloat. Added support for closures on Session::get default value. --- system/session.php | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/system/session.php b/system/session.php index 75c355e3..43931358 100644 --- a/system/session.php +++ b/system/session.php @@ -43,20 +43,13 @@ class Session { static::$session = static::driver()->load($id); } - // --------------------------------------------------------- // 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. 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)); @@ -110,7 +103,7 @@ class Session { return static::$session['data'][':new:'.$key]; } - return $default; + return is_callable($default) ? call_user_func($default) : $default; } /** @@ -165,13 +158,6 @@ 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); @@ -184,21 +170,14 @@ class Session { */ public static function close() { - // --------------------------------------------------------- - // 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. - // --------------------------------------------------------- + // Flash the old input data to the session. This allows the Input::old method to + // retrieve the input from the previous request made by the user. static::flash('laravel_old_input', Input::get()); static::age_flash(); 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()) { $minutes = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime'); @@ -206,10 +185,7 @@ class Session { Cookie::put('laravel_session', static::$session['id'], $minutes, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https')); } - // --------------------------------------------------------- - // Perform session garbage collection (2% chance). - // Session garbage collection removes all expired sessions. - // --------------------------------------------------------- + // 2% chance of performing session garbage collection... if (mt_rand(1, 100) <= 2) { static::driver()->sweep(time() - (Config::get('session.lifetime') * 60)); @@ -223,9 +199,6 @@ class Session { */ private static function age_flash() { - // ----------------------------------------------------- - // Remove all of the :old: items from the session. - // ----------------------------------------------------- foreach (static::$session['data'] as $key => $value) { if (strpos($key, ':old:') === 0) @@ -234,10 +207,6 @@ class Session { } } - // ----------------------------------------------------- - // 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)