Skip to content

Commit 7946bb7

Browse files
author
Michael Hoffmann
committed
Split ServerRequest::session() into getter/setter
1 parent 1151f5d commit 7946bb7

File tree

8 files changed

+65
-8
lines changed

8 files changed

+65
-8
lines changed

src/Auth/Storage/SessionStorage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SessionStorage implements StorageInterface
6767
*/
6868
public function __construct(ServerRequest $request, Response $response, array $config = [])
6969
{
70-
$this->_session = $request->session();
70+
$this->_session = $request->getSession();
7171
$this->setConfig($config);
7272
}
7373

src/Controller/Component/AuthComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function initialize(array $config)
254254
$controller = $this->_registry->getController();
255255
$this->eventManager($controller->eventManager());
256256
$this->response =& $controller->response;
257-
$this->session = $controller->request->session();
257+
$this->session = $controller->request->getSession();
258258
}
259259

260260
/**

src/Controller/Component/FlashComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FlashComponent extends Component
6060
public function __construct(ComponentRegistry $registry, array $config = [])
6161
{
6262
parent::__construct($registry, $config);
63-
$this->_session = $registry->getController()->request->session();
63+
$this->_session = $registry->getController()->request->getSession();
6464
}
6565

6666
/**

src/Controller/Component/SecurityComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SecurityComponent extends Component
100100
public function startup(Event $event)
101101
{
102102
$controller = $event->getSubject();
103-
$this->session = $controller->request->session();
103+
$this->session = $controller->request->getSession();
104104
$this->_action = $controller->request->getParam('action');
105105
$hasData = (bool)$controller->request->getData();
106106
try {

src/Http/ServerRequest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,40 @@ public function contentType()
514514
return $this->env('HTTP_CONTENT_TYPE');
515515
}
516516

517+
/**
518+
* Update the request with a new session instance.
519+
*
520+
* Returns an updated request object. This method returns
521+
* a *new* request object and does not mutate the request in-place.
522+
*
523+
* @param \Cake\Network\Session $session the session object to use
524+
* @return static
525+
*/
526+
public function withSession(Session $session)
527+
{
528+
$new = clone $this;
529+
$new->session = $session;
530+
531+
return $new;
532+
}
533+
534+
/**
535+
* Returns the instance of the Session object for this request
536+
*
537+
* @return \Cake\Network\Session
538+
*/
539+
public function getSession()
540+
{
541+
return $this->session;
542+
}
543+
517544
/**
518545
* Returns the instance of the Session object for this request
519546
*
520547
* If a session object is passed as first argument it will be set as
521548
* the session to use for this request
522549
*
550+
* @deprecated 3.5.0 Use getSession()/withSession() instead.
523551
* @param \Cake\Network\Session|null $session the session object to use
524552
* @return \Cake\Network\Session
525553
*/

src/View/Helper/FlashHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class FlashHelper extends Helper
7070
*/
7171
public function render($key = 'flash', array $options = [])
7272
{
73-
if (!$this->request->session()->check("Flash.$key")) {
73+
if (!$this->request->getSession()->check("Flash.$key")) {
7474
return null;
7575
}
7676

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

8686
$out = '';
8787
foreach ($flash as $message) {

src/View/Helper/SessionHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(View $View, array $config = [])
5454
*/
5555
public function read($name = null)
5656
{
57-
return $this->request->session()->read($name);
57+
return $this->request->getSession()->read($name);
5858
}
5959

6060
/**
@@ -70,7 +70,7 @@ public function read($name = null)
7070
*/
7171
public function check($name)
7272
{
73-
return $this->request->session()->check($name);
73+
return $this->request->getSession()->check($name);
7474
}
7575

7676
/**

tests/TestCase/Http/ServerRequestTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3178,6 +3178,35 @@ public function testSession()
31783178
$this->assertEquals($session, $request->session());
31793179
}
31803180

3181+
/**
3182+
* Tests getting the session from the request
3183+
*
3184+
* @return void
3185+
*/
3186+
public function testGetSession()
3187+
{
3188+
$session = new Session;
3189+
$request = new ServerRequest(['session' => $session]);
3190+
$this->assertSame($session, $request->getSession());
3191+
3192+
$request = ServerRequestFactory::fromGlobals();
3193+
$this->assertEquals($session, $request->getSession());
3194+
}
3195+
3196+
/**
3197+
* Tests setting the session to the request
3198+
*
3199+
* @return void
3200+
*/
3201+
public function testWithtSession()
3202+
{
3203+
$session = new Session;
3204+
$request = new ServerRequest();
3205+
$newRequest = $request->withSession($session);
3206+
$this->assertNotSame($newRequest, $request);
3207+
$this->assertSame($session, $newRequest->getSession());
3208+
}
3209+
31813210
/**
31823211
* Test the content type method.
31833212
*

0 commit comments

Comments
 (0)