diff --git a/src/Network/Request.php b/src/Network/Request.php index 7969447a581..6efd19dc511 100644 --- a/src/Network/Request.php +++ b/src/Network/Request.php @@ -16,6 +16,7 @@ use Cake\Core\Configure; use Cake\Error; +use Cake\Network\Session; use Cake\Utility\Hash; /** @@ -138,6 +139,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. * @@ -156,6 +164,7 @@ public static function createFromGlobals() { 'environment' => $_SERVER + $_ENV, 'base' => $base, 'webroot' => $webroot, + 'session' => new Session() ); $config['url'] = static::_url($config); return new static($config); @@ -177,6 +186,7 @@ public static function createFromGlobals() { * - `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 @@ public function __construct($config = array()) { 'base' => '', 'webroot' => '', 'input' => null, + 'session' => null ); $this->_setConfig($config); } @@ -225,6 +236,7 @@ protected function _setConfig($config) { $this->data = $this->_processFiles($config['post'], $config['files']); $this->query = $this->_processGet($config['query']); $this->params = $config['params']; + $this->_session = $config['session']; } /** @@ -425,6 +437,15 @@ protected function _processFileData(&$post, $path, $data, $field) { } } +/** + * 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. * diff --git a/tests/TestCase/Network/RequestTest.php b/tests/TestCase/Network/RequestTest.php index 05e2988aebf..13d39a37e30 100644 --- a/tests/TestCase/Network/RequestTest.php +++ b/tests/TestCase/Network/RequestTest.php @@ -17,6 +17,7 @@ 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; @@ -2250,6 +2251,20 @@ public function testAllowMethodException() { $request->allowMethod('POST'); } +/** + * 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 *