8000 Fix compatibility with Symfony6 · symfony/symfony@e6dd9a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6dd9a1

Browse files
committed
Fix compatibility with Symfony6
1 parent f836f6b commit e6dd9a1

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/session.php

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
->set('session_listener', SessionListener::class)
147147
->args([
148148
service_locator([
149+
'session_factory' => service('session.factory')->ignoreOnInvalid(),
149150
'session' => service('.session.do-not-use')->ignoreOnInvalid(),
150151
'initialized_session' => service('.session.do-not-use')->ignoreOnUninitialized(),
151152
'logger' => service('logger')->ignoreOnInvalid(),

src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function onKernelRequest(RequestEvent $event)
5858

5959
$request = $event->getRequest();
6060
if (!$request->hasSession()) {
61+
// This variable prevent calling `$this->getSession()` twice in case the Request (and the below factory) is cloned
6162
$sess = null;
6263
$request->setSessionFactory(function () use (&$sess) { return $sess ?? $sess = $this->getSession(); });
6364
}

src/Symfony/Component/HttpKernel/EventListener/SessionListener.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*
2626
* @author Fabien Potencier <fabien@symfony.com>
2727
*
28+
* @internal
29+
*
2830
* @final
2931
*/
3032
class SessionListener extends AbstractSessionListener
@@ -53,10 +55,14 @@ public function onKernelRequest(RequestEvent $event)
5355

5456
protected function getSession(): ?SessionInterface
5557
{
56-
if (!$this->container->has('session')) {
57-
return null;
58+
if ($this->container->has('session')) {
59+
return $this->container->get('session');
60+
}
61+
62+
if ($this->container->has('session_factory')) {
63+
return $this->container->get('session_factory')->createSession();
5864
}
5965

60-
return $this->container->get('session');
66+
return null;
6167
}
6268
}

src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\RequestStack;
2020
use Symfony\Component\HttpFoundation\Response;
2121
use Symfony\Component\HttpFoundation\Session\Session;
22+
use Symfony\Component\HttpFoundation\Session\SessionFactory;
2223
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
2324
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
2425
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
@@ -71,6 +72,28 @@ public function testSessionIsSet()
7172
$this->assertSame($session, $request->getSession());
7273
}
7374

75+
public function testSessionUsesFactory()
76+
{
77+
$session = $this->createMock(Session::class);
78+
$sessionFactory = $this->createMock(SessionFactory::class);
79+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
80+
81+
$container = new Container();
82+
$container->set('session_factory', $sessionFactory);
83+
84+
$request = new Request();
85+
$listener = new SessionListener($container);
86+
87+
$event = $this->createMock(RequestEvent::class);
88+
$event->expects($this->exactly(2))->method('isMainRequest')->willReturn(true);
89+
$event->expects($this->once())->method('getRequest')->willReturn($request);
90+
91+
$listener->onKernelRequest($event);
92+
93+
$this->assertTrue($request->hasSession());
94+
$this->assertSame($session, $request->getSession());
95+
}
96+
7497
public function testResponseIsPrivateIfSessionStarted()
7598
{
7699
$session = $this->createMock(Session::class);

0 commit comments

Comments
 (0)
0