File tree 2 files changed +44
-13
lines changed
src/Symfony/Component/HttpFoundation
2 files changed +44
-13
lines changed Original file line number Diff line number Diff line change @@ -102,21 +102,28 @@ class NativeSessionStorage implements SessionStorageInterface
102
102
*/
103
103
public function __construct (array $ options = array (), $ handler = null , MetadataBag $ metaBag = null )
104
104
{
105
- $ options += array (
106
- // disable by default because it's managed by HeaderBag (if used)
107
- 'cache_limiter ' => '' ,
108
- 'use_cookies ' => 1 ,
109
- );
110
-
111
- if (\PHP_VERSION_ID >= 50400 ) {
112
- session_register_shutdown ();
105
+ $ this ->setMetadataBag ($ metaBag );
106
+
107
+ if (\PHP_SESSION_ACTIVE === session_status () && \PHP_VERSION_ID >= 70200 ) {
108
+ if (!empty ($ options ) || null !== $ handler ) {
109
+ throw new \LogicException ('Cannot change options or handler of an active session ' );
110
+ }
113
111
} else {
114
- register_shutdown_function ('session_write_close ' );
115
- }
112
+ $ options += array (
113
+ // disable by default because it's managed by HeaderBag (if used)
114
+ 'cache_limiter ' => '' ,
115
+ 'use_cookies ' => 1 ,
116
+ );
116
117
117
- $ this ->setMetadataBag ($ metaBag );
118
- $ this ->setOptions ($ options );
119
- $ this ->setSaveHandler ($ handler );
118
+ if (\PHP_VERSION_ID >= 50400 ) {
119
+ session_register_shutdown ();
120
+ } else {
121
+ register_shutdown_function ('session_write_close ' );
122
+ }
123
+
124
+ $ this ->setOptions ($ options );
125
+ $ this ->setSaveHandler ($ handler );
126
+ }
120
127
}
121
128
122
129
/**
Original file line number Diff line number Diff line change @@ -272,4 +272,28 @@ public function testRestart()
272
272
$ this ->assertSame ($ id , $ storage ->getId (), 'Same session ID after restarting ' );
273
273
$ this ->assertSame (7 , $ storage ->getBag ('attributes ' )->get ('lucky ' ), 'Data still available ' );
274
274
}
275
+
276
+ public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted ()
277
+ {
278
+ session_start ();
279
+ $ this ->getStorage ();
280
+
281
+ // Assert no exception has been thrown by `getStorage()`
282
+ $ this ->assertTrue (true );
283
+ }
284
+
285
+ /**
286
+ * @expectedException \LogicException
287
+ */
288
+ public function testCannotSetSessionOptionsOnceSessionStarted ()
289
+ {
290
+ if (PHP_VERSION_ID < 70200 ) {
291
+ $ this ->markTestSkipped ('Limitation from PHP 7.2 only ' );
292
+ }
293
+
294
+ session_start ();
295
+ $ this ->getStorage (array (
296
+ 'name ' => 'something-else ' ,
297
+ ));
298
+ }
275
299
}
You can’t perform that action at this time.
0 commit comments