12
12
namespace Symfony \Component \Security \Core \Authentication \Token \Storage ;
13
13
14
14
use Psr \Container \ContainerInterface ;
15
+ use Symfony \Component \HttpFoundation \RequestStack ;
15
16
use Symfony \Component \HttpFoundation \Session \SessionInterface ;
16
17
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
17
18
use Symfony \Contracts \Service \ServiceSubscriberInterface ;
24
25
final class UsageTrackingTokenStorage implements TokenStorageInterface, ServiceSubscriberInterface
25
26
{
26
27
private $ storage ;
27
- private $ sessionLocator ;
28
+ private $ container ;
28
29
private $ enableUsageTracking = false ;
29
30
30
- public function __construct (TokenStorageInterface $ storage , ContainerInterface $ sessionLocator )
31
+ public function __construct (TokenStorageInterface $ storage , ContainerInterface $ container )
31
32
{
32
33
$ this ->storage = $ storage ;
33
- $ this ->sessionLocator = $ sessionLocator ;
34
+ $ this ->container = $ container ;
34
35
}
35
36
36
37
/**
@@ -40,7 +41,7 @@ public function getToken(): ?TokenInterface
40
41
{
41
42
if ($ this ->enableUsageTracking ) {
42
43
// increments the internal session usage index
43
- $ this ->sessionLocator -> get ( ' session ' )->getMetadataBag ();
44
+ $ this ->getSession ( )->getMetadataBag ();
44
45
}
45
46
46
47
return $ this ->storage ->getToken ();
@@ -55,7 +56,7 @@ public function setToken(TokenInterface $token = null): void
55
56
56
57
if ($ token && $ this ->enableUsageTracking ) {
57
58
// increments the internal session usage index
58
- $ this ->sessionLocator -> get ( ' session ' )->getMetadataBag ();
59
+ $ this ->getSession ( )->getMetadataBag ();
59
60
}
60
61
}
61
62
@@ -72,7 +73,19 @@ public function disableUsageTracking(): void
72
73
public static function getSubscribedServices (): array
73
74
{
74
75
return [
75
- 'session ' => SessionInterface ::class,
76
+ 'request_stack ' => RequestStack ::class,
76
77
];
77
78
}
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
+ }
78
91
}
0 commit comments