Browse Source

Fixing creation of sessions engines

Jose Lorenzo Rodriguez 12 years ago
parent
commit
7b57f47587

+ 6 - 5
src/Network/Session.php

@@ -158,7 +158,9 @@ class Session {
 		}
 
 		if (!empty($config['handler']['engine'])) {
-			session_set_save_handler($this->engine($config['handler']['engine']), false);
+			$class = $config['handler']['engine'];
+			unset($config['handler']['engine']);
+			session_set_save_handler($this->engine($class, $config['handler']), false);
 		}
 
 		session_register_shutdown();
@@ -167,11 +169,10 @@ class Session {
 /**
  * Find the handler class and make sure it implements the correct interface.
  *
- * @param string $class
  * @return void
  * @throws \Cake\Error\Exception
  */
-	public function engine($class = null) {
+	public function engine($class = null, $options = []) {
 		if ($class === null) {
 			return $this->_engine;
 		}
@@ -181,14 +182,14 @@ class Session {
 			throw new Error\Exception(sprintf('Could not load %s to handle the session.', $class));
 		}
 
-		$handler = new $class();
+		$handler = new $class($options);
 		if (!($handler instanceof SessionHandlerInterface)) {
 			throw new Error\Exception(
 				'Chosen SessionHandler does not implement SessionHandlerInterface, it cannot be used with an engine key.'
 			);
 		}
 
-		$this->_engine = $engine;
+		return $this->_engine = $handler;
 	}
 
 	public function options(array $options) {

+ 22 - 5
src/Network/Session/CacheSession.php

@@ -17,7 +17,6 @@
 namespace Cake\Network\Session;
 
 use Cake\Cache\Cache;
-use Cake\Core\Configure;
 use SessionHandlerInterface;
 
 /**
@@ -28,6 +27,24 @@ use SessionHandlerInterface;
 class CacheSession implements SessionHandlerInterface {
 
 /**
+ * Options for this session engine
+ *
+ * @var array
+ */
+	protected $_options = [];
+
+/**
+ * Constructor.
+ *
+ * @param array $config The configuration to use for this engine
+ * It requires the key 'config' which is the name of the Cache config to use for
+ * storign the session
+ */
+	public function __construct(array $config) {
+		$this->_options = $config;
+	}
+
+/**
  * Method called on open of a database session.
  *
  * @param string $savePath The path where to store/retrieve the session.
@@ -54,7 +71,7 @@ class CacheSession implements SessionHandlerInterface {
  * @return mixed The value of the key or false if it does not exist
  */
 	public function read($id) {
-		return Cache::read($id, Configure::read('Session.handler.config'));
+		return Cache::read($id, $this->_options['config']);
 	}
 
 /**
@@ -65,7 +82,7 @@ class CacheSession implements SessionHandlerInterface {
  * @return bool True for successful write, false otherwise.
  */
 	public function write($id, $data) {
-		return Cache::write($id, $data, Configure::read('Session.handler.config'));
+		return Cache::write($id, $data, $this->_options['config']);
 	}
 
 /**
@@ -75,7 +92,7 @@ class CacheSession implements SessionHandlerInterface {
  * @return bool True for successful delete, false otherwise.
  */
 	public function destroy($id) {
-		return Cache::delete($id, Configure::read('Session.handler.config'));
+		return Cache::delete($id, $this->_options['config']);
 	}
 
 /**
@@ -85,7 +102,7 @@ class CacheSession implements SessionHandlerInterface {
  * @return bool True on success, false on failure.
  */
 	public function gc($maxlifetime) {
-		return Cache::gc(Configure::read('Session.handler.config'), time() - $maxlifetime);
+		return Cache::gc($this->_options['config'], time() - $maxlifetime);
 	}
 
 }

+ 4 - 3
src/Network/Session/DatabaseSession.php

@@ -17,7 +17,6 @@
 
 namespace Cake\Network\Session;
 
-use Cake\Core\Configure;
 use Cake\ORM\Entity;
 use Cake\ORM\TableRegistry;
 use SessionHandlerInterface;
@@ -46,9 +45,11 @@ class DatabaseSession implements SessionHandlerInterface {
  * Constructor. Looks at Session configuration information and
  * sets up the session model.
  *
+ * @param array $config The configuration for this engine. It requires the 'model'
+ * key to be present corresponding to the Table to use for managing the sessions.
  */
-	public function __construct() {
-		$modelAlias = Configure::read('Session.handler.model');
+	public function __construct(array $config) {
+		$modelAlias = $config['model'];
 
 		if (empty($modelAlias)) {
 			$config = TableRegistry::exists('Sessions') ? [] : ['table' => 'cake_sessions'];