Skip to content

Commit b4a8647

Browse files
committed
[FrameworkBundle] Add KernelBrowser::getSession()
1 parent cef0da8 commit b4a8647

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Allow using their name without added suffix when using `#[Target]` for custom services
99
* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
1010
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
11+
* Add `getSession()` to `KernelBrowser`
1112

1213
7.3
1314
---

src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\DependencyInjection\ContainerInterface;
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2122
use Symfony\Component\HttpKernel\HttpKernelBrowser;
2223
use Symfony\Component\HttpKernel\KernelInterface;
2324
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
@@ -63,6 +64,38 @@ public function getProfile(): HttpProfile|false|null
6364
return $this->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
6465
}
6566

67+
public function getSession(): ?SessionInterface
68+
{
69+
$container = $this->getContainer();
70+
71+
if (!$container->has('session.factory')) {
72+
return null;
73+
}
74+
75+
$session = $container->get('session.factory')->createSession();
76+
77+
$cookieJar = $this->getCookieJar();
78+
$cookie = $cookieJar->get($session->getName());
79+
80+
// Load the current session if the cookie already exists
81+
if ($cookie instanceof Cookie) {
82+
$session->setId($cookie->getValue());
83+
}
84+
85+
$session->start();
86+
87+
// Create the cookie if it does not already exist
88+
if (!$cookie instanceof Cookie) {
89+
$domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $cookieJar->all())) ?: [''];
90+
foreach ($domains as $domain) {
91+
$cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain);
92+
$cookieJar->set($cookie);
93+
}
94+
}
95+
96+
return $session;
97+
}
98+
6699
/**
67100
* Enables the profiler for the very next request.
68101
*
@@ -116,20 +149,14 @@ public function loginUser(object $user, string $firewallContext = 'main', array
116149
$container = $this->getContainer();
117150
$container->get('security.untracked_token_storage')->setToken($token);
118151

119-
if (!$container->has('session.factory')) {
152+
$session = $this->getSession();
153+
if (!$session instanceof SessionInterface) {
120154
return $this;
121155
}
122156

123-
$session = $container->get('session.factory')->createSession();
124157
$session->set('_security_'.$firewallContext, serialize($token));
125158
$session->save();
126159

127-
$domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $this->getCookieJar()->all())) ?: [''];
128-
foreach ($domains as $domain) {
129-
$cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain);
130-
$this->getCookieJar()->set($cookie);
131-
}
132-
133160
return $this;
134161
}
135162

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public function testWelcome($config, $insulate)
4444
// prove cleared session
4545
$crawler = $client->request('GET', '/session');
4646
$this->assertStringContainsString('You are new here and gave no name.', $crawler->text());
47+
48+
// prepare session programmatically
49+
$session = $client->getSession();
50+
$session->set('name', 'drak');
51+
$session->save();
52+
53+
// ensure session can be saved multiple times without being reset
54+
$session = $client->getSession();
55+
$session->set('foo', 'bar');
56+
$session->save();
57+
58+
// prove remembered name from programmatically prepared session
59+
$crawler = $client->request('GET', '/session');
60+
$this->assertStringContainsString('Welcome back drak, nice to meet you.', $crawler->text());
4761
}
4862

4963
/**

0 commit comments

Comments
 (0)