Skip to content

Commit 8deed76

Browse files
Merge branch '4.4' into 5.2
* 4.4: [HttpKernel] Configure `session.cookie_secure` earlier Make sure the Psalm review CI job is working Adding a Github action to run Psalm
2 parents 55eaac9 + 3328f7c commit 8deed76

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

EventListener/SessionListener.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1616
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
17+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1718

1819
/**
1920
* Sets the session in the request.
@@ -33,10 +34,12 @@ public function __construct(ContainerInterface $container, bool $debug = false)
3334
parent::__construct($container, $debug);
3435
}
3536

36-
protected function getSession(): ?SessionInterface
37+
public function onKernelRequest(GetResponseEvent $event)
3738
{
38-
if (!$this->container->has('session')) {
39-
return null;
39+
parent::onKernelRequest($event);
40+
41+
if (!$event->isMasterRequest() || !$this->container->has('session')) {
42+
return;
4043
}
4144

4245
if ($this->container->has('session_storage')
@@ -46,6 +49,13 @@ protected function getSession(): ?SessionInterface
4649
) {
4750
$storage->setOptions(['cookie_secure' => true]);
4851
}
52+
}
53+
54+
protected function getSession(): ?SessionInterface
55+
{
56+
if (!$this->container->has('session')) {
57+
return null;
58+
}
4959

5060
return $this->container->get('session');
5161
}

Tests/EventListener/SessionListenerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testSessionIsSet()
6262
$listener = new SessionListener($container);
6363

6464
$event = $this->createMock(RequestEvent::class);
65-
$event->expects($this->once())->method('isMasterRequest')->willReturn(true);
65+
$event->expects($this->exactly(2))->method('isMasterRequest')->willReturn(true);
6666
$event->expects($this->once())->method('getRequest')->willReturn($request);
6767

6868
$listener->onKernelRequest($event);
@@ -206,12 +206,16 @@ public function testGetSessionIsCalledOnce()
206206
$listener = new SessionListener($container);
207207
$listener->onKernelRequest($event);
208208

209+
// storage->setOptions() should have been called already
210+
$container->set('session_storage', null);
211+
$sessionStorage = null;
212+
209213
$subRequest = $masterRequest->duplicate();
210214
// at this point both master and subrequest have a closure to build the session
211215

212216
$masterRequest->getSession();
213217

214-
// calling the factory on the subRequest should not trigger a second call to storage->sesOptions()
218+
// calling the factory on the subRequest should not trigger a second call to storage->setOptions()
215219
$subRequest->getSession();
216220
}
217221

0 commit comments

Comments
 (0)