diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index a6a5cb28dfc1d..1aee3845a1f39 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -761,11 +761,15 @@ public function hasPreviousSession() * like whether the session is started or not. It is just a way to check if this Request * is associated with a Session instance. * + * @param bool $skipIfUninitialized When true, ignores factories injected by `setSessionFactory` + * * @return bool */ - public function hasSession() + public function hasSession(/* bool $skipIfUninitialized = false */) { - return null !== $this->session; + $skipIfUninitialized = \func_num_args() > 0 ? func_get_arg(0) : false; + + return null !== $this->session && (!$skipIfUninitialized || $this->session instanceof SessionInterface); } public function setSession(SessionInterface $session) diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 0fab586dabe31..cf9f397a8a852 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -1635,8 +1635,15 @@ public function testHasSession() $request = new Request(); $this->assertFalse($request->hasSession()); + $this->assertFalse($request->hasSession(true)); + + $request->setSessionFactory(function () {}); + $this->assertTrue($request->hasSession()); + $this->assertFalse($request->hasSession(true)); + $request->setSession(new Session(new MockArraySessionStorage())); $this->assertTrue($request->hasSession()); + $this->assertTrue($request->hasSession(true)); } public function testGetSession()