Переглянути джерело

Merge remote branch 'kriswallsmith/http/session-fixes'

* kriswallsmith/http/session-fixes:
  [HttpFoundation] updated session to only save on destruct if started
  [HttpFoundation] fixed locale accessor after session clear
  [HttpFoundation] removed unnecessary method
Fabien Potencier 14 роки тому
батько
коміт
fed6dc9a1e

+ 9 - 8
src/Symfony/Component/HttpFoundation/Session.php

@@ -36,7 +36,7 @@ class Session implements \Serializable
     {
         $this->storage = $storage;
         $this->defaultLocale = $defaultLocale;
-        $this->attributes = array('_flash' => array(), '_locale' => $this->getDefaultLocale());
+        $this->attributes = array('_flash' => array(), '_locale' => $this->defaultLocale);
         $this->started = false;
     }
 
@@ -58,7 +58,7 @@ class Session implements \Serializable
         }
 
         if (!isset($this->attributes['_locale'])) {
-            $this->attributes['_locale'] = $this->getDefaultLocale();
+            $this->attributes['_locale'] = $this->defaultLocale;
         }
 
         // flag current flash messages to be removed at shutdown
@@ -194,6 +194,10 @@ class Session implements \Serializable
      */
     public function getLocale()
     {
+        if (!isset($this->attributes['_locale'])) {
+            $this->attributes['_locale'] = $this->defaultLocale;
+        }
+
         return $this->attributes['_locale'];
     }
 
@@ -283,7 +287,9 @@ class Session implements \Serializable
 
     public function __destruct()
     {
-        $this->save();
+        if (true === $this->started) {
+            $this->save();
+        }
     }
 
     public function serialize()
@@ -297,9 +303,4 @@ class Session implements \Serializable
         $this->attributes = array();
         $this->started = false;
     }
-
-    private function getDefaultLocale()
-    {
-        return $this->defaultLocale;
-    }
 }

+ 6 - 0
tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php

@@ -172,6 +172,12 @@ class SessionTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
     }
 
+    public function testLocaleAfterClear()
+    {
+        $this->session->clear();
+        $this->assertEquals('en', $this->session->getLocale());
+    }
+
     public function testGetId()
     {
         $this->assertSame(null, $this->session->getId());