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

Skip to content

Commit 70dd46b

Browse files
bug #24952 [HttpFoundation] Fix session-related BC break (nicolas-grekas, sroze)
This PR was merged into the 2.7 branch. Discussion ---------- [HttpFoundation] Fix session-related BC break | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24941, #24934, #24947 and #24946 | License | MIT | Doc PR | - Conservative fix. Commits ------- 38186aa [HttpFoundation] Add test 3eaa188 [HttpFoundation] Fix session-related BC break
2 parents 99f8d85 + 38186aa commit 70dd46b

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ 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-
111105
$options += array(
112106
// disable by default because it's managed by HeaderBag (if used)
113107
'cache_limiter' => '',
@@ -120,6 +114,7 @@ public function __construct(array $options = array(), $handler = null, MetadataB
120114
register_shutdown_function('session_write_close');
121115
}
122116

117+
$this->setMetadataBag($metaBag);
123118
$this->setOptions($options);
124119
$this->setSaveHandler($handler);
125120
}
@@ -292,7 +287,7 @@ public function getBag($name)
292287
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
293288
}
294289

295-
if ($this->saveHandler->isActive() && !$this->started) {
290+
if (!$this->started && $this->saveHandler->isActive()) {
296291
$this->loadSession();
297292
} elseif (!$this->started) {
298293
$this->start();
@@ -340,7 +335,7 @@ public function isStarted()
340335
*/
341336
public function setOptions(array $options)
342337
{
343-
if (headers_sent()) {
338+
if (headers_sent() || (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status())) {
344339
return;
345340
}
346341

@@ -395,10 +390,6 @@ public function setSaveHandler($saveHandler = null)
395390
throw new \InvalidArgumentException('Must be instance of AbstractProxy or NativeSessionHandler; implement \SessionHandlerInterface; or be null.');
396391
}
397392

398-
if (headers_sent($file, $line)) {
399-
throw new \RuntimeException(sprintf('Failed to set the session handler because headers have already been sent by "%s" at line %d.', $file, $line));
400-
}
401-
402393
// Wrap $saveHandler in proxy and prevent double wrapping of proxy
403394
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
404395
$saveHandler = new SessionHandlerProxy($saveHandler);
@@ -408,6 +399,10 @@ public function setSaveHandler($saveHandler = null)
408399
}
409400
$this->saveHandler = $saveHandler;
410401

402+
if (headers_sent() || (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status())) {
403+
return;
404+
}
405+
411406
if ($this->saveHandler instanceof \SessionHandlerInterface) {
412407
if (\PHP_VERSION_ID >= 50400) {
413408
session_set_save_handler($this->saveHandler, false);

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)