From ecf54e1d7ccad2903734f5471a2833b334b24b8f Mon Sep 17 00:00:00 2001 From: Markus Fasselt Date: Tue, 6 Oct 2015 00:22:44 +0200 Subject: [PATCH 1/3] [HttpFoundation] fixed bag registration after session start --- .../Session/Storage/NativeSessionStorage.php | 26 ++++++++++++++++--- .../Storage/NativeSessionStorageTest.php | 15 +++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index 31bf55c72dd99..56d9ee548c82f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -264,6 +264,10 @@ public function clear() public function registerBag(SessionBagInterface $bag) { $this->bags[$bag->getName()] = $bag; + + if ($this->isStarted()) { + $this->loadSessionBag($bag); + } } /** @@ -422,12 +426,28 @@ protected function loadSession(array &$session = null) $bags = array_merge($this->bags, array($this->metadataBag)); foreach ($bags as $bag) { - $key = $bag->getStorageKey(); - $session[$key] = isset($session[$key]) ? $session[$key] : array(); - $bag->initialize($session[$key]); + $this->loadSessionBag($bag, $session); } $this->started = true; $this->closed = false; } + + /** + * Load and initialize the session for the given bag and pass the + * session variable into the bag. + * + * @param SessionBagInterface $bag + * @param array|null $session + */ + protected function loadSessionBag(SessionBagInterface $bag, array &$session = null) + { + if (null === $session) { + $session = &$_SESSION; + } + + $key = $bag->getStorageKey(); + $session[$key] = isset($session[$key]) ? $session[$key] : array(); + $bag->initialize($session[$key]); + } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index 4870115caa64c..29d1920587bc7 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -254,4 +254,19 @@ public function testCanStartOutside54() $this->assertFalse(isset($_SESSION[$key])); $storage->start(); } + + public function testRegisterBagAfterStarting() + { + $storage = $this->getStorage(); + $storage->start(); + + // this is done after session start, as the session handler overwrites $_SESSION + $_SESSION['test_bag']['some_key'] = 'some_value'; + + $bag = new AttributeBag('test_bag'); + $bag->setName('test_bag'); + $storage->registerBag($bag); + + $this->assertSame('some_value', $bag->get('some_key')); + } } From db274e27f4e000e2e927ae6f92256e7007dcba70 Mon Sep 17 00:00:00 2001 From: Markus Fasselt Date: Mon, 18 Jan 2016 17:51:32 +0100 Subject: [PATCH 2/3] Add further test case for the native session storage --- .../Storage/NativeSessionStorageTest.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index 29d1920587bc7..42974c94a0f0b 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -255,12 +255,27 @@ public function testCanStartOutside54() $storage->start(); } + public function testRegisterBagBeforeStarting() + { + $storage = $this->getStorage(); + $bag = new AttributeBag('test_bag'); + $bag->setName('test_bag'); + $storage->registerBag($bag); + + $storage->start(); + + // variable is set after session start, as the session handler overwrites $_SESSION + $_SESSION['test_bag']['some_key'] = 'some_value'; + + $this->assertSame('some_value', $bag->get('some_key')); + } + public function testRegisterBagAfterStarting() { $storage = $this->getStorage(); $storage->start(); - // this is done after session start, as the session handler overwrites $_SESSION + // variable is set after session start, as the session handler overwrites $_SESSION $_SESSION['test_bag']['some_key'] = 'some_value'; $bag = new AttributeBag('test_bag'); From c54d1a00d51d7ffa64c0688b504d6151acac77df Mon Sep 17 00:00:00 2001 From: Markus Fasselt Date: Mon, 25 Jan 2016 12:39:56 +0100 Subject: [PATCH 3/3] Make loadSessionBag private --- .../HttpFoundation/Session/Storage/NativeSessionStorage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index 56d9ee548c82f..90809bbc2629b 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -440,7 +440,7 @@ protected function loadSession(array &$session = null) * @param SessionBagInterface $bag * @param array|null $session */ - protected function loadSessionBag(SessionBagInterface $bag, array &$session = null) + private function loadSessionBag(SessionBagInterface $bag, array &$session = null) { if (null === $session) { $session = &$_SESSION;