8000 Remove initialized_session · symfony/symfony@2e170ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e170ed

Browse files
committed
Remove initialized_session
1 parent 352f839 commit 2e170ed

File tree

4 files changed

+41
-80
lines changed

4 files changed

+41
-80
lines changed

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

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

+4-16
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/SessionListener.php

-21
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,8 @@ public function __construct(ContainerInterface $container, bool $debug = false)
3434
parent::__construct($container, $debug);
3535
}
3636

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-
5437
protected function getSession(): ?SessionInterface
5538
{
56-
if ($this->container->has('session')) {
57-
return $this->container->get('session');
58-
}
59-
6039
if ($this->container->has('session_factory')) {
6140
return $this->container->get('session_factory')->createSession();
6241
}

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

+37-42
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,23 +47,23 @@ 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);
6864

6965
$event = $this->createMock(RequestEvent::class);
70-
$event->expects($this->exactly(2))->method('isMainRequest')->willReturn(true);
66+
$event->expects($this->exactly(1))->method('isMainRequest')->willReturn(true);
7167
$event->expects($this->once())->method('getRequest')->willReturn($request);
7268

7369
$listener->onKernelRequest($event);
@@ -89,7 +85,7 @@ public function testSessionUsesFactory()
8985
$listener = new SessionListener($container);
9086

9187
$event = $this->createMock(RequestEvent::class);
92-
$event->expects($this->exactly(2))->method('isMainRequest')->willReturn(true);
88+
$event->expects($this->exactly(1))->method('isMainRequest')->willReturn(true);
9389
$event->expects($this->once())->method('getRequest')->willReturn($request);
9490

9591
$listener->onKernelRequest($event);
@@ -101,10 +97,12 @@ public function testSessionUsesFactory()
10197
public function testResponseIsPrivateIfSessionStarted()
10298
{
10399
$session = $this->createMock(Session::class);
104-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
100+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
101+
$sessionFactory = $this->createMock(SessionFactory::class);
102+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
105103

106104
$container = new Container();
107-
$container->set('initialized_session', $session);
105+
$container->set('session_factory', $sessionFactory);
108106

109107
$listener = new SessionListener($container);
110108
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -113,7 +111,7 @@ public function testResponseIsPrivateIfSessionStarted()
113111
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
114112

115113
$response = new Response();
116-
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
114+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
117115

118116
$this->assertTrue($response->headers->has('Expires'));
119117
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
@@ -126,10 +124,9 @@ public function testResponseIsPrivateIfSessionStarted()
126124
public function testResponseIsStillPublicIfSessionStartedAndHeaderPresent()
127125
{
128126
$session = $this->createMock(Session::class);
129-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
127+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
130128

131129
$container = new Container();
132-
$container->set('initialized_session', $session);
133130

134131
$listener = new SessionListener($container);
135132
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -140,7 +137,10 @@ public function testResponseIsStillPublicIfSessionStartedAndHeaderPresent()
140137
$response = new Response();
141138
$response->setSharedMaxAge(60);
142139
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
143-
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
140+
141+
$request = new Request();
142+
$request->setSession($session);
143+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));
144144

145145
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
146146
$this->assertFalse($response->headers->has('Expires'));
@@ -157,9 +157,7 @@ public function testUninitializedSession()
157157
$response->setSharedMaxAge(60);
158158
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
159159

160-
$container = new ServiceLocator([
161-
'initialized_session' => function () {},
162-
]);
160+
$container = new Container();
163161

164162
$listener = new SessionListener($container);
165163
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
@@ -174,11 +172,12 @@ public function testUninitializedSession()
174172
public function testSurrogateMainRequestIsPublic()
175173
{
176174
$session = $this->createMock(Session::class);
177-
$session->expects($this->exactly(4))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1, 1, 1));
175+
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
176+
$sessionFactory = $this->createMock(SessionFactory::class);
177+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
178178

179179
$container = new Container();
180-
$container->set('initialized_session', $session);
181-
$container->set('session', $session);
180+
$container->set('session_factory', $sessionFactory);
182181

183182
$listener = new SessionListener($container);
184183
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -193,7 +192,6 @@ public function testSurrogateMainRequestIsPublic()
193192
$this->assertSame($request->getSession(), $subRequest->getSession());
194193
$listener->onKernelRequest(new RequestEvent($kernel, $subRequest, HttpKernelInterface::MAIN_REQUEST));
195194
$listener->onKernelResponse(new ResponseEvent($kernel, $subRequest, HttpKernelInterface::MAIN_REQUEST, $response));
196-
$listener->onFinishRequest(new FinishRequestEvent($kernel, $subRequest, HttpKernelInterface::MAIN_REQUEST));
197195

198196
$this->assertFalse($response->headers->has('Expires'));
199197
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
@@ -213,19 +211,15 @@ public function testSurrogateMainRequestIsPublic()
213211
public function testGetSessionIsCalledOnce()
214212
{
215213
$session = $this->createMock(Session::class);
216-
$sessionStorage = $this->createMock(NativeSessionStorage::class);
214+
$sessionFactory = $this->createMock(SessionFactory::class);
215+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
217216
$kernel = $this->createMock(KernelInterface::class);
218217

219-
$sessionStorage->expects($this->once())
220-
->method('setOptions')
221-
->with(['cookie_secure' => true]);
222-
223218 F438
$requestStack = new RequestStack();
224219
$requestStack->push($mainRequest = new Request([], [], [], [], [], ['HTTPS' => 'on']));
225220

226221
$container = new Container();
227-
$container->set('session_storage', $sessionStorage);
228-
$container->set('session', $session);
222+
$container->set('session_factory', $sessionFactory);
229223
$container->set('request_stack', $requestStack);
230224

231225
$event = new RequestEvent($kernel, $mainRequest, HttpKernelInterface::MAIN_REQUEST);
@@ -234,9 +228,6 @@ public function testGetSessionIsCalledOnce()
234228
$listener->onKernelRequest($event);
235229

236230
// storage->setOptions() should have been called already
237-
$container->set('session_storage', null);
238-
$sessionStorage = null;
239-
240231
$subRequest = $mainRequest->duplicate();
241232
// at this point both main and subrequest have a closure to build the session
242233

@@ -249,10 +240,12 @@ public function testGetSessionIsCalledOnce()
249240
public function testSessionUsageExceptionIfStatelessAndSessionUsed()
250241
{
251242
$session = $this->createMock(Session::class);
252-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
243+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
244+
$sessionFactory = $this->createMock(SessionFactory::class);
245+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
253246

254247
$container = new Container();
255-
$container->set('initialized_session', $session);
248+
$container->set('session_factory', $sessionFactory);
256249

257250
$listener = new SessionListener($container, true);
258251
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -268,13 +261,15 @@ public function testSessionUsageExceptionIfStatelessAndSessionUsed()
268261
public function testSessionUsageLogIfStatelessAndSessionUsed()
269262
{
270263
$session = $this->createMock(Session::class);
271-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
264+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
265+
$sessionFactory = $this->createMock(SessionFactory::class);
266+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
272267

273268
$logger = $this->createMock(LoggerInterface::class);
274269
$logger->expects($this->exactly(1))->method('warning');
275270

276271
$container = new Container();
277-
$container->set('initialized_session', $session);
272+
$container->set('session_factory', $sessionFactory);
278273
$container->set('logger', $logger);
279274

280275
$listener = new SessionListener($container, false);
@@ -291,11 +286,13 @@ public function testSessionIsSavedWhenUnexpectedSessionExceptionThrown()
291286
{
292287
$session = $this->createMock(Session::class);
293288
$session->method('isStarted')->willReturn(true);
294-
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
289+
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
295290
$session->expects($this->exactly(1))->method('save');
291+
$sessionFactory = $this->createMock(SessionFactory::class);
292+
$sessionFactory->expects($this->once())->method('createSession')->willReturn($session);
296293

297294
$container = new Container();
298-
$container->set('initialized_session', $session);
295+
$container->set('session_factory', $sessionFactory);
299296

300297
$listener = new SessionListener($container, true);
301298
$kernel = $this->createMock(HttpKernelInterface::class);
@@ -323,13 +320,13 @@ public function testSessionUsageCallbackWhenDebugAndStateless()
323320

324321
$requestStack->push(new Request());
325322
$requestStack->push($request);
326-
$requestStack->push(new Request());
323+
$requestStack->push($subRequest = new Request());
324+
$subRequest->setSession($session);
327325

328326
$collector = $this->createMock(RequestDataCollector::class);
329327
$collector->expects($this->once())->method('collectSessionUsage');
330328

331329
$container = new Container();
332-
$container->set('initialized_session', $session);
333330
$container->set('request_stack', $requestStack);
334331
$container->set('session_collector', \Closure::fromCallable([$collector, 'collectSessionUsage']));
335332

@@ -353,7 +350,6 @@ public function testSessionUsageCallbackWhenNoDebug()
353350
$collector->expects($this->never())->method('collectSessionUsage');
354351

355352
$container = new Container();
356-
$container->set('initialized_session', $session);
357353
$container->set('request_stack', $requestStack);
358354
$container->set('session_collector', $collector);
359355

@@ -371,7 +367,6 @@ public function testSessionUsageCallbackWhenNoStateless()
371367
$requestStack->push(new Request());
372368

373369
$container = new Container();
374-
$container->set('initialized_session', $session);
375370
$container->set('request_stack', $requestStack);
376371

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

0 commit comments

Comments
 (0)
0