Bläddra i källkod

[HttpFoundation] replaced the option argument of Session by defaultLocale

Fabien Potencier 14 år sedan
förälder
incheckning
f77b94074f

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

@@ -24,18 +24,18 @@ class Session implements \Serializable
     protected $attributes;
     protected $oldFlashes;
     protected $started;
-    protected $options;
+    protected $defaultLocale;
 
     /**
      * Constructor.
      *
-     * @param SessionStorageInterface $session A SessionStorageInterface instance
-     * @param array                   $options An array of options
+     * @param SessionStorageInterface $session       A SessionStorageInterface instance
+     * @param string                  $defaultLocale The default locale
      */
-    public function __construct(SessionStorageInterface $storage, array $options = array())
+    public function __construct(SessionStorageInterface $storage, $defaultLocale = 'en')
     {
         $this->storage = $storage;
-        $this->options = $options;
+        $this->defaultLocale = $defaultLocale;
         $this->attributes = array('_flash' => array(), '_locale' => $this->getDefaultLocale());
         $this->started = false;
     }
@@ -288,18 +288,18 @@ class Session implements \Serializable
 
     public function serialize()
     {
-        return serialize(array($this->storage, $this->options));
+        return serialize(array($this->storage, $this->defaultLocale));
     }
 
     public function unserialize($serialized)
     {
-        list($this->storage, $this->options) = unserialize($serialized);
+        list($this->storage, $this->defaultLocale) = unserialize($serialized);
         $this->attributes = array();
         $this->started = false;
     }
 
-    protected function getDefaultLocale()
+    private function getDefaultLocale()
     {
-        return isset($this->options['default_locale']) ? $this->options['default_locale'] : 'en';
+        return $this->defaultLocale;
     }
 }

+ 12 - 12
tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php

@@ -123,34 +123,34 @@ class SessionTest extends \PHPUnit_Framework_TestCase
 
     public function testSerialize()
     {
-        $options = array('foo' => 'bar');
-        $this->session = new Session($this->storage, $options);
+        $defaultLocale = 'en';
+        $this->session = new Session($this->storage, $defaultLocale);
 
-        $compare = serialize(array($this->storage, $options));
+        $compare = serialize(array($this->storage, $defaultLocale));
 
         $this->assertSame($compare, $this->session->serialize());
 
         $this->session->unserialize($compare);
 
-        $_options = new \ReflectionProperty(get_class($this->session), 'options');
-        $_options->setAccessible(true);
+        $_defaultLocale = new \ReflectionProperty(get_class($this->session), 'defaultLocale');
+        $_defaultLocale->setAccessible(true);
 
         $_storage = new \ReflectionProperty(get_class($this->session), 'storage');
         $_storage->setAccessible(true);
 
-        $this->assertEquals($_options->getValue($this->session), $options, 'options match');
+        $this->assertEquals($_defaultLocale->getValue($this->session), $defaultLocale, 'options match');
         $this->assertEquals($_storage->getValue($this->session), $this->storage, 'storage match');
     }
 
     public function testSave()
     {
         $this->storage = new ArraySessionStorage();
-        $options = array('foo' => 'bar');
-        $this->session = new Session($this->storage, $options);
+        $defaultLocale = 'fr';
+        $this->session = new Session($this->storage, $defaultLocale);
         $this->session->set('foo', 'bar');
 
         $this->session->save();
-        $compare = array('_symfony2' => array('_flash' => array() ,'_locale' => 'en', 'foo' => 'bar'));
+        $compare = array('_symfony2' => array('_flash' => array(), '_locale' => 'fr', 'foo' => 'bar'));
 
         $r = new \ReflectionObject($this->storage);
         $p = $r->getProperty('data');
@@ -161,15 +161,15 @@ class SessionTest extends \PHPUnit_Framework_TestCase
 
     public function testLocale()
     {
-        $this->assertSame('en', $this->session->getLocale(),'default locale is en');
+        $this->assertSame('en', $this->session->getLocale(), 'default locale is en');
 
         $this->session->set('_locale','de');
 
-        $this->assertSame('de', $this->session->getLocale(),'locale is de');
+        $this->assertSame('de', $this->session->getLocale(), 'locale is de');
 
         $this->session = $this->getSession();
         $this->session->setLocale('fr');
-        $this->assertSame('fr', $this->session->getLocale(),'locale is fr');
+        $this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
     }
 
     public function testGetId()