diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 62ce948b6828..867ceba97f8d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -193,9 +193,7 @@ public function migrate($destroy = false, $lifetime = null) */ public function save() { - if ($this->isStarted()) { - $this->storage->save(); - } + $this->storage->save(); } /** diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php index c4dbe7586822..13d0a2034d5f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php @@ -219,6 +219,11 @@ public function regenerate($destroy = false, $lifetime = null) */ public function save() { + // In PHP <7.2 session_write_close() returns void if the session is not started. + // @see https://www.php.net/manual/function.session-write-close.php + if (!$this->started) { + return; + } $session = $_SESSION; foreach ($this->bags as $bag) { diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php index e75b3321b035..afa00fc7c304 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php @@ -260,14 +260,4 @@ public function testIsEmpty() $flash->get('hello'); $this->assertTrue($this->session->isEmpty()); } - - public function testSaveIfNotStarted() - { - $storage = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface')->getMock(); - $session = new Session($storage); - - $storage->expects($this->once())->method('isStarted')->willReturn(false); - $storage->expects($this->never())->method('save'); - $session->save(); - } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index 7cc2eb79c8db..25a2d8c9c17a 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -105,6 +105,13 @@ public function testGetId() $this->assertSame($id, $storage->getId(), 'ID stays after saving session'); } + public function testSaveIfNotStarted() + { + $storage = $this->getStorage(); + $this->assertNull($storage->save(), 'Saving unstarted sessions does not error'); + $this->assertEmpty(glob($this->savePath.'/*')); + } + public function testRegenerate() { $storage = $this->getStorage();