Skip to content

[HttpFoundation] Add a flag to hasSession to distinguished session from factory #42900

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 1 commit into from
Sep 6, 2021
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: 6 additions & 2 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down