Skip to content

Commit

Permalink
bug #24952 [HttpFoundation] Fix session-related BC break (nicolas-gre…
Browse files Browse the repository at this point in the history
…kas, 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
  • Loading branch information
nicolas-grekas committed Nov 13, 2017
2 parents 99f8d85 + 38186aa commit 70dd46b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Expand Up @@ -102,12 +102,6 @@ class NativeSessionStorage implements SessionStorageInterface
*/
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
{
$this->setMetadataBag($metaBag);

if (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
return;
}

$options += array(
// disable by default because it's managed by HeaderBag (if used)
'cache_limiter' => '',
Expand All @@ -120,6 +114,7 @@ public function __construct(array $options = array(), $handler = null, MetadataB
register_shutdown_function('session_write_close');
}

$this->setMetadataBag($metaBag);
$this->setOptions($options);
$this->setSaveHandler($handler);
}
Expand Down Expand Up @@ -292,7 +287,7 @@ public function getBag($name)
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
}

if ($this->saveHandler->isActive() && !$this->started) {
if (!$this->started && $this->saveHandler->isActive()) {
$this->loadSession();
} elseif (!$this->started) {
$this->start();
Expand Down Expand Up @@ -340,7 +335,7 @@ public function isStarted()
*/
public function setOptions(array $options)
{
if (headers_sent()) {
if (headers_sent() || (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status())) {
return;
}

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

if (headers_sent($file, $line)) {
throw new \RuntimeException(sprintf('Failed to set the session handler because headers have already been sent by "%s" at line %d.', $file, $line));
}

// Wrap $saveHandler in proxy and prevent double wrapping of proxy
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
$saveHandler = new SessionHandlerProxy($saveHandler);
Expand All @@ -408,6 +399,10 @@ public function setSaveHandler($saveHandler = null)
}
$this->saveHandler = $saveHandler;

if (headers_sent() || (\PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status())) {
return;
}

if ($this->saveHandler instanceof \SessionHandlerInterface) {
if (\PHP_VERSION_ID >= 50400) {
session_set_save_handler($this->saveHandler, false);
Expand Down
Expand Up @@ -296,4 +296,19 @@ public function testSetSessionOptionsOnceSessionStartedIsIgnored()
// Assert no exception has been thrown by `getStorage()`
$this->addToAssertionCount(1);
}

/**
* @requires PHP 5.4
*/
public function testGetBagsOnceSessionStartedIsIgnored()
{
session_start();
$bag = new AttributeBag();
$bag->setName('flashes');

$storage = $this->getStorage();
$storage->registerBag($bag);

$this->assertEquals($storage->getBag('flashes'), $bag);
}
}

0 comments on commit 70dd46b

Please sign in to comment.