8000 Ensure we can create the native session storage with PHP 7.2 · symfony/symfony@426a0cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 426a0cb

Browse files
committed
Ensure we can create the native session storage with PHP 7.2
1 parent 5e0bb5f commit 426a0cb

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,28 @@ class NativeSessionStorage implements SessionStorageInterface
102102
*/
103103
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
104104
{
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+
}
113111
} 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+
);
116117

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+
}
120127
}
121128

122129
/**

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,28 @@ public function testRestart()
272272
$this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
273273
$this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
274274
}
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+
}
275299
}

0 commit comments

Comments
 (0)
0