Skip to content

Commit

Permalink
Putting the session inside the request object
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 18, 2014
1 parent 3f2ed4e commit b08bd00
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Network/Request.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Core\Configure;
use Cake\Error;
use Cake\Network\Session;
use Cake\Utility\Hash;

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -196,6 +206,7 @@ public function __construct($config = array()) {
'base' => '',
'webroot' => '',
'input' => null,
'session' => null
);
$this->_setConfig($config);
}
Expand Down Expand Up @@ -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'];
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit b08bd00

Please sign in to comment.