Skip to content

Commit 01f978e

Browse files
committed
Deprecat service "session"
1 parent 9c5de45 commit 01f978e

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

Authentication/Token/Storage/UsageTrackingTokenStorage.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Core\Authentication\Token\Storage;
1313

1414
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\HttpFoundation\RequestStack;
1516
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1617
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1718
use Symfony\Contracts\Service\ServiceSubscriberInterface;
@@ -24,13 +25,13 @@
2425
final class UsageTrackingTokenStorage implements TokenStorageInterface, ServiceSubscriberInterface
2526
{
2627
private $storage;
27-
private $sessionLocator;
28+
private $container;
2829
private $enableUsageTracking = false;
2930

30-
public function __construct(TokenStorageInterface $storage, ContainerInterface $sessionLocator)
31+
public function __construct(TokenStorageInterface $storage, ContainerInterface $container)
3132
{
3233
$this->storage = $storage;
33-
$this->sessionLocator = $sessionLocator;
34+
$this->container = $container;
3435
}
3536

3637
/**
@@ -40,7 +41,7 @@ public function getToken(): ?TokenInterface
4041
{
4142
if ($this->enableUsageTracking) {
4243
// increments the internal session usage index
43-
$this->sessionLocator->get('session')->getMetadataBag();
44+
$this->getSession()->getMetadataBag();
4445
}
4546

4647
return $this->storage->getToken();
@@ -55,7 +56,7 @@ public function setToken(TokenInterface $token = null): void
5556

5657
if ($token && $this->enableUsageTracking) {
5758
// increments the internal session usage index
58-
$this->sessionLocator->get('session')->getMetadataBag();
59+
$this->getSession()->getMetadataBag();
5960
}
6061
}
6162

@@ -72,7 +73,19 @@ public function disableUsageTracking(): void
7273
public static function getSubscribedServices(): array
7374
{
7475
return [
75-
'session' => SessionInterface::class,
76+
'request_stack' => RequestStack::class,
7677
];
7778
}
79+
80+
private function getSession(): SessionInterface
81+
{
82+
// BC for symfony/security-bundle < 5.3
83+
if ($this->container->has('session')) {
84+
trigger_deprecation('symfony/security-core', '5.3', 'Injecting the "session" in "%s" is deprecated, inject the "request_stack" instead.', __CLASS__);
85+
86+
return $this->container->get('session');
87+
}
88+
89+
return $this->container->get('request_stack')->getSession();
90+
}
7891
}

Tests/Authentication/Token/Storage/UsageTrackingTokenStorageTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Container\ContainerInterface;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1618
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1719
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1820
use Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage;
@@ -24,14 +26,19 @@ class UsageTrackingTokenStorageTest extends TestCase
2426
public function testGetSetToken()
2527
{
2628
$sessionAccess = 0;
27-
$sessionLocator = new class(['session' => function () use (&$sessionAccess) {
29+
$sessionLocator = new class(['request_stack' => function () use (&$sessionAccess) {
2830
++$sessionAccess;
2931

3032
$session = $this->createMock(SessionInterface::class);
3133
$session->expects($this->once())
3234
->method('getMetadataBag');
3335

34-
return $session;
36+
$request = new Request();
37+
$request->setSession($session);
38+
$requestStack = new RequestStack();
39+
$requestStack->push($request);
40+
41+
return $requestStack;
3542
}]) implements ContainerInterface {
3643
use ServiceLocatorTrait;
3744
};

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
"psr/container": "^1.0",
2727
"symfony/event-dispatcher": "^4.4|^5.0",
2828
"symfony/expression-language": "^4.4|^5.0",
29-
"symfony/http-foundation": "^4.4|^5.0",
29+
"symfony/http-foundation": "^5.3",
3030
"symfony/ldap": "^4.4|^5.0",
3131
"symfony/translation": "^4.4|^5.0",
3232
"symfony/validator": "^5.2",
3333
"psr/log": "~1.0"
3434
},
3535
"conflict": {
3636
"symfony/event-dispatcher": "<4.4",
37+
"symfony/http-foundation": "<5.3",
3738
"symfony/security-guard": "<4.4",
3839
"symfony/ldap": "<4.4",
3940
"symfony/validator": "<5.2"

0 commit comments

Comments
 (0)