Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
* Add `framework.type_info.aliases` option
* Add `KernelBrowser::getSession()`

7.3
---
Expand Down
39 changes: 31 additions & 8 deletions src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -116,20 +146,13 @@ 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')) {
if (!$session = $this->getSession()) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 programatically
$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 programatically prepared session
$crawler = $client->request('GET', '/session');
$this->assertStringContainsString('Welcome back drak, nice to meet you.', $crawler->text());
}

/**
Expand Down
Loading