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

Skip to content

lazy session start for #6036 without bc #10310

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
30 changes: 30 additions & 0 deletions src/Symfony/Component/HttpFoundation/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpFoundation\Session;

use Symfony\Component\HttpFoundation\Session\Storage\LazySessionStorageInterface;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
Expand Down Expand Up @@ -78,6 +79,10 @@ public function start()
*/
public function has($name)
{
if (!$this->isExists()) {
return false;
}

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

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

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

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

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

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

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

Expand Down Expand Up @@ -248,4 +265,17 @@ public function getFlashBag()
{
return $this->getBag($this->flashName);
}

/**
* Is Session exists
*
* @return boolean
*/
private function isExists()
Copy link
Member

Choose a reason for hiding this comment

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

isExists is a weird name. You have 2 verbs in this name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any suggestions? I think about this name and haven't better name.

Copy link
Contributor

Choose a reason for hiding this comment

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

exists, has, isset, isDefined, ... ? It depends on what this method does

{
return
$this->isStarted()
|| !($this->storage instanceof LazySessionStorageInterface)
|| $this->storage->hasPreviousSession();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Session\Storage;

/**
* LazyMockArraySessionStorage mocks the session for unit tests.
*
* No PHP session is actually started since a session can be initialized
* and shutdown only once per PHP execution cycle.
*
* When doing functional testing, you should use MockFileSessionStorage instead.
*
* @author Fabien Potencier <[email protected]>
* @author Bulat Shakirzyanov <[email protected]>
* @author Drak <[email protected]>
*/
class LazyMockArraySessionStorage extends MockArraySessionStorage implements LazySessionStorageInterface
{
/**
* {@inheritdoc}
*/
public function hasPreviousSession()
{
return !empty($this->id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Session\Storage;

/**
* LazyStorageInterface.
*
* @author Fabien Potencier <[email protected]>
* @author Drak <[email protected]>
*
* @api
*/
interface LazySessionStorageInterface
{
/**
* Checks that storage has previous session
*
* @return boolean True if previous session exiting, false otherwise.
*/
public function hasPreviousSession();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Tests\Session;

use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Storage\LazyMockArraySessionStorage;

/**
* LazySessionTest
*
* @author Fabien Potencier <[email protected]>
* @author Robert Schönthal <[email protected]>
* @author Drak <[email protected]>
*/
class SessionLazyStartTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface
*/
protected $storage;

/**
* @var \Symfony\Component\HttpFoundation\Session\SessionInterface
*/
protected $session;

protected function setUp()
{
$this->storage = new LazyMockArraySessionStorage();
$this->session = new Session($this->storage, new AttributeBag(), new FlashBag());
}

protected function tearDown()
{
$this->storage = null;
$this->session = null;
}

public function testGet()
{
// tests defaults
$this->assertNull($this->session->get('foo'));
$this->assertEquals(1, $this->session->get('foo', 1));
$this->assertFalse($this->session->isStarted());
}

/**
* @dataProvider setProvider
*/
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());
}

public function testReplace()
{
$this->session->replace(array('happiness' => 'be good', 'symfony' => 'awesome'));
$this->assertTrue($this->session->isStarted());
}

/**
* @dataProvider setProvider
*/
public function testAll($key, $value, $result)
{
$this->assertEquals(array(), $this->session->all());
$this->session->all();
$this->assertFalse($this->session->isStarted());
}

/**
* @dataProvider setProvider
*/
public function testClear($key, $value)
{
$this->session->set('hi', 'fabien');
$this->session->set($key, $value);
$this->session->clear();
$this->assertEquals(array(), $this->session->all());
$this->assertTrue($this->session->isStarted());
}

public function setProvider()
{
return array(
array('foo', 'bar', array('foo' => 'bar')),
array('foo.bar', 'too much beer', array('foo.bar' => 'too much beer')),
array('great', 'symfony2 is great', array('great' => 'symfony2 is great')),
);
}

/**
* @dataProvider setProvider
*/
public function testRemove($key, $value)
{
$this->session->set('hi.world', 'have a nice day');
$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());
}

/**
* @covers \Symfony\Component\HttpFoundation\Session\Session::count
*/
public function testGetCount()
{
$this->assertEquals(0, count($this->session));
$this->assertFalse($this->session->isStarted());
}
}
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->assertTrue($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->assertTrue($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->assertTrue($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->assertTrue($this->session->isStarted());

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

Expand Down