diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 76b3cb9479256..d30f0d364ecb3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * Allow using their name without added suffix when using `#[Target]` for custom services * Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()` * Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait` + * Add `KernelBrowser::getSession()` method 7.3 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php b/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php index add2508ff466f..da1d50d2381fc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php +++ b/src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php @@ -18,6 +18,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpKernel\HttpKernelBrowser; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile; @@ -63,6 +64,35 @@ public function getProfile(): HttpProfile|false|null return $this->getContainer()->get('profiler')->loadProfileFromResponse($this->response); } + public function getSession(): ?SessionInterface + { + $container = $this->getContainer(); + + if (!$container->has('session.factory')) { + return null; + } + + $session = $container->get('session.factory')->createSession(); + + $cookieJar = $this->getCookieJar(); + $cookie = $cookieJar->get($session->getName()); + + if ($cookie instanceof Cookie) { + $session->setId($cookie->getValue()); + } + + $session->start(); + + if (!$cookie instanceof Cookie) { + $domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $cookieJar->all())) ?: ['']; + foreach ($domains as $domain) { + $cookieJar->set(new Cookie($session->getName(), $session->getId(), domain: $domain)); + } + } + + return $session; + } + /** * Enables the profiler for the very next request. * @@ -116,20 +146,14 @@ public function loginUser(object $user, string $firewallContext = 'main', array $container = $this->getContainer(); $container->get('security.untracked_token_storage')->setToken($token); - if (!$container->has('session.factory')) { + $session = $this->getSession(); + if (!$session instanceof SessionInterface) { return $this; } - $session = $container->get('session.factory')->createSession(); $session->set('_security_'.$firewallContext, serialize($token)); $session->save(); - $domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $this->getCookieJar()->all())) ?: ['']; - foreach ($domains as $domain) { - $cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain); - $this->getCookieJar()->set($cookie); - } - return $this; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php index 4c1b92ccf539f..78c6fbf84333e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php @@ -44,6 +44,20 @@ public function testWelcome($config, $insulate) // prove cleared session $crawler = $client->request('GET', '/session'); $this->assertStringContainsString('You are new here and gave no name.', $crawler->text()); + + // prepare session programmatically + $session = $client->getSession(); + $session->set('name', 'drak'); + $session->save(); + + // ensure session can be saved multiple times without being reset + $session = $client->getSession(); + $session->set('foo', 'bar'); + $session->save(); + + // prove remembered name from programmatically prepared session + $crawler = $client->request('GET', '/session'); + $this->assertStringContainsString('Welcome back drak, nice to meet you.', $crawler->text()); } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy