Skip to content

Commit ff58ec8

Browse files
bug symfony#24531 [HttpFoundation] Fix forward-compat of NativeSessionStorage with PHP 7.2 (sroze)
This PR was merged into the 2.7 branch. Discussion ---------- [HttpFoundation] Fix forward-compat of NativeSessionStorage with PHP 7.2 | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#24524 | License | MIT | Doc PR | ø PHP 7.2 disallow setting session options when the session was already started. This PR will not set any option if the session is already started and throw an exception if trying to do so with custom options. Commits ------- 00a1357 [HttpFoundation] Fix forward-compat of NativeSessionStorage with PHP 7.2
2 parents efb4891 + 00a1357 commit ff58ec8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ class NativeSessionStorage implements SessionStorageInterface
102102
*/
103103
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
104104
{
105+
$this->setMetadataBag($metaBag);
106+
107+
if (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
108+
return;
109+
}
110+
105111
$options += array(
106112
// disable by default because it's managed by HeaderBag (if used)
107113
'cache_limiter' => '',
@@ -114,7 +120,6 @@ public function __construct(array $options = array(), $handler = null, MetadataB
114120
register_shutdown_function('session_write_close');
115121
}
116122

117-
$this->setMetadataBag($metaBag);
118123
$this->setOptions($options);
119124
$this->setSaveHandler($handler);
120125
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,30 @@ public function testRestart()
270270
$this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
271271
$this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
272272
}
273+
274+
/**
275+
* @requires PHP 5.4
276+
*/
277+
public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted()
278+
{
279+
session_start();
280+
$this->getStorage();
281+
282+
// Assert no exception has been thrown by `getStorage()`
283+
$this->addToAssertionCount(1);
284+
}
285+
286+
/**
287+
* @requires PHP 5.4
288+
*/
289+
public function testSetSessionOptionsOnceSessionStartedIsIgnored()
290+
{
291+
session_start();
292+
$this->getStorage(array(
293+
'name' => 'something-else',
294+
));
295+
296+
// Assert no exception has been thrown by `getStorage()`
297+
$this->addToAssertionCount(1);
298+
}
273299
}

0 commit comments

Comments
 (0)