From 39d323c44f1ed79070f26cdbed108f3dc4fabca1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 14 Aug 2011 23:10:46 -0500 Subject: [PATCH] refactoring response and session classes. --- system/response.php | 2 +- system/session.php | 49 ++++++++++++++++----------------------------- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/system/response.php b/system/response.php index b756e6ed..95ff4a4d 100644 --- a/system/response.php +++ b/system/response.php @@ -133,7 +133,7 @@ public static function prepare($response) */ public function send() { - if ( ! array_key_exists('Content-Type', $this->headers)) $this->header('Content-Type', 'text/html; charset=utf-8') + if ( ! array_key_exists('Content-Type', $this->headers)) $this->header('Content-Type', 'text/html; charset=utf-8'); if ( ! headers_sent()) $this->send_headers(); diff --git a/system/session.php b/system/session.php index cf12c5b5..440e5921 100644 --- a/system/session.php +++ b/system/session.php @@ -10,7 +10,7 @@ class Session { public static $driver; /** - * The session. + * The session payload, which contains the session ID, data and last activity timestamp. * * @var array */ @@ -60,28 +60,24 @@ public static function load($id) { static::$session = ( ! is_null($id)) ? static::driver()->load($id) : null; - if (is_null(static::$session) or static::expired(static::$session['last_activity'])) - { - static::$session = array('id' => Str::random(40), 'data' => array()); - } + if (static::invalid(static::$session)) static::$session = array('id' => Str::random(40), 'data' => array()); - if ( ! static::has('csrf_token')) - { - static::put('csrf_token', Str::random(16)); - } + if ( ! static::has('csrf_token')) static::put('csrf_token', Str::random(16)); static::$session['last_activity'] = time(); } /** - * Determine if a session has expired based on the last activity. + * Determine if a session is valid. * - * @param int $last_activity + * A session is considered valid if it exists and has not expired. + * + * @param array $session * @return bool */ - private static function expired($last_activity) + private static function invalid($session) { - return (time() - $last_activity) > (Config::get('session.lifetime') * 60); + return is_null($session) or (time() - $session['last_activity']) > (Config::get('session.lifetime') * 60); } /** @@ -92,9 +88,7 @@ private static function expired($last_activity) */ public static function has($key) { - return (array_key_exists($key, static::$session['data']) or - array_key_exists(':old:'.$key, static::$session['data']) or - array_key_exists(':new:'.$key, static::$session['data'])); + return ( ! is_null(static::get($key))); } /** @@ -105,17 +99,9 @@ public static function has($key) */ public static function get($key, $default = null) { - if (array_key_exists($key, static::$session['data'])) + foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility) { - 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]; + if (array_key_exists($possibility, static::$session['data'])) return static::$session['data'][$possibility]; } return is_callable($default) ? call_user_func($default) : $default; @@ -181,12 +167,14 @@ public static function regenerate() /** * Close the session. * + * The session will be stored in persistant storage and the session cookie will be + * session cookie will be sent to the browser. The old input data will also be + * stored in the session flash data. + * * @return void */ public static function close() { - // 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(); @@ -210,10 +198,7 @@ private static function age_flash() { foreach (static::$session['data'] as $key => $value) { - if (strpos($key, ':old:') === 0) - { - static::forget($key); - } + if (strpos($key, ':old:') === 0) static::forget($key); } foreach (static::$session['data'] as $key => $value)