Quellcode durchsuchen

[HttpFoundation] changed the strategy introduced in a5ccda47b4406518ee75929ce2e690b6998c021b to fix functional tests and still allow to call save more than once for a Session

Fabien Potencier vor 13 Jahren
Ursprung
Commit
27ba003e5e

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/EventListener/TestSessionListener.php

@@ -69,6 +69,7 @@ class TestSessionListener
 
         if ($session = $event->getRequest()->getSession()) {
             $session->save();
+            $session->close();
 
             $params = session_get_cookie_params();
 

+ 14 - 4
src/Symfony/Component/HttpFoundation/Session.php

@@ -29,7 +29,7 @@ class Session implements \Serializable
     protected $oldFlashes;
     protected $locale;
     protected $defaultLocale;
-    protected $saved;
+    protected $closed;
 
     /**
      * Constructor.
@@ -47,7 +47,7 @@ class Session implements \Serializable
         $this->attributes = array();
         $this->setPhpDefaultLocale($this->defaultLocale);
         $this->started = false;
-        $this->saved = false;
+        $this->closed = false;
     }
 
     /**
@@ -358,12 +358,22 @@ class Session implements \Serializable
             'flashes'    => $this->flashes,
             'locale'     => $this->locale,
         ));
-        $this->saved = true;
+    }
+
+    /**
+     * This method should be called when you don't want the session to be saved
+     * when the Session object is garbaged collected (useful for instance when
+     * you want to simulate the interaction of several users/sessions in a single
+     * PHP process).
+     */
+    public function close()
+    {
+        $this->closed = true;
     }
 
     public function __destruct()
     {
-        if (true === $this->started && !$this->saved) {
+        if (true === $this->started && !$this->closed) {
             $this->save();
         }
     }