Commit 6048d209 by Taylor Otwell

refactoring the session class.

parent 2e1fed29
...@@ -40,7 +40,7 @@ return array( ...@@ -40,7 +40,7 @@ return array(
| |
*/ */
'key' => '', 'key' => 'SomethingElse',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
...@@ -135,7 +135,7 @@ return array( ...@@ -135,7 +135,7 @@ return array(
'Redis' => 'Laravel\\Redis', 'Redis' => 'Laravel\\Redis',
'Request' => 'Laravel\\Request', 'Request' => 'Laravel\\Request',
'Response' => 'Laravel\\Response', 'Response' => 'Laravel\\Response',
'Session' => 'Laravel\\Facades\\Session', 'Session' => 'Laravel\\Session',
'Str' => 'Laravel\\Str', 'Str' => 'Laravel\\Str',
'Validator' => 'Laravel\\Validation\\Validator', 'Validator' => 'Laravel\\Validation\\Validator',
'View' => 'Laravel\\View', 'View' => 'Laravel\\View',
......
...@@ -16,7 +16,7 @@ return array( ...@@ -16,7 +16,7 @@ return array(
| |
*/ */
'driver' => '', 'driver' => 'apc',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
......
...@@ -51,7 +51,6 @@ unset($application, $public, $storage, $laravel, $environment); ...@@ -51,7 +51,6 @@ unset($application, $public, $storage, $laravel, $environment);
*/ */
require SYS_PATH.'arr'.EXT; require SYS_PATH.'arr'.EXT;
require SYS_PATH.'config'.EXT; require SYS_PATH.'config'.EXT;
require SYS_PATH.'facades'.EXT;
require SYS_PATH.'container'.EXT; require SYS_PATH.'container'.EXT;
require SYS_PATH.'autoloader'.EXT; require SYS_PATH.'autoloader'.EXT;
......
...@@ -2,12 +2,6 @@ ...@@ -2,12 +2,6 @@
return array( return array(
'laravel.autoloader' => array('singleton' => true, 'resolver' => function($c)
{
return new Autoloader(Config::$items['application']['aliases']);
}),
'laravel.routing.router' => array('singleton' => true, 'resolver' => function($c) 'laravel.routing.router' => array('singleton' => true, 'resolver' => function($c)
{ {
return new Routing\Router($c->core('routing.loader'), CONTROLLER_PATH); return new Routing\Router($c->core('routing.loader'), CONTROLLER_PATH);
......
...@@ -58,5 +58,3 @@ abstract class Facade { ...@@ -58,5 +58,3 @@ abstract class Facade {
} }
} }
class Session extends Facade { public static $resolve = 'laravel.session'; }
\ No newline at end of file
...@@ -159,7 +159,7 @@ class Form { ...@@ -159,7 +159,7 @@ class Form {
throw new \Exception("A session driver must be specified before using CSRF tokens."); throw new \Exception("A session driver must be specified before using CSRF tokens.");
} }
return IoC::container()->core('session')->get('csrf_token'); return Session::get('csrf_token');
} }
/** /**
......
...@@ -70,7 +70,7 @@ class Input { ...@@ -70,7 +70,7 @@ class Input {
*/ */
public static function flash() public static function flash()
{ {
IoC::container()->core('session')->flash(Input::old_input, static::get()); Session::flash(Input::old_input, static::get());
} }
/** /**
...@@ -106,9 +106,7 @@ class Input { ...@@ -106,9 +106,7 @@ class Input {
throw new \Exception('A session driver must be specified in order to access old input.'); throw new \Exception('A session driver must be specified in order to access old input.');
} }
$session = IoC::container()->core('session'); return Arr::get(Session::get(Input::old_input, array()), $key, $default);
return Arr::get($session->get(Input::old_input, array()), $key, $default);
} }
/** /**
......
...@@ -26,21 +26,9 @@ date_default_timezone_set(Config::$items['application']['timezone']); ...@@ -26,21 +26,9 @@ date_default_timezone_set(Config::$items['application']['timezone']);
*/ */
if (Config::$items['session']['driver'] !== '') if (Config::$items['session']['driver'] !== '')
{ {
require SYS_PATH.'cookie'.EXT;
require SYS_PATH.'session/payload'.EXT;
$driver = IoC::container()->core('session.'.Config::$items['session']['driver']); $driver = IoC::container()->core('session.'.Config::$items['session']['driver']);
if ( ! is_null($id = Cookie::get(Config::$items['session']['cookie']))) Session::start($driver);
{
$payload = new Session\Payload($driver->load($id));
}
else
{
$payload = new Session\Payload;
}
IoC::container()->instance('laravel.session', $payload);
} }
/** /**
...@@ -129,7 +117,7 @@ $response->content = $response->render(); ...@@ -129,7 +117,7 @@ $response->content = $response->render();
*/ */
if (Config::$items['session']['driver'] !== '') if (Config::$items['session']['driver'] !== '')
{ {
IoC::container()->core('session')->save($driver); Session::save($driver);
} }
/** /**
......
...@@ -61,7 +61,7 @@ class Redirect extends Response { ...@@ -61,7 +61,7 @@ class Redirect extends Response {
throw new \Exception('A session driver must be set before setting flash data.'); throw new \Exception('A session driver must be set before setting flash data.');
} }
IoC::container()->core('session')->flash($key, $value); Session::flash($key, $value);
return $this; return $this;
} }
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
use Laravel\Str; use Laravel\Str;
use Laravel\Config; use Laravel\Config;
use Laravel\Cookie; use Laravel\Cookie;
use Laravel\Session\Manager as Session; use Laravel\Session;
class Auth { class Auth {
......
<?php namespace Laravel;
use Closure;
use Laravel\Session\Drivers\Driver;
use Laravel\Session\Drivers\Sweeper;
class Session {
/**
* The session array that is stored by the driver.
*
* @var array
*/
protected static $session;
/**
* Indicates if the session already exists in storage.
*
* @var bool
*/
protected static $exists = true;
/**
* Start the session handling for the current request.
*
* @param Driver $driver
* @return void
*/
public static function start(Driver $driver)
{
if ( ! is_null($id = Cookie::get(Config::$items['session']['cookie'])))
{
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
// cross-site request forgery attacks. The token is simply a long,
// random string which should be posted with each request.
$token = Str::random(40);
static::$session = array('id' => Str::random(40), 'data' => compact('token'));
}
}
/**
* Deteremine if the session payload instance is valid.
*
* The session is considered valid if it exists and has not expired.
*
* @return bool
*/
protected static function invalid()
{
$lifetime = Config::$items['session']['lifetime'];
return is_null(static::$session) or (time() - static::$session['last_activity'] > ($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.
*
* The session flash data will also be checked for the requested item.
*
* <code>
* // Get an item from the session
* $name = Session::get('name');
*
* // Return a default value if the item doesn't exist
* $name = Session::get('name', 'Taylor');
* </code>
*
* @param string $key
* @param mixed $default
* @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 ($default instanceof Closure) ? 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.
*
* Flash data only exists for the next request to the application.
*
* @param string $key
* @param mixed $value
* @return void
*/
public static function flash($key, $value)
{
static::put(':new:'.$key, $value);
}
/**
* Keep all of the session flash data from expiring at the end of the request.
*
* @return void
*/
public static function reflash()
{
static::keep(array_keys(static::$session['data']));
}
/**
* Keep a session flash item from expiring at the end of the request.
*
* @param string|array $key
* @return void
*/
public static function keep($keys)
{
foreach ((array) $keys as $key) static::flash($key, static::get($key));
}
/**
* Remove an item from the session data.
*
* @param string $key
* @return Driver
*/
public static function forget($key)
{
unset(static::$session['data'][$key]);
}
/**
* Remove all of the items from the session.
*
* @return void
*/
public static function flush()
{
static::$session['data'] = array();
}
/**
* Assign a new, random ID to the session.
*
* @return void
*/
public static function regenerate()
{
static::$session['id'] = Str::random(40);
static::$exists = false;
}
/**
* Store the session payload in storage.
*
* @param Driver $driver
* @return void
*/
public static function save(Driver $driver)
{
static::$session['last_activity'] = time();
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(static::$session, $config, static::$exists);
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
// need to determine if garbage collection should be run for the request.
// Since garbage collection can be expensive, the probability of it
// occuring is controlled by the "sweepage" configuration option.
if ($driver instanceof Sweeper and (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0]))
{
$driver->sweep(time() - ($config['lifetime'] * 60));
}
}
/**
* Age the session flash data.
*
* Session flash data is only available during the request in which it
* was flashed, and the request after that. To "age" the data, we will
* remove all of the :old: items and re-address the new items.
*
* @return void
*/
protected static function age()
{
foreach (static::$session['data'] as $key => $value)
{
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(static::$session['data']));
static::$session['data'] = array_combine($keys, array_values(static::$session['data']));
}
/**
* Send the session ID cookie to the browser.
*
* @return void
*/
protected static function cookie()
{
$config = Config::$items['session'];
extract($config, EXTR_SKIP);
$minutes = ( ! $expire_on_close) ? $lifetime : 0;
Cookie::put($cookie, static::$session['id'], $minutes, $path, $domain, $secure);
}
}
\ No newline at end of file
...@@ -6,35 +6,38 @@ use Laravel\Cookie; ...@@ -6,35 +6,38 @@ use Laravel\Cookie;
use Laravel\Session\Drivers\Driver; use Laravel\Session\Drivers\Driver;
use Laravel\Session\Drivers\Sweeper; use Laravel\Session\Drivers\Sweeper;
class Payload { class Session {
/** /**
* The session array that is stored by the driver. * The session array that is stored by the driver.
* *
* @var array * @var array
*/ */
protected $session; protected static $session;
/** /**
* Indicates if the session already exists in storage. * Indicates if the session already exists in storage.
* *
* @var bool * @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 * @return void
*/ */
public function __construct($session = null) public static function start(Driver $driver)
{
if ( ! is_null($id = Cookie::get(Config::$items['session']['cookie'])))
{ {
$this->session = $session; static::$session = $driver->load($id);
}
if ($this->invalid()) if (static::invalid())
{ {
$this->exists = false; static::$exists = false;
// A CSRF token is stored in every session. The token is used by the // 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 // Form class and the "csrf" filter to protect the application from
...@@ -42,7 +45,7 @@ class Payload { ...@@ -42,7 +45,7 @@ class Payload {
// random string which should be posted with each request. // random string which should be posted with each request.
$token = Str::random(40); $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 { ...@@ -53,11 +56,11 @@ class Payload {
* *
* @return bool * @return bool
*/ */
protected function invalid() protected static function invalid()
{ {
$lifetime = Config::$items['session']['lifetime']; $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 { ...@@ -66,9 +69,9 @@ class Payload {
* @param string $key * @param string $key
* @return bool * @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 { ...@@ -88,13 +91,13 @@ class Payload {
* @param mixed $default * @param mixed $default
* @return mixed * @return mixed
*/ */
public function get($key, $default = null) public static function get($key, $default = null)
{ {
foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility) 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 { ...@@ -108,9 +111,9 @@ class Payload {
* @param mixed $value * @param mixed $value
* @return void * @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 { ...@@ -122,9 +125,9 @@ class Payload {
* @param mixed $value * @param mixed $value
* @return void * @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 { ...@@ -132,9 +135,9 @@ class Payload {
* *
* @return void * @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 { ...@@ -143,9 +146,9 @@ class Payload {
* @param string|array $key * @param string|array $key
* @return void * @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 { ...@@ -154,9 +157,9 @@ class Payload {
* @param string $key * @param string $key
* @return Driver * @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 { ...@@ -164,9 +167,9 @@ class Payload {
* *
* @return void * @return void
*/ */
public function flush() public static function flush()
{ {
$this->session['data'] = array(); static::$session['data'] = array();
} }
/** /**
...@@ -174,11 +177,11 @@ class Payload { ...@@ -174,11 +177,11 @@ class Payload {
* *
* @return void * @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 { ...@@ -187,20 +190,20 @@ class Payload {
* @param Driver $driver * @param Driver $driver
* @return void * @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']; $config = Config::$items['session'];
// To keep the session persistence code clean, session drivers are // To keep the session persistence code clean, session drivers are
// responsible for the storage of the session array to the various // responsible for the storage of the session array to the various
// available persistent storage mechanisms. // 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 // Some session drivers implement the Sweeper interface, meaning that they
// must clean up expired sessions manually. If the driver is a sweeper, we // must clean up expired sessions manually. If the driver is a sweeper, we
...@@ -222,20 +225,20 @@ class Payload { ...@@ -222,20 +225,20 @@ class Payload {
* *
* @return void * @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, // 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. // 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 // The array_combine method uses the first array for keys, and the second
// array for values to construct a single array from both. // 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 { ...@@ -243,21 +246,13 @@ class Payload {
* *
* @return void * @return void
*/ */
protected function cookie() protected static function cookie()
{ {
$config = Config::$items['session']; $config = Config::$items['session'];
$minutes = ( ! $config['expire_on_close']) ? $config['lifetime'] : 0; $minutes = ( ! $config['expire_on_close']) ? $config['lifetime'] : 0;
Cookie::put($cookie, $this->id, $minutes, $config['path'], $config['domain'], $config['secure']); Cookie::put($cookie, static::$session['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);
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment