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

Skip to content

Commit ceffe76

Browse files
committed
feature #26564 [HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false (fmata)
This PR was merged into the 4.1-dev branch. Discussion ---------- [HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #26539 | License | MIT Commits ------- 4110d57 [HttpFoundation] deprecate call to Request::getSession() when Request::hasSession() returns false
2 parents 0cfc00e + 4110d57 commit ceffe76

File tree

8 files changed

+23
-6
lines changed

8 files changed

+23
-6
lines changed

UPGRADE-4.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ HttpFoundation
6060
--------------
6161

6262
* Passing the file size to the constructor of the `UploadedFile` class is deprecated.
63-
6463
* The `getClientSize()` method of the `UploadedFile` class is deprecated. Use `getSize()` instead.
64+
* Deprecated `Symfony\Component\HttpFoundation\Request::getSession()` when no session has been set. Use `Symfony\Component\HttpFoundation\Request::hasSession()` instead.
6565

6666
Security
6767
--------

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ HttpFoundation
5959

6060
* The `$size` argument of the `UploadedFile` constructor has been removed.
6161
* The `getClientSize()` method of the `UploadedFile` class has been removed.
62+
* The `getSession()` method of the `Request` class throws an exception when session is null.
6263

6364
Security
6465
--------

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ public function toolbarAction(Request $request, $token)
129129
throw new NotFoundHttpException('The profiler must be enabled.');
130130
}
131131

132-
$session = $request->getSession();
133-
134-
if (null !== $session && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
132+
if ($request->hasSession() && ($session = $request->getSession()) && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
135133
// keep current flashes for one more request if using AutoExpireFlashBag
136134
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
137135
}

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* The `get()` method of the `AcceptHeader` class now takes into account the
1111
`*` and `*/*` default values (if they are present in the Accept HTTP header)
1212
when looking for items.
13+
* deprecated `Request::getSession()` when no session has been set. Use `Request::hasSession()` instead.
1314

1415
4.0.0
1516
-----

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,11 @@ public function getSession()
725725
$this->setSession($session = $session());
726726
}
727727

728+
if (null === $session) {
729+
@trigger_error(sprintf('Calling "%s()" when no session has been set is deprecated since Symfony 4.1 and will throw an exception in 5.0. Use "hasSession()" instead.', __METHOD__), E_USER_DEPRECATED);
730+
// throw new \BadMethodCallException('Session has not been set');
731+
}
732+
728733
return $session;
729734
}
730735

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,15 @@ public function testGetSession()
14911491
$this->assertObjectHasAttribute('attributeName', $session);
14921492
}
14931493

1494+
/**
1495+
* @group legacy
1496+
* @expectedDeprecation Calling "Symfony\Component\HttpFoundation\Request::getSession()" when no session has been set is deprecated since Symfony 4.1 and will throw an exception in 5.0. Use "hasSession()" instead.
1497+
*/
1498+
public function testGetSessionNullable()
1499+
{
1500+
(new Request())->getSession();
1501+
}
1502+
14941503
public function testHasPreviousSession()
14951504
{
14961505
$request = new Request();

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/SessionValueResolver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ final class SessionValueResolver implements ArgumentValueResolverInterface
2828
*/
2929
public function supports(Request $request, ArgumentMetadata $argument)
3030
{
31+
if (!$request->hasSession()) {
32+
return false;
33+
}
34+
3135
$type = $argument->getType();
3236
if (SessionInterface::class !== $type && !is_subclass_of($type, SessionInterface::class)) {
3337
return false;

src/Symfony/Component/Security/Guard/Authenticator/AbstractFormLoginAuthenticator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Security\Guard\Authenticator;
1313

14-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1514
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
1615
use Symfony\Component\HttpFoundation\RedirectResponse;
1716
use Symfony\Component\HttpFoundation\Request;
@@ -42,7 +41,7 @@ abstract protected function getLoginUrl();
4241
*/
4342
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
4443
{
45-
if ($request->getSession() instanceof SessionInterface) {
44+
if ($request->hasSession()) {
4645
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
4746
}
4847

0 commit comments

Comments
 (0)