8000 bug #41343 [HttpKernel] Fix compatibility with Symfony6 (jderusse) · symfony/symfony@cfb1016 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfb1016

Browse files
bug #41343 [HttpKernel] Fix compatibility with Symfony6 (jderusse)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpKernel] Fix compatibility with Symfony6 | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The `session` service will be removed in FrameworkBundle 6, this PR make sure the session listener 5.4 will be compatible with FrameworkBundle 6.0 Commits ------- 0458384 Fix compatibility with Symfony6
2 parents 2ba71b0 + 0458384 commit cfb1016

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
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/Bundle/FrameworkBundle/Resources/config/test.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
->set('test.session.listener', TestSessionListener::class)
3939
->args([
4040
service_locator([
41+
'session_factory' => service('session.factory')->ignoreOnInvalid(),
4142
'session' => service('.session.do-not-use')->ignoreOnInvalid(),
4243
]),
4344
])

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public function testNullSessionHandler()
581581
$this->assertNull($container->getDefinition('session.storage.factory.php_bridge')->getArgument(0));
582582
$this->assertSame('session.handler.native_file', (string) $container->getAlias('session.handler'));
583583

584-
$expected = ['session', 'initialized_session', 'logger', 'session_collector'];
584+
$expected = ['session_factory', 'session', 'initialized_session', 'logger', 'session_collector'];
585585
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
586586
$this->assertFalse($container->getDefinition('session.storage.factory.native')->getArgument(3));
587587
}
@@ -600,7 +600,7 @@ public function testNullSessionHandlerLegacy()
600600
$this->assertNull($container->getDefinition('session.storage.php_bridge')->getArgument(0));
601601
$this->assertSame('session.handler.native_file', (string) $container->getAlias('session.handler'));
602602

603-
$expected = ['session', 'initialized_session', 'logger', 'session_collector'];
603+
$expected = ['session_factory', 'session', 'initialized_session', 'logger', 'session_collector'];
604604
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
605605
$this->assertFalse($container->getDefinition('session.storage.factory.native')->getArgument(3));
606606
}
@@ -1618,7 +1618,7 @@ public function testSessionCookieSecureAuto()
16181618
{
16191619
$container = $this->createContainerFromFile('session_cookie_secure_auto');
16201620

1621-
$expected = ['session', 'initialized_session', 'logger', 'session_collector'];
1621+
$expected = ['session_factory', 'session', 'initialized_session', 'logger', 'session_collector'];
16221622
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
16231623
}
16241624

@@ -1631,7 +1631,7 @@ public function testSessionCookieSecureAutoLegacy()
16311631

16321632
$container = $this->createContainerFromFile('session_cookie_secure_auto_legacy');
16331633

1634-
$expected = ['session', 'initialized_session', 'logger', 'session_collector', 'session_storage', 'request_stack'];
1634+
$expected = ['session_factory', 'session', 'initialized_session', 'logger', 'session_collector', 'session_storage', 'request_stack'];
16351635
$this->assertEquals($expected, array_keys($container->getDefinition('session_listener')->getArgument(0)->getValues()));
16361636
}
16371637

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 prevents 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

+7-3
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,14 @@ public function onKernelRequest(RequestEvent $event)
5353

5454
protected function getSession(): ?SessionInterface
5555
{
56-
if (!$this->container->has('session')) {
57-
return null;
56+
if ($this->container->has('session')) {
57+
return $this->container->get('session');
5858
}
5959

60-
return $this->container->get('session');
60+
if ($this->container->has('session_factory')) {
61+
return $this->container->get('session_factory')->createSession();
62+
}
63+
64+
return null;
6165
}
6266
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ public function __construct(ContainerInterface $container, array $sessionOptions
3333

3434
protected function getSession(): ?SessionInterface
3535
{
36-
if (!$this->container->has('session')) {
37-
return null;
36+
if ($this->container->has('session')) {
37+
return $this->container->get('session');
3838
}
3939

40-
return $this->container->get('session');
40+
if ($this->container->has('session_factory')) {
41+
return $this->container->get('session_factory')->createSession();
42+
}
43+
44+
return null;
4145
}
4246
}

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