8000 Fix session called initized several time · symfony/symfony@30a3c7c · GitHub
[go: up one dir, main page]

Skip to content

Commit 30a3c7c

Browse files
committed
Fix session called initized several time
1 parent 8ef1826 commit 30a3c7c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public function onKernelRequest(GetResponseEvent $event)
5656
$session = null;
5757
$request = $event->getRequest();
5858
if (!$request->hasSession()) {
59-
$request->setSessionFactory(function () { return $this->getSession(); });
59+
$sess = null;
60+
$request->setSessionFactory(function () use (&$sess) { return $sess ?? $sess = $this->getSession(); });
6061
}
6162

6263
$session = $session ?? ($this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : null);

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
use Symfony\Component\HttpFoundation\Session\Session;
2121
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
2222
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
23+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
2324
use Symfony\Component\HttpKernel\Event\RequestEvent;
2425
use Symfony\Component\HttpKernel\Event\ResponseEvent;
2526
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
2627
use Symfony\Component\HttpKernel\EventListener\SessionListener;
2728
use Symfony\Component\HttpKernel\HttpKernelInterface;
29+
use Symfony\Component\HttpKernel\KernelInterface;
2830

2931
class SessionListenerTest extends TestCase
3032
{
@@ -178,4 +180,36 @@ public function testSurrogateMasterRequestIsPublic()
178180
$this->assertTrue($response->headers->has('Expires'));
179181
$this->assertLessThanOrEqual((new \DateTime('now', new \DateTimeZone('UTC'))), (new \DateTime($response->headers->get('Expires'))));
180182
}
183+
184+
public function testGetSessionIsCalledOnce()
185+
{
186+
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
187+
$sessionStorage = $this->getMockBuilder(NativeSessionStorage::class)->getMock();
188+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
189+
190+
$sessionStorage->expects($this->once())
191+
->method('setOptions')
192+
->with(['cookie_secure' => true]);
193+
194+
$requestStack = new RequestStack();
195+
$requestStack->push($masterRequest = new Request([], [], [], [ A7D6 ], [], ['HTTPS' => 'on']));
196+
197+
$container = new Container();
198+
$container->set('session_storage', $sessionStorage);
199+
$container->set('session', $session);
200+
$container->set('request_stack', $requestStack);
201+
202+
$event = new GetResponseEvent($kernel, $masterRequest, HttpKernelInterface::MASTER_REQUEST);
203+
204+
$listener = new SessionListener($container);
205+
$listener->onKernelRequest($event);
206+
207+
$subRequest = $masterRequest->duplicate();
208+
// at this point both master and subrequest have a closure to build the session
209+
210+
$masterRequest->getSession();
211+
212+
// calling the factory on the subRequest should not trigger a second call to storage->sesOptions()
213+
$subRequest->getSession();
214+
}
181215
}

0 commit comments

Comments
 (0)
0