refactoring the session class.
This commit is contained in:
@@ -6,35 +6,38 @@ use Laravel\Cookie;
|
||||
use Laravel\Session\Drivers\Driver;
|
||||
use Laravel\Session\Drivers\Sweeper;
|
||||
|
||||
class Payload {
|
||||
class Session {
|
||||
|
||||
/**
|
||||
* The session array that is stored by the driver.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $session;
|
||||
protected static $session;
|
||||
|
||||
/**
|
||||
* Indicates if the session already exists in storage.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $exists = true;
|
||||
protected static $exists = true;
|
||||
|
||||
/**
|
||||
* Create a new session payload instance.
|
||||
* Start the session handling for the current request.
|
||||
*
|
||||
* @param array $session
|
||||
* @param Driver $driver
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($session = null)
|
||||
public static function start(Driver $driver)
|
||||
{
|
||||
$this->session = $session;
|
||||
|
||||
if ($this->invalid())
|
||||
if ( ! is_null($id = Cookie::get(Config::$items['session']['cookie'])))
|
||||
{
|
||||
$this->exists = false;
|
||||
static::$session = $driver->load($id);
|
||||
}
|
||||
|
||||
if (static::invalid())
|
||||
{
|
||||
static::$exists = false;
|
||||
|
||||
// A CSRF token is stored in every session. The token is used by the
|
||||
// Form class and the "csrf" filter to protect the application from
|
||||
@@ -42,7 +45,7 @@ class Payload {
|
||||
// random string which should be posted with each request.
|
||||
$token = Str::random(40);
|
||||
|
||||
$this->session = array('id' => Str::random(40), 'data' => compact('token'));
|
||||
static::$session = array('id' => Str::random(40), 'data' => compact('token'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,11 +56,11 @@ class Payload {
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function invalid()
|
||||
protected static function invalid()
|
||||
{
|
||||
$lifetime = Config::$items['session']['lifetime'];
|
||||
|
||||
return is_null($this->session) or (time() - $this->last_activity > ($lifetime * 60));
|
||||
return is_null(static::$session) or (time() - static::$session['last_activity'] > ($lifetime * 60));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,9 +69,9 @@ class Payload {
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key)
|
||||
public static function has($key)
|
||||
{
|
||||
return ( ! is_null($this->get($key)));
|
||||
return ( ! is_null(static::get($key)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,13 +91,13 @@ class Payload {
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
public static function get($key, $default = null)
|
||||
{
|
||||
foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
|
||||
{
|
||||
if (array_key_exists($possibility, $this->session['data']))
|
||||
if (array_key_exists($possibility, static::$session['data']))
|
||||
{
|
||||
return $this->session['data'][$possibility];
|
||||
return static::$session['data'][$possibility];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,9 +111,9 @@ class Payload {
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function put($key, $value)
|
||||
public static function put($key, $value)
|
||||
{
|
||||
$this->session['data'][$key] = $value;
|
||||
static::$session['data'][$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,9 +125,9 @@ class Payload {
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function flash($key, $value)
|
||||
public static function flash($key, $value)
|
||||
{
|
||||
$this->put(':new:'.$key, $value);
|
||||
static::put(':new:'.$key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,9 +135,9 @@ class Payload {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function reflash()
|
||||
public static function reflash()
|
||||
{
|
||||
$this->keep(array_keys($this->session['data']));
|
||||
static::keep(array_keys(static::$session['data']));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,9 +146,9 @@ class Payload {
|
||||
* @param string|array $key
|
||||
* @return void
|
||||
*/
|
||||
public function keep($keys)
|
||||
public static function keep($keys)
|
||||
{
|
||||
foreach ((array) $keys as $key) $this->flash($key, $this->get($key));
|
||||
foreach ((array) $keys as $key) static::flash($key, static::get($key));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,9 +157,9 @@ class Payload {
|
||||
* @param string $key
|
||||
* @return Driver
|
||||
*/
|
||||
public function forget($key)
|
||||
public static function forget($key)
|
||||
{
|
||||
unset($this->session['data'][$key]);
|
||||
unset(static::$session['data'][$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,9 +167,9 @@ class Payload {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function flush()
|
||||
public static function flush()
|
||||
{
|
||||
$this->session['data'] = array();
|
||||
static::$session['data'] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,11 +177,11 @@ class Payload {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function regenerate()
|
||||
public static function regenerate()
|
||||
{
|
||||
$this->session['id'] = Str::random(40);
|
||||
static::$session['id'] = Str::random(40);
|
||||
|
||||
$this->exists = false;
|
||||
static::$exists = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,20 +190,20 @@ class Payload {
|
||||
* @param Driver $driver
|
||||
* @return void
|
||||
*/
|
||||
public function save(Driver $driver)
|
||||
public static function save(Driver $driver)
|
||||
{
|
||||
$this->session['last_activity'] = time();
|
||||
static::$session['last_activity'] = time();
|
||||
|
||||
$this->age();
|
||||
static::age();
|
||||
|
||||
$config = Config::$items['session'];
|
||||
|
||||
// To keep the session persistence code clean, session drivers are
|
||||
// responsible for the storage of the session array to the various
|
||||
// available persistent storage mechanisms.
|
||||
$driver->save($this->session, $config, $this->exists);
|
||||
$driver->save(static::$session, $config, static::$exists);
|
||||
|
||||
$this->cookie();
|
||||
static::cookie();
|
||||
|
||||
// Some session drivers implement the Sweeper interface, meaning that they
|
||||
// must clean up expired sessions manually. If the driver is a sweeper, we
|
||||
@@ -222,20 +225,20 @@ class Payload {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function age()
|
||||
protected static function age()
|
||||
{
|
||||
foreach ($this->session['data'] as $key => $value)
|
||||
foreach (static::$session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':old:') === 0) $this->forget($key);
|
||||
if (strpos($key, ':old:') === 0) static::forget($key);
|
||||
}
|
||||
|
||||
// Now that all of the "old" keys have been removed from the session data,
|
||||
// we can re-address all of the newly flashed keys to have old addresses.
|
||||
// The array_combine method uses the first array for keys, and the second
|
||||
// array for values to construct a single array from both.
|
||||
$keys = str_replace(':new:', ':old:', array_keys($this->session['data']));
|
||||
$keys = str_replace(':new:', ':old:', array_keys(static::$session['data']));
|
||||
|
||||
$this->session['data'] = array_combine($keys, array_values($this->session['data']));
|
||||
static::$session['data'] = array_combine($keys, array_values(static::$session['data']));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -243,21 +246,13 @@ class Payload {
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function cookie()
|
||||
protected static function cookie()
|
||||
{
|
||||
$config = Config::$items['session'];
|
||||
|
||||
$minutes = ( ! $config['expire_on_close']) ? $config['lifetime'] : 0;
|
||||
|
||||
Cookie::put($cookie, $this->id, $minutes, $config['path'], $config['domain'], $config['secure']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically retrieve items from the session array.
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return (isset($this->session[$key])) ? $this->session[$key] : $this->get($key);
|
||||
Cookie::put($cookie, static::$session['id'], $minutes, $config['path'], $config['domain'], $config['secure']);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user