more 2.0 changes
This commit is contained in:
@@ -3,39 +3,21 @@
|
||||
use Laravel\Cache;
|
||||
use Laravel\Config;
|
||||
|
||||
class APC implements Driver {
|
||||
class APC extends Driver {
|
||||
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
public function load($id)
|
||||
protected function load($id)
|
||||
{
|
||||
return Cache::driver('apc')->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a session.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
public function save($session)
|
||||
protected function save()
|
||||
{
|
||||
Cache::driver('apc')->put($session['id'], $session, Config::get('session.lifetime'));
|
||||
Cache::driver('apc')->put($this->session['id'], $this->session, Config::get('session.lifetime'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
protected function delete()
|
||||
{
|
||||
Cache::driver('apc')->forget($id);
|
||||
Cache::driver('apc')->forget($this->session['id']);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
use Laravel\Config;
|
||||
use Laravel\Crypter;
|
||||
|
||||
class Cookie implements Driver {
|
||||
class Cookie extends Driver {
|
||||
|
||||
/**
|
||||
* The Crypter instance.
|
||||
@@ -27,13 +27,7 @@ class Cookie implements Driver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
public function load($id)
|
||||
protected function load($id)
|
||||
{
|
||||
if (\System\Cookie::has('session_payload'))
|
||||
{
|
||||
@@ -41,31 +35,19 @@ class Cookie implements Driver {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a session.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
public function save($session)
|
||||
protected function save()
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
extract(Config::get('session'));
|
||||
|
||||
$payload = $this->crypter->encrypt(serialize($session));
|
||||
$payload = $this->crypter->encrypt(serialize($this->session));
|
||||
|
||||
\System\Cookie::put('session_payload', $payload, $lifetime, $path, $domain, $https, $http_only);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
protected function delete()
|
||||
{
|
||||
\System\Cookie::forget('session_payload');
|
||||
}
|
||||
|
||||
@@ -2,15 +2,9 @@
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
class DB implements Driver, Sweeper {
|
||||
class DB extends Driver implements Sweeper {
|
||||
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
public function load($id)
|
||||
protected function load($id)
|
||||
{
|
||||
$session = $this->table()->find($id);
|
||||
|
||||
@@ -24,40 +18,22 @@ class DB implements Driver, Sweeper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a session.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
public function save($session)
|
||||
protected function save()
|
||||
{
|
||||
$this->delete($session['id']);
|
||||
$this->delete($this->session['id']);
|
||||
|
||||
$this->table()->insert(array(
|
||||
'id' => $session['id'],
|
||||
'last_activity' => $session['last_activity'],
|
||||
'data' => serialize($session['data'])
|
||||
'id' => $this->session['id'],
|
||||
'last_activity' => $this->session['last_activity'],
|
||||
'data' => serialize($this->session['data'])
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
protected function delete()
|
||||
{
|
||||
$this->table()->delete($id);
|
||||
$this->table()->delete($this->session['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all expired sessions.
|
||||
*
|
||||
* @param int $expiration
|
||||
* @return void
|
||||
*/
|
||||
public function sweep($expiration)
|
||||
{
|
||||
$this->table()->where('last_activity', '<', $expiration)->delete();
|
||||
|
||||
@@ -1,29 +1,242 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
interface Driver {
|
||||
use Laravel\Str;
|
||||
use Laravel\Config;
|
||||
use Laravel\Cookie;
|
||||
|
||||
abstract class Driver {
|
||||
|
||||
/**
|
||||
* The session payload, which contains the session ID, data and last activity timestamp.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $session = array();
|
||||
|
||||
/**
|
||||
* Load the session for a given session ID.
|
||||
*
|
||||
* The session will be checked for validity and necessary data. For example, if the session
|
||||
* does not have a CSRF token, a token will be generated for the session.
|
||||
*
|
||||
* If the session has expired, a new, empty session will be generated.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function start($id)
|
||||
{
|
||||
$this->session = ( ! is_null($id)) ? $this->load($id) : null;
|
||||
|
||||
if (is_null($this->session) or (time() - $this->session['last_activity']) > (Config::get('session.lifetime') * 60))
|
||||
{
|
||||
$this->session = array('id' => Str::random(40), 'data' => array());
|
||||
}
|
||||
|
||||
if ( ! $this->has('csrf_token')) $this->put('csrf_token', Str::random(16));
|
||||
|
||||
$this->session['last_activity'] = time();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* The session will be retrieved from persistant storage and returned as an array.
|
||||
* The array contains the session ID, last activity UNIX timestamp, and session data.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
public function load($id);
|
||||
abstract protected function load($id);
|
||||
|
||||
/**
|
||||
* Save a session.
|
||||
* Delete the session from persistant storage.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
public function save($session);
|
||||
abstract protected function delete();
|
||||
|
||||
/**
|
||||
* Delete a session by ID.
|
||||
* Save the session to persistant storage.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id);
|
||||
abstract protected function save();
|
||||
|
||||
/**
|
||||
* Determine if the session or flash data contains an item.
|
||||
*
|
||||
* <code>
|
||||
* // Determine if "name" item exists in the session
|
||||
* $exists = Session::driver()->has('name');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key)
|
||||
{
|
||||
return ( ! is_null($this->get($key)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the session.
|
||||
*
|
||||
* A default value may also be specified, and will be returned in the requested
|
||||
* item does not exist in the session.
|
||||
*
|
||||
* <code>
|
||||
* // Get the "name" item from the session
|
||||
* $name = Session::driver()->get('name');
|
||||
*
|
||||
* // Get the "name" item from the session or return "Fred"
|
||||
* $name = Session::driver()->get('name', 'Fred');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
foreach (array($key, ':old:'.$key, ':new:'.$key) as $possibility)
|
||||
{
|
||||
if (array_key_exists($possibility, $this->session['data'])) return $this->session['data'][$possibility];
|
||||
}
|
||||
|
||||
return is_callable($default) ? call_user_func($default) : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an item to the session.
|
||||
*
|
||||
* <code>
|
||||
* // Write the "name" item to the session
|
||||
* Session::driver()->put('name', 'Fred');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function put($key, $value)
|
||||
{
|
||||
$this->session['data'][$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an item to the session flash data.
|
||||
*
|
||||
* Flash data only exists for the next request. After that, it will be removed from
|
||||
* the session. Flash data is useful for temporary status or welcome messages.
|
||||
*
|
||||
* <code>
|
||||
* // Write the "name" item to the session flash data
|
||||
* Session::driver()->flash('name', 'Fred');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function flash($key, $value)
|
||||
{
|
||||
$this->put(':new:'.$key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an item from the session.
|
||||
*
|
||||
* <code>
|
||||
* // Remove the "name" item from the session
|
||||
* Session::driver()->forget('name');
|
||||
* </code>
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function forget($key)
|
||||
{
|
||||
unset($this->session['data'][$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all items from the session.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function flush()
|
||||
{
|
||||
$this->session['data'] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate the session ID.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function regenerate()
|
||||
{
|
||||
$this->delete();
|
||||
|
||||
$this->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.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
$this->age_flash();
|
||||
|
||||
$this->save();
|
||||
|
||||
$this->write_cookie();
|
||||
}
|
||||
|
||||
/**
|
||||
* Age the session flash data.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function age_flash()
|
||||
{
|
||||
foreach ($this->session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':old:') === 0) $this->forget($key);
|
||||
}
|
||||
|
||||
foreach ($this->session['data'] as $key => $value)
|
||||
{
|
||||
if (strpos($key, ':new:') === 0)
|
||||
{
|
||||
$this->put(':old:'.substr($key, 5), $value);
|
||||
|
||||
$this->forget($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the session cookie.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function write_cookie()
|
||||
{
|
||||
if ( ! headers_sent())
|
||||
{
|
||||
extract(Config::get('session'));
|
||||
|
||||
$minutes = ($expire_on_close) ? 0 : $lifetime;
|
||||
|
||||
Cookie::put('laravel_session', $this->session['id'], $minutes, $path, $domain, $https, $http_only);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,46 +1,22 @@
|
||||
<?php namespace Laravel\Session;
|
||||
|
||||
class File implements Driver, Sweeper {
|
||||
class File extends Driver implements Sweeper {
|
||||
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
public function load($id)
|
||||
protected function load($id)
|
||||
{
|
||||
if (file_exists($path = SESSION_PATH.$id)) return unserialize(file_get_contents($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a session.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
public function save($session)
|
||||
protected function save()
|
||||
{
|
||||
file_put_contents(SESSION_PATH.$session['id'], serialize($session), LOCK_EX);
|
||||
file_put_contents(SESSION_PATH.$this->session['id'], serialize($this->session), LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
protected function delete()
|
||||
{
|
||||
@unlink(SESSION_PATH.$id);
|
||||
@unlink(SESSION_PATH.$this->session['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all expired sessions.
|
||||
*
|
||||
* @param int $expiration
|
||||
* @return void
|
||||
*/
|
||||
public function sweep($expiration)
|
||||
{
|
||||
foreach (glob(SESSION_PATH.'*') as $file)
|
||||
|
||||
@@ -3,39 +3,21 @@
|
||||
use Laravel\Cache;
|
||||
use Laravel\Config;
|
||||
|
||||
class Memcached implements Driver {
|
||||
class Memcached extends Driver {
|
||||
|
||||
/**
|
||||
* Load a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return array
|
||||
*/
|
||||
public function load($id)
|
||||
protected function load($id)
|
||||
{
|
||||
return Cache::driver('memcached')->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a session.
|
||||
*
|
||||
* @param array $session
|
||||
* @return void
|
||||
*/
|
||||
public function save($session)
|
||||
protected function save()
|
||||
{
|
||||
Cache::driver('memcached')->put($session['id'], $session, Config::get('session.lifetime'));
|
||||
Cache::driver('memcached')->put($this->session['id'], $this->session, Config::get('session.lifetime'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a session by ID.
|
||||
*
|
||||
* @param string $id
|
||||
* @return void
|
||||
*/
|
||||
public function delete($id)
|
||||
protected function delete()
|
||||
{
|
||||
Cache::driver('memcached')->forget($id);
|
||||
Cache::driver('memcached')->forget($this->session['id']);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
interface Sweeper {
|
||||
|
||||
/**
|
||||
* Delete all expired sessions.
|
||||
* Delete all expired sessions from persistant storage.
|
||||
*
|
||||
* @param int $expiration
|
||||
* @return void
|
||||
|
||||
Reference in New Issue
Block a user