Browse Source

Putting the session inside the request object

Jose Lorenzo Rodriguez 12 years ago
parent
commit
b08bd00998
2 changed files with 36 additions and 0 deletions
  1. 21 0
      src/Network/Request.php
  2. 15 0
      tests/TestCase/Network/RequestTest.php

+ 21 - 0
src/Network/Request.php

@@ -16,6 +16,7 @@ namespace Cake\Network;
 
 use Cake\Core\Configure;
 use Cake\Error;
+use Cake\Network\Session;
 use Cake\Utility\Hash;
 
 /**
@@ -139,6 +140,13 @@ class Request implements \ArrayAccess {
 	protected $_input = '';
 
 /**
+ * Instance of a Session object relative to this request
+ *
+ * @var \Cake\Network\Session
+ */
+	protected $_session;
+
+/**
  * Wrapper method to create a new request from PHP superglobals.
  *
  * Uses the $_GET, $_POST, $_FILES, $_COOKIE, $_SERVER, $_ENV and php://input data to construct
@@ -156,6 +164,7 @@ class Request implements \ArrayAccess {
 			'environment' => $_SERVER + $_ENV,
 			'base' => $base,
 			'webroot' => $webroot,
+			'session' => new Session()
 		);
 		$config['url'] = static::_url($config);
 		return new static($config);
@@ -177,6 +186,7 @@ class Request implements \ArrayAccess {
  * - `base` The base url for the request.
  * - `webroot` The webroot directory for the request.
  * - `input` The data that would come from php://input this is useful for simulating
+ * - `session` An instance of a Session object
  *   requests with put, patch or delete data.
  *
  * @param string|array $config An array of request data to create a request with.
@@ -196,6 +206,7 @@ class Request implements \ArrayAccess {
 			'base' => '',
 			'webroot' => '',
 			'input' => null,
+			'session' => null
 		);
 		$this->_setConfig($config);
 	}
@@ -225,6 +236,7 @@ class Request implements \ArrayAccess {
 		$this->data = $this->_processFiles($config['post'], $config['files']);
 		$this->query = $this->_processGet($config['query']);
 		$this->params = $config['params'];
+		$this->_session = $config['session'];
 	}
 
 /**
@@ -426,6 +438,15 @@ class Request implements \ArrayAccess {
 	}
 
 /**
+ * Returns the instance of the Session object for this request
+ *
+ * @return \Cake\Network\Session
+ */
+	public function session() {
+		return $this->_session;
+	}
+
+/**
  * Get the IP the client is using, or says they are using.
  *
  * @return string The client IP.

+ 15 - 0
tests/TestCase/Network/RequestTest.php

@@ -17,6 +17,7 @@ namespace Cake\Test\TestCase\Network;
 use Cake\Core\Configure;
 use Cake\Error;
 use Cake\Network\Request;
+use Cake\Network\Session;
 use Cake\Routing\Dispatcher;
 use Cake\TestSuite\TestCase;
 use Cake\Utility\Xml;
@@ -2251,6 +2252,20 @@ XML;
 	}
 
 /**
+ * Tests getting the sessions from the request
+ *
+ * @return void
+ */
+	public function testSession() {
+		$session = new Session;
+		$request = new Request(['session' => $session]);
+		$this->assertSame($session, $request->session());
+
+		$request = Request::createFromGlobals();
+		$this->assertEquals($session, $request->session());
+	}
+
+/**
  * loadEnvironment method
  *
  * @param array $env