more 2.0 changes
This commit is contained in:
@@ -9,16 +9,13 @@ class Session {
|
||||
*/
|
||||
public static $driver;
|
||||
|
||||
/**
|
||||
* The session payload, which contains the session ID, data and last activity timestamp.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $session = array();
|
||||
|
||||
/**
|
||||
* Get the session driver.
|
||||
*
|
||||
* The session driver returned will be the driver specified in the session configuration
|
||||
* file. Only one session driver may be active for a given request, so the driver will
|
||||
* be managed as a singleton.
|
||||
*
|
||||
* @return Session\Driver
|
||||
*/
|
||||
public static function driver()
|
||||
@@ -51,188 +48,22 @@ class Session {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a user session by ID.
|
||||
* Pass all other methods to the default session driver.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
* By dynamically passing these method calls to the default driver, the developer is
|
||||
* able to use with a convenient API when working with the session.
|
||||
*
|
||||
* <code>
|
||||
* // Get an item from the default session driver
|
||||
* $name = Session::get('name');
|
||||
*
|
||||
* // Equivalent call using the driver method
|
||||
* $name = Session::driver()->get('name');
|
||||
* </code>
|
||||
*/
|
||||
public static function load($id)
|
||||
public static function __callStatic($method, $parameters)
|
||||
{
|
||||
static::$session = ( ! is_null($id)) ? static::driver()->load($id) : null;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
static::$session['last_activity'] = time();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a session is valid.
|
||||
*
|
||||
* A session is considered valid if it exists and has not expired.
|
||||
*
|
||||
* @param array $session
|
||||
* @return bool
|
||||
*/
|
||||
private static function invalid($session)
|
||||
{
|
||||
return is_null($session) or (time() - $session['last_activity']) > (Config::get('session.lifetime') * 60);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the session or flash data contains an item.
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public static function has($key)
|
||||
{
|
||||
return ( ! is_null(static::get($key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the session or flash data.
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public static function get($key, $default = null)
|
||||
{
|
||||
foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
|
||||
{
|
||||
if (array_key_exists($possibility, static::$session['data'])) return static::$session['data'][$possibility];
|
||||
}
|
||||
|
||||
return is_callable($default) ? call_user_func($default) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an item to the session.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public static function put($key, $value)
|
||||
{
|
||||
static::$session['data'][$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an item to the session flash data.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public static function flash($key, $value)
|
||||
{
|
||||
static::put(':new:'.$key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an item from the session.
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public static function forget($key)
|
||||
{
|
||||
unset(static::$session['data'][$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all items from the session.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function flush()
|
||||
{
|
||||
static::$session['data'] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate the session ID.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function regenerate()
|
||||
{
|
||||
static::driver()->delete(static::$session['id']);
|
||||
|
||||
static::$session['id'] = Str::random(40);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
static::flash('laravel_old_input', Input::get());
|
||||
|
||||
static::age_flash();
|
||||
|
||||
static::driver()->save(static::$session);
|
||||
|
||||
static::write_cookie();
|
||||
|
||||
if (mt_rand(1, 100) <= 2 and static::driver() instanceof Session\Sweeper)
|
||||
{
|
||||
static::driver()->sweep(time() - (Config::get('session.lifetime') * 60));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Age the session flash data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function age_flash()
|
||||
{
|
||||
foreach (static::$session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':old:') === 0) static::forget($key);
|
||||
}
|
||||
|
||||
foreach (static::$session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':new:') === 0)
|
||||
{
|
||||
static::put(':old:'.substr($key, 5), $value);
|
||||
|
||||
static::forget($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the session cookie.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private static function write_cookie()
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
extract(Config::get('session'));
|
||||
|
||||
$minutes = ($expire_on_close) ? 0 : $lifetime;
|
||||
|
||||
Cookie::put('laravel_session', static::$session['id'], $minutes, $path, $domain, $https, $http_only);
|
||||
}
|
||||
return call_user_func_array(array(static::driver(), $method), $parameters);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user