Browse Source

restructured session driver interfaces and added cookie session driver.

Taylor Otwell 13 years ago
parent
commit
32f383205e

+ 1 - 1
application/config/session.php

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

+ 6 - 2
system/session.php

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

+ 0 - 11
system/session/apc.php

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

+ 54 - 0
system/session/cookie.php

@@ -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');
+	}
+
+}

+ 1 - 1
system/session/db.php

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

+ 0 - 8
system/session/driver.php

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

+ 1 - 1
system/session/file.php

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

+ 0 - 11
system/session/memcached.php

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

+ 13 - 0
system/session/sweeper.php

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