Skip to content

Commit

Permalink
[HttpFoundation] refactored Session
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 15, 2011
1 parent 570db76 commit 1467a9b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 53 deletions.
Expand Up @@ -53,8 +53,6 @@ public function __construct(Session $session, $secret)
*/
protected function getSessionId()
{
$this->session->start();

return $this->session->getId();
}
}
87 changes: 47 additions & 40 deletions src/Symfony/Component/HttpFoundation/Session.php
Expand Up @@ -21,9 +21,11 @@
class Session implements \Serializable
{
protected $storage;
protected $started;
protected $attributes;
protected $flashes;
protected $oldFlashes;
protected $started;
protected $locale;
protected $defaultLocale;

/**
Expand All @@ -36,8 +38,11 @@ public function __construct(SessionStorageInterface $storage, $defaultLocale = '
{
$this->storage = $storage;
$this->defaultLocale = $defaultLocale;
$this->attributes = array('_flash' => array(), '_locale' => $this->defaultLocale);
\Locale::setDefault($this->attributes['_locale']);
$this->locale = $defaultLocale;
$this->flashes = array();
$this->oldFlashes = array();
$this->attributes = array();
$this->setPhpDefaultLocale($this->defaultLocale);
$this->started = false;
}

Expand All @@ -52,21 +57,18 @@ public function start()

$this->storage->start();

$this->attributes = $this->storage->read('_symfony2');
$attributes = $this->storage->read('_symfony2');

if (!isset($this->attributes['_flash'])) {
$this->attributes['_flash'] = array();
}
if (isset($attributes['attributes'])) {
$this->attributes = $attributes['attributes'];
$this->flashes = $attributes['flashes'];
$this->locale = $attributes['locale'];
$this->setPhpDefaultLocale($this->locale);

if (!isset($this->attributes['_locale'])) {
$this->attributes['_locale'] = $this->defaultLocale;
// flag current flash messages to be removed at shutdown
$this->oldFlashes = array_flip(array_keys($this->flashes));
}

\Locale::setDefault($this->attributes['_locale']);

// flag current flash messages to be removed at shutdown
$this->oldFlashes = array_flip(array_keys($this->attributes['_flash']));

$this->started = true;
}

Expand Down Expand Up @@ -107,11 +109,7 @@ public function set($name, $value)
$this->start();
}

if ('_locale' === $name) {
$this->setLocale($value);
} else {
$this->attributes[$name] = $value;
}
$this->attributes[$name] = $value;
}

/**
Expand All @@ -135,10 +133,6 @@ public function setAttributes(array $attributes)
$this->start();
}

if (isset($attributes['_locale'])) {
$this->setLocale($attributes['_locale']);
}

$this->attributes = $attributes;
}

Expand Down Expand Up @@ -168,6 +162,8 @@ public function clear()
}

$this->attributes = array();
$this->flashes = array();
$this->setPhpDefaultLocale($this->locale = $this->defaultLocale);
}

/**
Expand Down Expand Up @@ -195,6 +191,10 @@ public function migrate()
*/
public function getId()
{
if (false === $this->started) {
$this->start();
}

return $this->storage->getId();
}

Expand All @@ -205,11 +205,7 @@ public function getId()
*/
public function getLocale()
{
if (!isset($this->attributes['_locale'])) {
$this->attributes['_locale'] = $this->defaultLocale;
}

return $this->attributes['_locale'];
return $this->locale;
}

/**
Expand All @@ -223,7 +219,7 @@ public function setLocale($locale)
$this->start();
}

\Locale::setDefault($this->attributes['_locale'] = $locale);
$this->setPhpDefaultLocale($this->locale = $locale);
}

/**
Expand All @@ -233,7 +229,7 @@ public function setLocale($locale)
*/
public function getFlashes()
{
return isset($this->attributes['_flash']) ? $this->attributes['_flash'] : array();
return $this->flashes;
}

/**
Expand All @@ -247,7 +243,7 @@ public function setFlashes($values)
$this->start();
}

$this->attributes['_flash'] = $values;
$this->flashes = $values;
$this->oldFlashes = array();
}

Expand All @@ -261,7 +257,7 @@ public function setFlashes($values)
*/
public function getFlash($name, $default = null)
{
return array_key_exists($name, $this->getFlashes()) ? $this->attributes['_flash'][$name] : $default;
return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default;
}

/**
Expand All @@ -276,7 +272,7 @@ public function setFlash($name, $value)
$this->start();
}

$this->attributes['_flash'][$name] = $value;
$this->flashes[$name] = $value;
unset($this->oldFlashes[$name]);
}

Expand All @@ -293,7 +289,7 @@ public function hasFlash($name)
$this->start();
}

return array_key_exists($name, $this->attributes['_flash']);
return array_key_exists($name, $this->flashes);
}

/**
Expand All @@ -307,7 +303,7 @@ public function removeFlash($name)
$this->start();
}

unset($this->attributes['_flash'][$name]);
unset($this->flashes[$name]);
}

/**
Expand All @@ -319,7 +315,7 @@ public function clearFlashes()
$this->start();
}

$this->attributes['_flash'] = array();
$this->flashes = array();
$this->oldFlashes = array();
}

Expand All @@ -329,11 +325,13 @@ public function save()
$this->start();
}

if (isset($this->attributes['_flash'])) {
$this->attributes['_flash'] = array_diff_key($this->attributes['_flash'], $this->oldFlashes);
}
$this->flashes = array_diff_key($this->flashes, $this->oldFlashes);

$this->storage->write('_symfony2', $this->attributes);
$this->storage->write('_symfony2', array(
'attributes' => $this->attributes,
'flashes' => $this->flashes,
'locale' => $this->locale,
));
}

public function __destruct()
Expand All @@ -354,4 +352,13 @@ public function unserialize($serialized)
$this->attributes = array();
$this->started = false;
}

private function setPhpDefaultLocale($locale)
{
try {
\Locale::setDefault($this->locale);
} catch (\Exception $e) {
// means that intl is not installed.
}
}
}
14 changes: 3 additions & 11 deletions tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php
Expand Up @@ -150,7 +150,7 @@ public function testSave()
$this->session->set('foo', 'bar');

$this->session->save();
$compare = array('_symfony2' => array('_flash' => array(), '_locale' => 'fr', 'foo' => 'bar'));
$compare = array('_symfony2' => array('attributes' => array('foo' => 'bar'), 'flashes' => array(), 'locale' => 'fr'));

$r = new \ReflectionObject($this->storage);
$p = $r->getProperty('data');
Expand All @@ -164,18 +164,14 @@ public function testLocale()
$this->assertSame('en', $this->session->getLocale(), 'default locale is en');
$this->assertSame('en', \Locale::getDefault(), '\Locale::getDefault() is en');

$this->session->set('_locale','de');
$this->session->setLocale('de');
$this->assertSame('de', $this->session->getLocale(), 'locale is de');
$this->assertSame('de', \Locale::getDefault(), '\Locale::getDefault() is de');

$this->session = $this->getSession();
$this->session->setLocale('fr');
$this->assertSame('fr', $this->session->getLocale(), 'locale is fr');
$this->assertSame('fr', \Locale::getDefault(), '\Locale::getDefault() is fr');

$this->session->setAttributes(array('_locale' => 'de'));
$this->assertSame('de', $this->session->getLocale(), 'locale is de');
$this->assertSame('de', \Locale::getDefault(), '\Locale::getDefault() is de');
}

public function testLocaleAfterClear()
Expand All @@ -197,11 +193,7 @@ public function testStart()
$this->assertSame('en', \Locale::getDefault());

$this->assertSame(array(), $this->session->getFlashes());
$this->assertSame(array('_flash' => array(), '_locale' => 'en'), $this->session->getAttributes());

$this->session->start();
$this->assertSame('en', $this->session->getLocale());
$this->assertSame('en', \Locale::getDefault());
$this->assertSame(array(), $this->session->getAttributes());
}

protected function getSession()
Expand Down

0 comments on commit 1467a9b

Please sign in to comment.