restructured session driver interfaces and added cookie session driver.

This commit is contained in:
Taylor Otwell 2011-08-02 21:05:20 -05:00
parent 3583bc3b09
commit 32f383205e
9 changed files with 76 additions and 35 deletions

View File

@ -12,7 +12,7 @@
| Since HTTP is stateless, sessions are used to maintain "state" across | Since HTTP is stateless, sessions are used to maintain "state" across
| multiple requests from the same user of your application. | multiple requests from the same user of your application.
| |
| Supported Drivers: 'file', 'db', 'memcached', 'apc'. | Supported Drivers: 'cookie', 'file', 'db', 'memcached', 'apc'.
| |
*/ */

View File

@ -27,6 +27,10 @@ public static function driver()
{ {
switch (Config::get('session.driver')) switch (Config::get('session.driver'))
{ {
case 'cookie':
static::$driver = new Session\Cookie;
break;
case 'file': case 'file':
static::$driver = new Session\File; static::$driver = new Session\File;
break; break;
@ -203,8 +207,8 @@ public static function close()
Cookie::put('laravel_session', static::$session['id'], $minutes, $config['path'], $config['domain'], $config['https'], $config['http_only']); Cookie::put('laravel_session', static::$session['id'], $minutes, $config['path'], $config['domain'], $config['https'], $config['http_only']);
} }
// 2% chance of performing session garbage collection... // 2% chance of performing session garbage collection on any given request...
if (mt_rand(1, 100) <= 2) if (mt_rand(1, 100) <= 2 and static::driver() instanceof Session\Sweeper)
{ {
static::driver()->sweep(time() - ($config['lifetime'] * 60)); static::driver()->sweep(time() - ($config['lifetime'] * 60));
} }

View File

@ -37,15 +37,4 @@ public function delete($id)
Cache::driver('apc')->forget($id); Cache::driver('apc')->forget($id);
} }
/**
* Delete all expired sessions.
*
* @param int $expiration
* @return void
*/
public function sweep($expiration)
{
// APC sessions will expire automatically.
}
} }

54
system/session/cookie.php Normal file
View File

@ -0,0 +1,54 @@
<?php namespace System\Session;
use System\Crypt;
use System\Config;
class Cookie implements Driver {
public function __construct()
{
if (Config::get('application.key') == '')
{
throw new \Exception("You must set an application key before using the Cookie session driver.");
}
}
/**
* Load a session by ID.
*
* @param string $id
* @return array
*/
public function load($id)
{
if (\System\Cookie::has('session_payload'))
{
return unserialize(Crypt::decrypt(\System\Cookie::get('session_payload')));
}
}
/**
* Save a session.
*
* @param array $session
* @return void
*/
public function save($session)
{
$c = \System\Config::get('session');
\System\Cookie::put('session_payload', Crypt::encrypt(serialize($session)), $c['lifetime'], $c['path'], $c['domain'], $c['https'], $c['http_only']);
}
/**
* Delete a session by ID.
*
* @param string $id
* @return void
*/
public function delete($id)
{
\System\Cookie::forget('session_payload');
}
}

View File

@ -3,7 +3,7 @@
use System\Config; use System\Config;
use System\DB\Manager; use System\DB\Manager;
class DB implements Driver { class DB implements Driver, Sweeper {
/** /**
* Load a session by ID. * Load a session by ID.

View File

@ -26,12 +26,4 @@ public function save($session);
*/ */
public function delete($id); public function delete($id);
/**
* Delete all expired sessions.
*
* @param int $expiration
* @return void
*/
public function sweep($expiration);
} }

View File

@ -1,6 +1,6 @@
<?php namespace System\Session; <?php namespace System\Session;
class File implements Driver { class File implements Driver, Sweeper {
/** /**
* Load a session by ID. * Load a session by ID.

View File

@ -38,15 +38,4 @@ public function delete($id)
Cache::driver('memcached')->forget($id); Cache::driver('memcached')->forget($id);
} }
/**
* Delete all expired sessions.
*
* @param int $expiration
* @return void
*/
public function sweep($expiration)
{
// Memcached sessions will expire automatically.
}
} }

View File

@ -0,0 +1,13 @@
<?php namespace System\Session;
interface Sweeper {
/**
* Delete all expired sessions.
*
* @param int $expiration
* @return void
*/
public function sweep($expiration);
}