Skip to content

Commit 37d1a21

Browse files
committed
Removed compatibility layer for PHP <5.4 sessions
Signed-off-by: Alexandru Furculita <[email protected]>
1 parent 086daf8 commit 37d1a21

File tree

16 files changed

+88
-669
lines changed

16 files changed

+88
-669
lines changed

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ public function testEnvironment()
4747

4848
public function testGetSession()
4949
{
50-
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
5150
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
52-
$request->method('getSession')->willReturn($session);
51+
$request->method('getSession')->willReturn($session = new Session());
5352

5453
$this->setRequestStack($request);
5554

@@ -168,9 +167,8 @@ public function testGetFlashesWithNoRequest()
168167

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

175173
$this->setRequestStack($request);
176174

@@ -259,7 +257,7 @@ private function setFlashMessages()
259257
$flashBag = new FlashBag();
260258
$flashBag->initialize($flashMessages);
261259

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

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function testRedirectToRoute()
376376
public function testAddFlash()
377377
{
378378
$flashBag = new FlashBag();
379-
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->disableOriginalConstructor()->getMock();
379+
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session')->getMock();
380380
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
381381

382382
$container = new Container();

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ CHANGELOG
1717
* checking for cacheable HTTP methods using the `Request::isMethodSafe()`
1818
method (by not passing `false` as its argument) is not supported anymore and
1919
throws a `\BadMethodCallException`
20+
* the `NativeSessionHandler` class has been removed
21+
* the `AbstractProxy`, `NativeProxy` and `SessionHandlerProxy` classes have been removed
22+
* setting session save handlers that do not implement `\SessionHandlerInterface` in
23+
`NativeSessionStorage::setSaveHandler()` is not supported anymore and throws a
24+
`\TypeError`
2025

2126
3.4.0
2227
-----

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Drak <[email protected]>
1818
*/
19-
class NativeFileSessionHandler extends NativeSessionHandler
19+
class NativeFileSessionHandler extends \SessionHandler
2020
{
2121
/**
2222
* @param string $savePath Path of directory to save session files

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\HttpFoundation\Session\Storage;
1313

1414
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15-
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
16-
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
1715

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

4038
/**
41-
* @var AbstractProxy|\SessionHandlerInterface
39+
* @var \SessionHandlerInterface
4240
*/
4341
protected $saveHandler;
4442

@@ -87,12 +85,8 @@ class NativeSessionStorage implements SessionStorageInterface
8785
* sid_bits_per_character, "5"
8886
* trans_sid_hosts, $_SERVER['HTTP_HOST']
8987
* trans_sid_tags, "a=href,area=href,frame=src,form="
90-
*
91-
* @param array $options Session configuration options
92-
* @param \SessionHandlerInterface|null $handler
93-
* @param MetadataBag $metaBag MetadataBag
9488
*/
95-
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
89+
public function __construct(array $options = array(), \SessionHandlerInterface $handler = null, MetadataBag $metaBag = null)
9690
{
9791
session_cache_limiter(''); // disable by default because it's managed by HeaderBag (if used)
9892
ini_set('session.use_cookies', 1);
@@ -107,7 +101,7 @@ public function __construct(array $options = array(), $handler = null, MetadataB
107101
/**
108102
* Gets the save handler instance.
109103
*
110-
* @return AbstractProxy|\SessionHandlerInterface
104+
* @return \SessionHandlerInterface
111105
*/
112106
public function getSaveHandler()
113107
{
@@ -146,31 +140,43 @@ public function start()
146140
*/
147141
public function getId()
148142
{
149-
return $this->saveHandler->getId();
143+
return session_id();
150144
}
151145

152146
/**
153147
* {@inheritdoc}
148+
*
149+
* @throws \LogicException When the session is active
154150
*/
155151
public function setId($id)
156152
{
157-
$this->saveHandler->setId($id);
153+
if (\PHP_SESSION_ACTIVE === session_status()) {
154+
throw new \LogicException('Cannot change the ID of an active session');
155+
}
156+
157+
session_id($id);
158158
}
159159

160160
/**
161161
* {@inheritdoc}
162162
*/
163163
public function getName()
164164
{
165-
return $this->saveHandler->getName();
165+
return session_name();
166166
}
167167

168168
/**
169169
* {@inheritdoc}
170+
*
171+
* @throws \LogicException When the session is active
170172
*/
171173
public function setName($name)
172174
{
173-
$this->saveHandler->setName($name);
175+
if (\PHP_SESSION_ACTIVE === session_status()) {
176+
throw new \LogicException('Cannot change the name of an active session');
177+
}
178+
179+
session_name($name);
174180
}
175181

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

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

249252
/**
250253
* {@inheritdoc}
254+
*
255+
* @throws \LogicException When the session is already started
251256
*/
252257
public function registerBag(SessionBagInterface $bag)
253258
{
@@ -267,7 +272,7 @@ public function getBag($name)
267272
throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
268273
}
269274

270-
if (!$this->started && $this->saveHandler->isActive()) {
275+
if (!$this->started && \PHP_SESSION_ACTIVE === session_status()) {
271276
$this->loadSession();
272277
} elseif (!$this->started) {
273278
$this->start();
@@ -357,37 +362,12 @@ public function setOptions(array $options)
357362
* @see http://php.net/sessionhandlerinterface
358363
* @see http://php.net/sessionhandler
359364
* @see http://github.com/drak/NativeSession
360-
*
361-
* @param \SessionHandlerInterface|null $saveHandler
362-
*
363-
* @throws \InvalidArgumentException
364365
*/
365-
public function setSaveHandler($saveHandler = null)
366+
public function setSaveHandler(\SessionHandlerInterface $saveHandler = null)
366367
{
367-
if (!$saveHandler instanceof AbstractProxy &&
368-
!$saveHandler instanceof \SessionHandlerInterface &&
369-
null !== $saveHandler) {
370-
throw new \InvalidArgumentException('Must be instance of AbstractProxy; implement \SessionHandlerInterface; or be null.');
371-
}
368+
$this->saveHandler = $saveHandler ?: new \SessionHandler();
372369

373-
if ($saveHandler instanceof AbstractProxy) {
374-
@trigger_error(
375-
'Using session save handlers that are instances of AbstractProxy is deprecated since version 3.4 and will be removed in 4.0.',
376-
E_USER_DEPRECATED
377-
);
378-
}
379-
380-
// Wrap $saveHandler in proxy and prevent double wrapping of proxy
381-
if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
382-
$saveHandler = new SessionHandlerProxy($saveHandler);
383-
} elseif (!$saveHandler instanceof AbstractProxy) {
384-
$saveHandler = new SessionHandlerProxy(new \SessionHandler());
385-
}
386-
$this->saveHandler = $saveHandler;
387-
388-
if ($this->saveHandler instanceof \SessionHandlerInterface) {
389-
session_set_save_handler($this->saveHandler, false);
390-
}
370+
session_set_save_handler($this->saveHandler, false);
391371
}
392372

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

409-
$bags = array_merge($this->bags, array($this->metadataBag));
389+
$bags = $this->bags;
390+
$bags[] = $this->metadataBag;
410391

411392
foreach ($bags as $bag) {
412393
$key = $bag->getStorageKey();
413-
$session[$key] = isset($session[$key]) ? $session[$key] : array();
394+
$session[$key] = $session[$key] ?? array();
414395
$bag->initialize($session[$key]);
415396
}
416397

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/AbstractProxy.php

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)