Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d04017

Browse files
committed
Just skip PHP's session_set_save_handler, not the full setSaveHandler method
1 parent 99f8d85 commit 9d04017

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ class NativeSessionStorage implements SessionStorageInterface
103103
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
104104
{
105105
$this->setMetadataBag($metaBag);
106+
$this->setSaveHandler($handler);
106107

107-
if (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
108+
if (!$this->canUpdatePhpSession()) {
108109
return;
109110
}
110111

@@ -121,7 +122,6 @@ public function __construct(array $options = array(), $handler = null, MetadataB
121122
}
122123

123124
$this->setOptions($options);
124-
$this->setSaveHandler($handler);
125125
}
126126

127127
/**
@@ -383,6 +383,7 @@ public function setOptions(array $options)
383383
* @see http://github.com/drak/NativeSession
384384
*
385385
* @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $saveHandler
386+
* @param bool $updatePhpHandler
386387
*
387388
* @throws \InvalidArgumentException
388389
*/
@@ -408,7 +409,7 @@ public function setSaveHandler($saveHandler = null)
408409
}
409410
$this->saveHandler = $saveHandler;
410411

411-
if ($this->saveHandler instanceof \SessionHandlerInterface) {
412+
if ($this->saveHandler instanceof \SessionHandlerInterface && $this->canUpdatePhpSession()) {
412413
if (\PHP_VERSION_ID >= 50400) {
413414
session_set_save_handler($this->saveHandler, false);
414415
} else {
@@ -449,4 +450,14 @@ protected function loadSession(array &$session = null)
449450
$this->started = true;
450451
$this->closed = false;
451452
}
453+
454+
/**
455+
* Return true if we can update PHP's session.
456+
*
457+
* @return bool
458+
*/
459+
private function canUpdatePhpSession(): bool
460+
{
461+
return \PHP_VERSION_ID <= 50400 || \PHP_SESSION_ACTIVE !== session_status();
462+
}
452463
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,19 @@ public function testSetSessionOptionsOnceSessionStartedIsIgnored()
296296
// Assert no exception has been thrown by `getStorage()`
297297
$this->addToAssertionCount(1);
298298
}
299+
300+
/**
301+
* @requires PHP 5.4
302+
*/
303+
public function testGetBagsOnceSessionStartedIsIgnored()
304+
{
305+
session_start();
306+
$bag = new AttributeBag();
307+
$bag->setName('flashes');
308+
309+
$storage = $this->getStorage();
310+
$storage->registerBag($bag);
311+
312+
$this->assertEquals($storage->getBag('flashes'), $bag);
313+
}
299314
}

0 commit comments

Comments
 (0)