From 00a1357d823bb55373dceb103699bdf448decbad Mon Sep 17 00:00:00 2001
From: Samuel ROZE <samuel.roze@gmail.com>
Date: Thu, 12 Oct 2017 11:48:23 +0100
Subject: [PATCH] [HttpFoundation] Fix forward-compat of NativeSessionStorage
 with PHP 7.2

---
 .../Session/Storage/NativeSessionStorage.php  |  7 ++++-
 .../Storage/NativeSessionStorageTest.php      | 26 +++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php
index 76654d26fd8..e35af4e291a 100644
--- a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php
+++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php
@@ -102,6 +102,12 @@ class NativeSessionStorage implements SessionStorageInterface
      */
     public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
     {
+        $this->setMetadataBag($metaBag);
+
+        if (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
+            return;
+        }
+
         $options += array(
             // disable by default because it's managed by HeaderBag (if used)
             'cache_limiter' => '',
@@ -114,7 +120,6 @@ class NativeSessionStorage implements SessionStorageInterface
             register_shutdown_function('session_write_close');
         }
 
-        $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 7eda5b3a3d2..673cd386bf6 100644
--- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php
+++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php
@@ -270,4 +270,30 @@ 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');
     }
+
+    /**
+     * @requires PHP 5.4
+     */
+    public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted()
+    {
+        session_start();
+        $this->getStorage();
+
+        // Assert no exception has been thrown by `getStorage()`
+        $this->addToAssertionCount(1);
+    }
+
+    /**
+     * @requires PHP 5.4
+     */
+    public function testSetSessionOptionsOnceSessionStartedIsIgnored()
+    {
+        session_start();
+        $this->getStorage(array(
+            'name' => 'something-else',
+        ));
+
+        // Assert no exception has been thrown by `getStorage()`
+        $this->addToAssertionCount(1);
+    }
 }
-- 
GitLab