Skip to content

Commit

Permalink
Split ServerRequest::session() into getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Hoffmann committed Apr 11, 2017
1 parent 1151f5d commit 7946bb7
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Auth/Storage/SessionStorage.php
Expand Up @@ -67,7 +67,7 @@ class SessionStorage implements StorageInterface
*/
public function __construct(ServerRequest $request, Response $response, array $config = [])
{
$this->_session = $request->session();
$this->_session = $request->getSession();
$this->setConfig($config);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Component/AuthComponent.php
Expand Up @@ -254,7 +254,7 @@ public function initialize(array $config)
$controller = $this->_registry->getController();
$this->eventManager($controller->eventManager());
$this->response =& $controller->response;
$this->session = $controller->request->session();
$this->session = $controller->request->getSession();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Component/FlashComponent.php
Expand Up @@ -60,7 +60,7 @@ class FlashComponent extends Component
public function __construct(ComponentRegistry $registry, array $config = [])
{
parent::__construct($registry, $config);
$this->_session = $registry->getController()->request->session();
$this->_session = $registry->getController()->request->getSession();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Component/SecurityComponent.php
Expand Up @@ -100,7 +100,7 @@ class SecurityComponent extends Component
public function startup(Event $event)
{
$controller = $event->getSubject();
$this->session = $controller->request->session();
$this->session = $controller->request->getSession();
$this->_action = $controller->request->getParam('action');
$hasData = (bool)$controller->request->getData();
try {
Expand Down
28 changes: 28 additions & 0 deletions src/Http/ServerRequest.php
Expand Up @@ -514,12 +514,40 @@ public function contentType()
return $this->env('HTTP_CONTENT_TYPE');
}

/**
* Update the request with a new session instance.
*
* Returns an updated request object. This method returns
* a *new* request object and does not mutate the request in-place.
*
* @param \Cake\Network\Session $session the session object to use
* @return static
*/
public function withSession(Session $session)
{
$new = clone $this;
$new->session = $session;

return $new;
}

/**
* Returns the instance of the Session object for this request
*
* @return \Cake\Network\Session
*/
public function getSession()
{
return $this->session;
}

/**
* Returns the instance of the Session object for this request
*
* If a session object is passed as first argument it will be set as
* the session to use for this request
*
* @deprecated 3.5.0 Use getSession()/withSession() instead.
* @param \Cake\Network\Session|null $session the session object to use
* @return \Cake\Network\Session
*/
Expand Down
4 changes: 2 additions & 2 deletions src/View/Helper/FlashHelper.php
Expand Up @@ -70,7 +70,7 @@ class FlashHelper extends Helper
*/
public function render($key = 'flash', array $options = [])
{
if (!$this->request->session()->check("Flash.$key")) {
if (!$this->request->getSession()->check("Flash.$key")) {
return null;
}

Expand All @@ -81,7 +81,7 @@ public function render($key = 'flash', array $options = [])
$key
));
}
$this->request->session()->delete("Flash.$key");
$this->request->getSession()->delete("Flash.$key");

$out = '';
foreach ($flash as $message) {
Expand Down
4 changes: 2 additions & 2 deletions src/View/Helper/SessionHelper.php
Expand Up @@ -54,7 +54,7 @@ public function __construct(View $View, array $config = [])
*/
public function read($name = null)
{
return $this->request->session()->read($name);
return $this->request->getSession()->read($name);
}

/**
Expand All @@ -70,7 +70,7 @@ public function read($name = null)
*/
public function check($name)
{
return $this->request->session()->check($name);
return $this->request->getSession()->check($name);
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Http/ServerRequestTest.php
Expand Up @@ -3178,6 +3178,35 @@ public function testSession()
$this->assertEquals($session, $request->session());
}

/**
* Tests getting the session from the request
*
* @return void
*/
public function testGetSession()
{
$session = new Session;
$request = new ServerRequest(['session' => $session]);
$this->assertSame($session, $request->getSession());

$request = ServerRequestFactory::fromGlobals();
$this->assertEquals($session, $request->getSession());
}

/**
* Tests setting the session to the request
*
* @return void
*/
public function testWithtSession()
{
$session = new Session;
$request = new ServerRequest();
$newRequest = $request->withSession($session);
$this->assertNotSame($newRequest, $request);
$this->assertSame($session, $newRequest->getSession());
}

/**
* Test the content type method.
*
Expand Down

0 comments on commit 7946bb7

Please sign in to comment.