diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index b1d5b11f48..04eb68cfb4 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -96,6 +96,12 @@ class NativeSessionStorage implements SessionStorageInterface */ public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null) { + $this->setMetadataBag($metaBag); + + if (\PHP_SESSION_ACTIVE === session_status()) { + return; + } + $options += array( 'cache_limiter' => 'private_no_expire', 'cache_expire' => 0, @@ -106,7 +112,6 @@ class NativeSessionStorage implements SessionStorageInterface session_register_shutdown(); - $this->setMetadataBag($metaBag); $this->setOptions($options); $this->setSaveHandler($handler); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index e5b080d8a4..8864bb7069 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -242,4 +242,24 @@ class NativeSessionStorageTest extends TestCase $this->assertSame($id, $storage->getId(), 'Same session ID after restarting'); $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available'); } + + public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted() + { + session_start(); + $this->getStorage(); + + // Assert no exception has been thrown by `getStorage()` + $this->addToAssertionCount(1); + } + + public function testSetSessionOptionsOnceSessionStartedIsIgnored() + { + session_start(); + $this->getStorage(array( + 'name' => 'something-else', + )); + + // Assert no exception has been thrown by `getStorage()` + $this->addToAssertionCount(1); + } }