Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] [Session] lazy session start for #6036 #10156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class LocalizedController extends ContainerAware
{
public function loginAction()
{
$this->container->get('request')->getSession()->start();

// get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class LoginController extends ContainerAware
{
public function loginAction()
{
$this->container->get('request')->getSession()->start();

// get the login error if there is one
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function start()
*/
public function has($name)
{
if (!$this->isStarted() && !$this->storage->hasPreviousSession()) {
return false;
}

return $this->storage->getBag($this->attributeName)->has($name);
}

Expand All @@ -86,6 +90,10 @@ public function has($name)
*/
public function get($name, $default = null)
{
if (!$this->isStarted() && !$this->storage->hasPreviousSession()) {
return $default;
}

return $this->storage->getBag($this->attributeName)->get($name, $default);
}

Expand All @@ -102,6 +110,10 @@ public function set($name, $value)
*/
public function all()
{
if (!$this->isStarted() && !$this->storage->hasPreviousSession()) {
return array();
}

return $this->storage->getBag($this->attributeName)->all();
}

Expand Down Expand Up @@ -154,6 +166,10 @@ public function getIterator()
*/
public function count()
{
if (!$this->isStarted() && !$this->storage->hasPreviousSession()) {
return 0;
}

return count($this->storage->getBag($this->attributeName)->all());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ public function isStarted()
return $this->started;
}

/**
* {@inheritdoc}
*/
public function hasPreviousSession()
{
return !empty($this->id);
}

/**
* Sets the MetadataBag.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ public function isStarted()
return $this->started;
}

/**
* {@inheritdoc}
*/
public function hasPreviousSession()
{
return isset($_COOKIE[session_name()]);
}

/**
* Sets session.* ini variables.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public function save();
*/
public function clear();

/**
* Checks that storage has previous session
*
* @return boolean True if previous session exiting, false otherwise.
*/
public function hasPreviousSession();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SessionStorageInterface is marked as API so it can't be changed.

Its best to introduce a new additional Interface to solve this - LazySessionStorageInterface?
And then check if the storage implements this interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #10310 I make it in this way.


/**
* Gets a SessionBagInterface by name.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function testGet()
// tests defaults
$this->assertNull($this->session->get('foo'));
$this->assertEquals(1, $this->session->get('foo', 1));
$this->assertFalse($this->session->isStarted());
}

/**
Expand All @@ -91,13 +92,17 @@ public function testSet($key, $value)
{
$this->session->set($key, $value);
$this->assertEquals($value, $this->session->get($key));
$this->assertTrue($this->session->isStarted());
}

/**
* @dataProvider setProvider
*/
public function testHas($key, $value)
{
$this->assertFalse($this->session->has($key.'_lazy_start'));
$this->assertFalse($this->session->isStarted());

$this->session->set($key, $value);
$this->assertTrue($this->session->has($key));
$this->assertFalse($this->session->has($key.'non_value'));
Expand All @@ -106,6 +111,7 @@ public function testHas($key, $value)
public function testReplace()
{
$this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
$this->assertTrue($this->session->isStarted());
$this->assertEquals(array('happiness' => 'be good', 'symfony' => 'awesome'), $this->session->all());
$this->session->replace(array());
$this->assertEquals(array(), $this->session->all());
Expand All @@ -116,6 +122,10 @@ public function testReplace()
*/
public function testAll($key, $value, $result)
{
$this->assertEquals(array(), $this->session->all());
$this->session->all();
$this->assertFalse($this->session->isStarted());

$this->session->set($key, $value);
$this->assertEquals($result, $this->session->all());
}
Expand All @@ -129,6 +139,7 @@ public function testClear($key, $value)
$this->session->set($key, $value);
$this->session->clear();
$this->assertEquals(array(), $this->session->all());
$this->assertTrue($this->session->isStarted());
}

public function setProvider()
Expand All @@ -149,27 +160,31 @@ public function testRemove($key, $value)
$this->session->set($key, $value);
$this->session->remove($key);
$this->assertEquals(array('hi.world' => 'have a nice day'), $this->session->all());
$this->assertTrue($this->session->isStarted());
}

public function testInvalidate()
{
$this->session->set('invalidate', 123);
$this->session->invalidate();
$this->assertEquals(array(), $this->session->all());
$this->assertTrue($this->session->isStarted());
}

public function testMigrate()
{
$this->session->set('migrate', 321);
$this->session->migrate();
$this->assertEquals(321, $this->session->get('migrate'));
$this->assertTrue($this->session->isStarted());
}

public function testMigrateDestroy()
{
$this->session->set('migrate', 333);
$this->session->migrate(true);
$this->assertEquals(333, $this->session->get('migrate'));
$this->assertTrue($this->session->isStarted());
}

public function testSave()
Expand Down Expand Up @@ -214,6 +229,9 @@ public function testGetIterator()
*/
public function testGetCount()
{
$this->assertEquals(0, count($this->session));
$this->assertFalse($this->session->isStarted());

$this->session->set('hello', 'world');
$this->session->set('symfony2', 'rocks');

Expand Down