Skip to content

Commit 5c44ae0

Browse files
jderussenicolas-grekas
authored andcommitted
Remove deprecate session service
1 parent 3fc0438 commit 5c44ae0

File tree

6 files changed

+96
-116
lines changed

6 files changed

+96
-116
lines changed

EventListener/AbstractSessionListener.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ abstract class AbstractSessionListener implements EventSubscriberInterface, Rese
4444
public const NO_AUTO_CACHE_CONTROL_HEADER = 'Symfony-Session-NoAutoCacheControl';
4545

4646
protected $container;
47-
private $sessionUsageStack = [];
4847
private $debug;
4948

5049
/**
@@ -86,9 +85,6 @@ public function onKernelRequest(RequestEvent $event)
8685
return $sess;
8786
});
8887
}
89-
90-
$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : null;
91-
$this->sessionUsageStack[] = $session instanceof Session ? $session->getUsageIndex() : 0;
9288
}
9389

9490
public function onKernelResponse(ResponseEvent $event)
@@ -101,10 +97,10 @@ public function onKernelResponse(ResponseEvent $event)
10197
$autoCacheControl = !$response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER);
10298
// Always remove the internal header if present
10399
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
104-
105-
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $event->getRequest()->getSession()) {
100+
if (!$event->getRequest()->hasSession(true)) {
106101
return;
107102
}
103+
$session = $event->getRequest()->getSession();
108104

109105
if ($session->isStarted()) {
110106
/*
@@ -151,7 +147,7 @@ public function onKernelResponse(ResponseEvent $event)
151147
$request = $event->getRequest();
152148
$requestSessionCookieId = $request->cookies->get($sessionName);
153149

154-
if ($requestSessionCookieId && $session->isEmpty()) {
150+
if ($requestSessionCookieId && ($session instanceof Session ? $session->isEmpty() : empty($session->all()))) {
155151
$response->headers->clearCookie(
156152
$sessionName,
157153
$sessionCookiePath,
@@ -183,7 +179,7 @@ public function onKernelResponse(ResponseEvent $event)
183179
}
184180
}
185181

186-
if ($session instanceof Session ? $session->getUsageIndex() === end($this->sessionUsageStack) : !$session->isStarted()) {
182+
if ($session instanceof Session ? $session->getUsageIndex() === 0 : !$session->isStarted()) {
187183
return;
188184
}
189185

@@ -208,13 +204,6 @@ public function onKernelResponse(ResponseEvent $event)
208204
}
209205
}
210206

211-
public function onFinishRequest(FinishRequestEvent $event)
212-
{
213-
if ($event->isMainRequest()) {
214-
array_pop($this->sessionUsageStack);
215-
}
216-
}
217-
218207
public function onSessionUsage(): void
219208
{
220209
if (!$this->debug) {
@@ -239,7 +228,7 @@ public function onSessionUsage(): void
239228
return;
240229
}
241230

242-
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $requestStack->getCurrentRequest()->getSession()) {
231+
if (!$session = $requestStack->getCurrentRequest()->getSession()) {
243232
return;
244233
}
245234

@@ -256,7 +245,6 @@ public static function getSubscribedEvents(): array
256245
KernelEvents::REQUEST => ['onKernelRequest', 128],
257246
// low priority to come after regular response listeners, but higher than StreamedResponseListener
258247
KernelEvents::RESPONSE => ['onKernelResponse', -1000],
259-
KernelEvents::FINISH_REQUEST => ['onFinishRequest'],
260248
];
261249
}
262250

EventListener/AbstractTestSessionListener.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1515
use Symfony\Component\HttpFoundation\Cookie;
1616
use Symfony\Component\HttpFoundation\Session\Session;
17-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1817
use Symfony\Component\HttpKernel\Event\RequestEvent;
1918
use Symfony\Component\HttpKernel\Event\ResponseEvent;
2019
use Symfony\Component\HttpKernel\KernelEvents;
@@ -50,12 +49,11 @@ public function onKernelRequest(RequestEvent $event)
5049
}
5150

5251
// bootstrap the session
53-
if ($event->getRequest()->hasSession()) {
54-
$session = $event->getRequest()->getSession();
55-
} elseif (!$session = $this->getSession()) {
52+
if (!$event->getRequest()->hasSession()) {
5653
return;
5754
}
5855

56+
$session = $event->getRequest()->getSession();
5957
$cookies = $event->getRequest()->cookies;
6058

6159
if ($cookies->has($session->getName())) {
@@ -110,11 +108,4 @@ public static function getSubscribedEvents(): array
110108
KernelEvents::RESPONSE => ['onKernelResponse', -128],
111109
];
112110
}
113-
114-
/**
115-
* Gets the session object.
116-
*
117-
* @deprecated since Symfony 5.4, will be removed in 6.0.
118-
*/
119-
abstract protected function getSession(): ?SessionInterface;
120111
}

EventListener/SessionListener.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,14 @@
1818
/**
1919
* Sets the session in the request.
2020
*
21-
* When the passed container contains a "session_storage" entry which
22-
* holds a NativeSessionStorage instance, the "cookie_secure" option
23-
* will be set to true whenever the current main request is secure.
24-
*
2521
* @author Fabien Potencier <fabien@symfony.com>
2622
*
2723
* @final
2824
*/
2925
class SessionListener extends AbstractSessionListener
3026
{
31-
public function onKernelRequest(RequestEvent $event)
32-
{
33-
parent::onKernelRequest($event);
34-
35-
if (!$event->isMainRequest() || (!$this->container->has('session') && !$this->container->has('session_factory'))) {
36-
return;
37-
}
38-
39-
if ($this->container->has('session_storage')
40-
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
41-
&& ($mainRequest = $this->container->get('request_stack')->getMainRequest())
42-
&& $mainRequest->isSecure()
43-
) {
44-
$storage->setOptions(['cookie_secure' => true]);
45-
}
46-
}
47-
4827
protected function getSession(): ?SessionInterface
4928
{
50-
if ($this->container->has('session')) {
51-
return $this->container->get('session');
52-
}
53-
5429
if ($this->container->has('session_factory')) {
5530
return $this->container->get('session_factory')->createSession();
5631
}

EventListener/TestSessionListener.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1615

1716
/**
1817
* Sets the session in the request.
@@ -25,25 +24,8 @@
2524
*/
2625
class TestSessionListener extends AbstractTestSessionListener
2726
{
28-
private $container;
29-
3027
public function __construct(ContainerInterface $container, array $sessionOptions = [])
3128
{
32-
$this->container = $container;
3329
parent::__construct($sessionOptions);
3430
}
35-
36-
/**
37-
* @deprecated since Symfony 5.4, will be removed in 6.0.
38-
*/
39-
protected function getSession(): ?SessionInterface
40-
{
41-
trigger_deprecation('symfony/http-kernel', '5.4', '"%s" is deprecated and will be removed in 6.0, inject a session in the request instead.', __METHOD__);
42-
43-
if ($this->container->has('session')) {
44-
return $this->container->get('session');
45-
}
46-
47-
return null;
48-
}
4931
}

0 commit comments

Comments
 (0)