Browse Source

Split ServerRequest::session() into getter/setter

Michael Hoffmann 9 years ago
parent
commit
7946bb72ac

+ 1 - 1
src/Auth/Storage/SessionStorage.php

@@ -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);
     }
 

+ 1 - 1
src/Controller/Component/AuthComponent.php

@@ -254,7 +254,7 @@ class AuthComponent extends Component
         $controller = $this->_registry->getController();
         $this->eventManager($controller->eventManager());
         $this->response =& $controller->response;
-        $this->session = $controller->request->session();
+        $this->session = $controller->request->getSession();
     }
 
     /**

+ 1 - 1
src/Controller/Component/FlashComponent.php

@@ -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();
     }
 
     /**

+ 1 - 1
src/Controller/Component/SecurityComponent.php

@@ -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 {

+ 28 - 0
src/Http/ServerRequest.php

@@ -515,11 +515,39 @@ class ServerRequest implements ArrayAccess, ServerRequestInterface
     }
 
     /**
+     * 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
      */

+ 2 - 2
src/View/Helper/FlashHelper.php

@@ -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;
         }
 
@@ -81,7 +81,7 @@ class FlashHelper extends Helper
                 $key
             ));
         }
-        $this->request->session()->delete("Flash.$key");
+        $this->request->getSession()->delete("Flash.$key");
 
         $out = '';
         foreach ($flash as $message) {

+ 2 - 2
src/View/Helper/SessionHelper.php

@@ -54,7 +54,7 @@ class SessionHelper extends Helper
      */
     public function read($name = null)
     {
-        return $this->request->session()->read($name);
+        return $this->request->getSession()->read($name);
     }
 
     /**
@@ -70,7 +70,7 @@ class SessionHelper extends Helper
      */
     public function check($name)
     {
-        return $this->request->session()->check($name);
+        return $this->request->getSession()->check($name);
     }
 
     /**

+ 29 - 0
tests/TestCase/Http/ServerRequestTest.php

@@ -3179,6 +3179,35 @@ XML;
     }
 
     /**
+     * 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.
      *
      * @return void