Browse Source

Using gc_maxlifetime as the default timeout and improving engine
instantiation

Jose Lorenzo Rodriguez 12 years ago
parent
commit
a514aa7b3e

+ 5 - 6
src/Network/Session.php

@@ -142,18 +142,13 @@ class Session {
 			$this->options($config['ini']);
 		}
 
-		if (!empty($config['handler']) && !isset($config['handler']['engine'])) {
-			//TODO: REmove this feature
-			call_user_func_array('session_set_save_handler', $config['handler']);
-		}
-
 		if (!empty($config['handler']['engine'])) {
 			$class = $config['handler']['engine'];
 			unset($config['handler']['engine']);
 			session_set_save_handler($this->engine($class, $config['handler']), false);
 		}
 
-		$this->_lifetime = ini_get('session.cookie_lifetime');
+		$this->_lifetime = ini_get('session.gc_maxlifetime');
 		session_register_shutdown();
 	}
 
@@ -164,6 +159,10 @@ class Session {
  * @throws \Cake\Error\Exception
  */
 	public function engine($class = null, $options = []) {
+		if ($class instanceof SessionHandlerInterface) {
+			return $this->_engine = $class;
+		}
+
 		if ($class === null) {
 			return $this->_engine;
 		}

+ 1 - 1
src/Network/Session/DatabaseSession.php

@@ -58,7 +58,7 @@ class DatabaseSession implements SessionHandlerInterface {
 			$this->_table = TableRegistry::get($modelAlias);
 		}
 
-		$this->_timeout = Configure::read('Session.timeout') * 60;
+		$this->_timeout = ini_get('session.gc_maxlifetime');
 	}
 
 /**

+ 20 - 1
tests/TestCase/Network/SessionTest.php

@@ -365,13 +365,16 @@ class SessionTest extends TestCase {
 		$config = [
 			'defaults' => 'cake',
 			'handler' => array(
-				'engine' => 'TestAppLibSession'
+				'engine' => 'TestAppLibSession',
+				'these' => 'are',
+				'a few' => 'options'
 			)
 		];
 
 		$session = Session::create($config);
 		$this->assertInstanceOf('TestApp\Network\Session\TestAppLibSession', $session->engine());
 		$this->assertEquals('user', ini_get('session.save_handler'));
+		$this->assertEquals(['these' => 'are', 'a few' => 'options'], $session->engine()->options);
 	}
 
 /**
@@ -396,6 +399,22 @@ class SessionTest extends TestCase {
 	}
 
 /**
+ * Tests that it is possible to pass an already made instance as the session engine
+ *
+ * @return void
+ */
+	public function testEngineWithPreMadeInstance() {
+		\Cake\Core\Configure::write('App.namespace', 'TestApp');
+		$engine = new \TestApp\Network\Session\TestAppLibSession;
+		$session = new Session(['handler' => ['engine' => $engine]]);
+		$this->assertSame($engine, $session->engine());
+
+		$session = new Session();
+		$session->engine($engine);
+		$this->assertSame($engine, $session->engine());
+	}
+
+/**
  * Test that cookieTimeout matches timeout when unspecified.
  *
  * @return void

+ 6 - 0
tests/test_app/TestApp/Network/Session/TestAppLibSession.php

@@ -9,6 +9,12 @@ use SessionHandlerInterface;
  */
 class TestAppLibSession implements SessionHandlerInterface {
 
+	public $options = [];
+
+	public function __construct($options = []) {
+		$this->options = $options;
+	}
+
 	public function open($savePath, $name) {
 		return true;
 	}