Skip to content

[HttpFoundation] Removed compatibility layer for PHP <5.4 sessions #24338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public function testEnvironment()

public function testGetSession()
{
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the fact that the session is constructed using an instance of NativeSessionStorage that by default used the deprecated SessionHandlerProxy, this test did not pass due to the deprecation error. Now there are no deprecations, so no need for disabling the original constructor,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be using MockArraySessionStorage rather than NativeSessionStorage in tests (or create a mock as before).

$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->method('getSession')->willReturn($session);
$request->method('getSession')->willReturn($session = new Session());

$this->setRequestStack($request);

Expand Down Expand Up @@ -168,9 +167,8 @@ public function testGetFlashesWithNoRequest()

public function testGetFlashesWithNoSessionStarted()
{
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
$request->method('getSession')->willReturn($session);
$request->method('getSession')->willReturn(new Session());

$this->setRequestStack($request);

Expand Down Expand Up @@ -259,7 +257,7 @@ private function setFlashMessages()
$flashBag = new FlashBag();
$flashBag->initialize($flashMessages);

$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock();
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
$session->method('isStarted')->willReturn(true);
$session->method('getFlashBag')->willReturn($flashBag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public function testRedirectToRoute()
public function testAddFlash()
{
$flashBag = new FlashBag();
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock();
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);

$container = new Container();
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ CHANGELOG
* checking for cacheable HTTP methods using the `Request::isMethodSafe()`
method (by not passing `false` as its argument) is not supported anymore and
throws a `\BadMethodCallException`
* the `NativeSessionHandler` class has been removed
* the `AbstractProxy`, `NativeProxy` and `SessionHandlerProxy` classes have been removed
* setting session save handlers that do not implement `\SessionHandlerInterface` in
`NativeSessionStorage::setSaveHandler()` is not supported anymore and throws a
`\TypeError`

3.4.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Drak <[email protected]>
*/
class NativeFileSessionHandler extends NativeSessionHandler
class NativeFileSessionHandler extends \SessionHandler
{
/**
* @param string $savePath Path of directory to save session files
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
namespace Symfony\Component\HttpFoundation\Session\Storage;

use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

/**
* This provides a base class for session attribute storage.
Expand All @@ -38,7 +36,7 @@ class NativeSessionStorage implements SessionStorageInterface
protected $closed = false;

/**
* @var AbstractProxy|\SessionHandlerInterface
* @var \SessionHandlerInterface
*/
protected $saveHandler;

Expand Down Expand Up @@ -87,12 +85,8 @@ class NativeSessionStorage implements SessionStorageInterface
* sid_bits_per_character, "5"
* trans_sid_hosts, $_SERVER['HTTP_HOST']
* trans_sid_tags, "a=href,area=href,frame=src,form="
*
* @param array $options Session configuration options
* @param \SessionHandlerInterface|null $handler
* @param MetadataBag $metaBag MetadataBag
*/
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
public function __construct(array $options = array(), \SessionHandlerInterface $handler = null, MetadataBag $metaBag = null)
{
session_cache_limiter(''); // disable by default because it's managed by HeaderBag (if used)
ini_set('session.use_cookies', 1);
Expand All @@ -107,7 +101,7 @@ public function __construct(array $options = array(), $handler = null, MetadataB
/**
* Gets the save handler instance.
*
* @return AbstractProxy|\SessionHandlerInterface
* @return \SessionHandlerInterface
*/
public function getSaveHandler()
{
Expand Down Expand Up @@ -146,31 +140,43 @@ public function start()
*/
public function getId()
{
return $this->saveHandler->getId();
return session_id();
}

/**
* {@inheritdoc}
*
* @throws \LogicException When the session is active
*/
public function setId($id)
{
$this->saveHandler->setId($id);
if (\PHP_SESSION_ACTIVE === session_status()) {
throw new \LogicException('Cannot change the ID of an active session');
}

session_id($id);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->saveHandler->getName();
return session_name();
}

/**
* {@inheritdoc}
*
* @throws \LogicException When the session is active
*/
public function setName($name)
{
$this->saveHandler->setName($name);
if (\PHP_SESSION_ACTIVE === session_status()) {
throw new \LogicException('Cannot change the name of an active session');
}

session_name($name);
}

/**
Expand Down Expand Up @@ -217,9 +223,6 @@ public function save()
// The default PHP error message is not very helpful, as it does not give any information on the current save handler.
// Therefore, we catch this error and trigger a warning with a better error message
$handler = $this->getSaveHandler();
if ($handler instanceof SessionHandlerProxy) {
$handler = $handler->getHandler();
}

restore_error_handler();
trigger_error(sprintf('session_write_close(): Failed to write session data with %s handler', get_class($handler)), E_USER_WARNING);
Expand Down Expand Up @@ -248,6 +251,8 @@ public function clear()

/**
* {@inheritdoc}
*
* @throws \LogicException When the session is already started
*/
public function registerBag(SessionBagInterface $bag)
{
Expand All @@ -267,7 +272,7 @@ public function getBag($name)
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
}

if (!$this->started && $this->saveHandler->isActive()) {
if (!$this->started && \PHP_SESSION_ACTIVE === session_status()) {
$this->loadSession();
} elseif (!$this->started) {
$this->start();
Expand Down Expand Up @@ -357,37 +362,12 @@ public function setOptions(array $options)
* @see http://php.net/sessionhandlerinterface
* @see http://php.net/sessionhandler
* @see http://github.com/drak/NativeSession
*
* @param \SessionHandlerInterface|null $saveHandler
*
* @throws \InvalidArgumentException
*/
public function setSaveHandler($saveHandler = null)
public function setSaveHandler(\SessionHandlerInterface $saveHandler = null)
{
if (!$saveHandler instanceof AbstractProxy &&
!$saveHandler instanceof \SessionHandlerInterface &&
null !== $saveHandler) {
throw new \InvalidArgumentException('Must be instance of AbstractProxy; implement \SessionHandlerInterface; or be null.');
}
$this->saveHandler = $saveHandler ?: new \SessionHandler();

if ($saveHandler instanceof AbstractProxy) {
@trigger_error(
'Using session save handlers that are instances of AbstractProxy is deprecated since version 3.4 and will be removed in 4.0.',
E_USER_DEPRECATED
);
}

// Wrap $saveHandler in proxy and prevent double wrapping of proxy
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
$saveHandler = new SessionHandlerProxy($saveHandler);
} elseif (!$saveHandler instanceof AbstractProxy) {
$saveHandler = new SessionHandlerProxy(new \SessionHandler());
}
$this->saveHandler = $saveHandler;

if ($this->saveHandler instanceof \SessionHandlerInterface) {
session_set_save_handler($this->saveHandler, false);
}
session_set_save_handler($this->saveHandler, false);
}

/**
Expand All @@ -406,11 +386,12 @@ protected function loadSession(array &$session = null)
$session = &$_SESSION;
}

$bags = array_merge($this->bags, array($this->metadataBag));
$bags = $this->bags;
$bags[] = $this->metadataBag;

foreach ($bags as $bag) {
$key = $bag->getStorageKey();
$session[$key] = isset($session[$key]) ? $session[$key] : array();
$session[$key] = $session[$key] ?? array();
$bag->initialize($session[$key]);
}

Expand Down

This file was deleted.

Loading