Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 08ab7c3

Browse filesBrowse files
committed
Remove initialized_session
1 parent 8f595e3 commit 08ab7c3
Copy full SHA for 08ab7c3

File tree

5 files changed

+40
-83
lines changed
Filter options

5 files changed

+40
-83
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
7979
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
8080
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
81-
use Symfony\Component\HttpKernel\EventListener\TestSessionListener;
8281
use Symfony\Component\Lock\Lock;
8382
use Symfony\Component\Lock\LockFactory;
8483
use Symfony\Component\Lock\PersistingStoreInterface;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php
+4-16Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ abstract class AbstractSessionListener implements EventSubscriberInterface
4141
public const NO_AUTO_CACHE_CONTROL_HEADER = 'Symfony-Session-NoAutoCacheControl';
4242

4343
protected $container;
44-
private $sessionUsageStack = [];
4544
private $debug;
4645

4746
public function __construct(ContainerInterface $container = null, bool $debug = false)
@@ -62,10 +61,6 @@ public function onKernelRequest(RequestEvent $event)
6261
$sess = null;
6362
$request->setSessionFactory(function () use (&$sess) { return $sess ?? $sess = $this->getSession(); });
6463
}
65-
66-
// Code related to `initialized_session` can be removed when symfony/http-kernel will stop being compatible with symfony/framework-bundle<6.0
67-
$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : null;
68-
$this->sessionUsageStack[] = $session instanceof Session ? $session->getUsageIndex() : 0;
6964
}
7065

7166
public function onKernelResponse(ResponseEvent $event)
@@ -79,9 +74,10 @@ public function onKernelResponse(ResponseEvent $event)
7974
// Always remove the internal header if present
8075
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
8176

82-
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $event->getRequest()->getSession()) {
77+
if (!$event->getRequest()->hasSession()) {
8378
return;
8479
}
80+
$session = $event->getRequest()->getSession();
8581

8682
if ($session->isStarted()) {
8783
/*
@@ -112,7 +108,7 @@ public function onKernelResponse(ResponseEvent $event)
112108
$session->save();
113109
}
114110

115-
if ($session instanceof Session ? $session->getUsageIndex() === end($this->sessionUsageStack) : !$session->isStarted()) {
111+
if ($session instanceof Session ? $session->getUsageIndex() === 0 : !$session->isStarted()) {
116112
return;
117113
}
118114

@@ -137,13 +133,6 @@ public function onKernelResponse(ResponseEvent $event)
137133
}
138134
}
139135

140-
public function onFinishRequest(FinishRequestEvent $event)
141-
{
142-
if ($event->isMainRequest()) {
143-
array_pop($this->sessionUsageStack);
144-
}
145-
}
146-
147136
public function onSessionUsage(): void
148137
{
149138
if (!$this->debug) {
@@ -168,7 +157,7 @@ public function onSessionUsage(): void
168157
return;
169158
}
170159

171-
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $requestStack->getCurrentRequest()->getSession()) {
160+
if (!$session = $requestStack->getCurrentRequest()->getSession()) {
172161
return;
173162
}
174163

@@ -185,7 +174,6 @@ public static function getSubscribedEvents(): array
185174
KernelEvents::REQUEST => ['onKernelRequest', 128],
186175
// low priority to come after regular response listeners, but higher than StreamedResponseListener
187176
KernelEvents::RESPONSE => ['onKernelResponse', -1000],
188-
KernelEvents::FINISH_REQUEST => ['onFinishRequest'],
189177
];
190178
}
191179

‎src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function onKernelResponse(ResponseEvent $event)
100100
public static function getSubscribedEvents(): array
101101
{
102102
return [
103-
KernelEvents::REQUEST => ['onKernelRequest', 127], // After SessionListener
103+
KernelEvents::REQUEST => ['onKernelRequest', 127], // AFTER SessionListener
104104
KernelEvents::RESPONSE => ['onKernelResponse', -128],
105105
];
106106
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/SessionListener.php
-25Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
/**
2020
* Sets the session in the request.
2121
*
22-
* When the passed container contains a "session_storage" entry which
23-
* holds a NativeSessionStorage instance, the "cookie_secure" option
24-
* will be set to true whenever the current main request is secure.
25-
*
2622
* @author Fabien Potencier <fabien@symfony.com>
2723
*
2824
* @final
@@ -34,29 +30,8 @@ public function __construct(ContainerInterface $container, bool $debug = false)
3430
parent::__construct($container, $debug);
3531
}
3632

37-
public function onKernelRequest(RequestEvent $event)
38-
{
39-
parent::onKernelRequest($event);
40-
41-
if (!$event->isMainRequest() || (!$this->container->has('session') && !$this->container->has('session_factory'))) {
42-
return;
43-
}
44-
45-
if ($this->container->has('session_storage')
46-
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
47-
&& ($mainRequest = $this->container->get('request_stack')->getMainRequest())
48-
&& $mainRequest->isSecure()
49-
) {
50-
$storage->setOptions(['cookie_secure' => true]);
51-
}
52-
}
53-
5433
protected function getSession(): ?SessionInterface
5534
{
56-
if ($this->container->has('session')) {
57-
return $this->container->get('session');
58-
}
59-
6035
if ($this->container->has('session_factory')) {
6136
return $this->container->get('session_factory')->createSession();
6237
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php
+35-40Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
use Symfony\Component\HttpKernel\HttpKernelInterface;
3232
use Symfony\Component\HttpKernel\KernelInterface;
3333

34-
/**
35-
* Tests related to `initialized_session` and `session` can be updated when symfony/http-kernel will stop being
36-
* compatible with symfony/framework-bundle<6.0
37-
*/
3834
class SessionListenerTest extends TestCase
3935
{
4036
public function testOnlyTriggeredOnMainRequest()
@@ -51,17 +47,17 @@ public function testOnlyTriggeredOnMainRequest()
5147
public function testSessionIsSet()
5248
{
5349
$session = $this->createMock(Session::class);
50+
$sessionFactory = $this->createMock(SessionFactory::class);
51+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
5452

5553
$requestStack = $this->createMock(RequestStack::class);
56-
$requestStack->expects($this->once())->method('getMainRequest')->willReturn(null);
5754

5855
$sessionStorage = $this->createMock(NativeSessionStorage::class);
5956
$sessionStorage->expects($this->never())->method('setOptions')->with(['cookie_secure' => true]);
6057

6158
$container = new Container();
62-
$container->set('session', $session);
59+
$container->set('session_factory', $sessionFactory);
6360
$container->set('request_stack', $requestStack);
64-
$container->set('session_storage', $sessionStorage);
6561

6662
$request = new Request();
6763
$listener = new SessionListener($container);
@@ -97,10 +93,12 @@ public function testSessionUsesFactory()
9793
public function testResponseIsPrivateIfSessionStarted()
9894
{
9995
$session = $this->createMock(Session::class);
100-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
96+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
97+
$sessionFactory = $this->createMock(SessionFactory::class);
98+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
10199

102100
$container = new Container();
103-
$container->set('initialized_session', $session);
101+
$container->set('session_factory', $sessionFactory);
104102

105103
$listener = new SessionListener($container);
106104
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -109,7 +107,7 @@ public function testResponseIsPrivateIfSessionStarted()
109107
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
110108

111109
$response = new Response();
112-
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
110+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
113111

114112
$this->assertTrue($response->headers->has('Expires'));
115113
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
@@ -122,10 +120,9 @@ public function testResponseIsPrivateIfSessionStarted()
122120
public function testResponseIsStillPublicIfSessionStartedAndHeaderPresent()
123121
{
124122
$session = $this->createMock(Session::class);
125-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
123+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
126124

127125
$container = new Container();
128-
$container->set('initialized_session', $session);
129126

130127
$listener = new SessionListener($container);
131128
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -136,7 +133,10 @@ public function testResponseIsStillPublicIfSessionStartedAndHeaderPresent()
136133
$response = new Response();
137134
$response->setSharedMaxAge(60);
138135
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
139-
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
136+
137+
$request = new Request();
138+
$request->setSession($session);
139+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
140140

141141
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
142142
$this->assertFalse($response->headers->has('Expires'));
@@ -153,9 +153,7 @@ public function testUninitializedSession()
153153
$response->setSharedMaxAge(60);
154154
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
155155

156-
$container = new ServiceLocator([
157-
'initialized_session' => function () {},
158-
]);
156+
$container = new Container();
159157

160158
$listener = new SessionListener($container);
161159
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
@@ -170,11 +168,12 @@ public function testUninitializedSession()
170168
public function testSurrogateMainRequestIsPublic()
171169
{
172170
$session = $this->createMock(Session::class);
173-
$session->expects($this->exactly(4))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1, 1, 1));
171+
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
172+
$sessionFactory = $this->createMock(SessionFactory::class);
173+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
174174

175175
$container = new Container();
176-
$container->set('initialized_session', $session);
177-
$container->set('session', $session);
176+
$container->set('session_factory', $sessionFactory);
178177

179178
$listener = new SessionListener($container);
180179
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -189,7 +188,6 @@ public function testSurrogateMainRequestIsPublic()
189188
$this->assertSame($request->getSession(), $subRequest->getSession());
190189
$listener->onKernelRequest(new RequestEvent($kernel, $subRequest, HttpKernelInterface::MAIN_REQUEST));
191190
$listener->onKernelResponse(new ResponseEvent($kernel, $subRequest, HttpKernelInterface::MAIN_REQUEST, $response));
192-
$listener->onFinishRequest(new FinishRequestEvent($kernel, $subRequest, HttpKernelInterface::MAIN_REQUEST));
193191

194192
$this->assertFalse($response->headers->has('Expires'));
195193
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
@@ -209,19 +207,15 @@ public function testSurrogateMainRequestIsPublic()
209207
public function testGetSessionIsCalledOnce()
210208
{
211209
$session = $this->createMock(Session::class);
212-
$sessionStorage = $this->createMock(NativeSessionStorage::class);
210+
$sessionFactory = $this->createMock(SessionFactory::class);
211+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
213212
$kernel = $this->createMock(KernelInterface::class);
214213

215-
$sessionStorage->expects($this->once())
216-
->method('setOptions')
217-
->with(['cookie_secure' => true]);
218-
219214
$requestStack = new RequestStack();
220215
$requestStack->push($mainRequest = new Request([], [], [], [], [], ['HTTPS' => 'on']));
221216

222217
$container = new Container();
223-
$container->set('session_storage', $sessionStorage);
224-
$container->set('session', $session);
218+
$container->set('session_factory', $sessionFactory);
225219
$container->set('request_stack', $requestStack);
226220

227221
$event = new RequestEvent($kernel, $mainRequest, HttpKernelInterface::MAIN_REQUEST);
@@ -230,9 +224,6 @@ public function testGetSessionIsCalledOnce()
230224
$listener->onKernelRequest($event);
231225

232226
// storage->setOptions() should have been called already
233-
$container->set('session_storage', null);
234-
$sessionStorage = null;
235-
236227
$subRequest = $mainRequest->duplicate();
237228
// at this point both main and subrequest have a closure to build the session
238229

@@ -245,10 +236,12 @@ public function testGetSessionIsCalledOnce()
245236
public function testSessionUsageExceptionIfStatelessAndSessionUsed()
246237
{
247238
$session = $this->createMock(Session::class);
248-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
239+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
240+
$sessionFactory = $this->createMock(SessionFactory::class);
241+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
249242

250243
$container = new Container();
251-
$container->set('initialized_session', $session);
244+
$container->set('session_factory', $sessionFactory);
252245

253246
$listener = new SessionListener($container, true);
254247
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -264,13 +257,15 @@ public function testSessionUsageExceptionIfStatelessAndSessionUsed()
264257
public function testSessionUsageLogIfStatelessAndSessionUsed()
265258
{
266259
$session = $this->createMock(Session::class);
267-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
260+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
261+
$sessionFactory = $this->createMock(SessionFactory::class);
262+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
268263

269264
$logger = $this->createMock(LoggerInterface::class);
270265
$logger->expects($this->exactly(1))->method('warning');
271266

272267
$container = new Container();
273-
$container->set('initialized_session', $session);
268+
$container->set('session_factory', $sessionFactory);
274269
$container->set('logger', $logger);
275270

276271
$listener = new SessionListener($container, false);
@@ -287,11 +282,13 @@ public function testSessionIsSavedWhenUnexpectedSessionExceptionThrown()
287282
{
288283
$session = $this->createMock(Session::class);
289284
$session->method('isStarted')->willReturn(true);
290-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
285+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
291286
$session->expects($this->exactly(1))->method('save');
287+
$sessionFactory = $this->createMock(SessionFactory::class);
288+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
292289

293290
$container = new Container();
294-
$container->set('initialized_session', $session);
291+
$container->set('session_factory', $sessionFactory);
295292

296293
$listener = new SessionListener($container, true);
297294
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -319,13 +316,13 @@ public function testSessionUsageCallbackWhenDebugAndStateless()
319316

320317
$requestStack->push(new Request());
321318
$requestStack->push($request);
322-
$requestStack->push(new Request());
319+
$requestStack->push($subRequest = new Request());
320+
$subRequest->setSession($session);
323321

324322
$collector = $this->createMock(RequestDataCollector::class);
325323
$collector->expects($this->once())->method('collectSessionUsage');
326324

327325
$container = new Container();
328-
$container->set('initialized_session', $session);
329326
$container->set('request_stack', $requestStack);
330327
$container->set('session_collector', \Closure::fromCallable([$collector, 'collectSessionUsage']));
331328

@@ -349,7 +346,6 @@ public function testSessionUsageCallbackWhenNoDebug()
349346
$collector->expects($this->never())->method('collectSessionUsage');
350347

351348
$container = new Container();
352-
$container->set('initialized_session', $session);
353349
$container->set('request_stack', $requestStack);
354350
$container->set('session_collector', $collector);
355351

@@ -367,7 +363,6 @@ public function testSessionUsageCallbackWhenNoStateless()
367363
$requestStack->push(new Request());
368364

369365
$container = new Container();
370-
$container->set('initialized_session', $session);
371366
$container->set('request_stack', $requestStack);
372367

373368
(new SessionListener($container, true))->onSessionUsage();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.