From e3ccc7947f4f664abfd01843827a745a20436a42 Mon Sep 17 00:00:00 2001 From: Evgeniy Sokolov Date: Wed, 29 Jan 2014 14:07:33 +0100 Subject: [PATCH] lazy session start for #6036 --- .../Controller/LocalizedController.php | 2 ++ .../Controller/LoginController.php | 2 ++ .../HttpFoundation/Session/Session.php | 16 ++++++++++++++++ .../Storage/MockArraySessionStorage.php | 8 ++++++++ .../Session/Storage/NativeSessionStorage.php | 8 ++++++++ .../Storage/SessionStorageInterface.php | 7 +++++++ .../Tests/Session/SessionTest.php | 18 ++++++++++++++++++ 7 files changed, 61 insertions(+) diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php index 381b13d7dd3cc..d3db97016e8f4 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LocalizedController.php @@ -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); diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LoginController.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LoginController.php index cec298c8916d0..d48cda1b76d11 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LoginController.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/FormLoginBundle/Controller/LoginController.php @@ -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); diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index ac626fd58998d..a238b59b6618e 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -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); } @@ -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); } @@ -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(); } @@ -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()); } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php index 8b1c2137f6a8a..35b0dc9fa7c99 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockArraySessionStorage.php @@ -215,6 +215,14 @@ public function isStarted() return $this->started; } + /** + * {@inheritdoc} + */ + public function hasPreviousSession() + { + return !empty($this->id); + } + /** * Sets the MetadataBag. * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index 083df9de40f9d..acaa16812cdf0 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -316,6 +316,14 @@ public function isStarted() return $this->started; } + /** + * {@inheritdoc} + */ + public function hasPreviousSession() + { + return isset($_COOKIE[session_name()]); + } + /** * Sets session.* ini variables. * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php index 74f19c5216dcb..f65b397234598 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/SessionStorageInterface.php @@ -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(); + /** * Gets a SessionBagInterface by name. * diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index 9d5ad352845bf..64210d3c3e936 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -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()); } /** @@ -91,6 +92,7 @@ public function testSet($key, $value) { $this->session->set($key, $value); $this->assertEquals($value, $this->session->get($key)); + $this->assertTrue($this->session->isStarted()); } /** @@ -98,6 +100,9 @@ public function testSet($key, $value) */ 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')); @@ -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()); @@ -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()); } @@ -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() @@ -149,6 +160,7 @@ 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() @@ -156,6 +168,7 @@ 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() @@ -163,6 +176,7 @@ 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() @@ -170,6 +184,7 @@ 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() @@ -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');