16
16
use Symfony \Component \Form \Extension \Csrf \CsrfProvider \CsrfProviderInterface ;
17
17
use Symfony \Component \HttpFoundation \RequestStack ;
18
18
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
19
+ use Symfony \Component \Security \Http \Logout \LogoutUrlGenerator ;
19
20
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
20
21
use Symfony \Component \Security \Csrf \CsrfTokenManagerInterface ;
22
+ use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
21
23
use Symfony \Component \Templating \Helper \Helper ;
22
24
23
25
/**
@@ -35,45 +37,21 @@ class LogoutUrlHelper extends Helper
35
37
/**
36
38
* Constructor.
37
39
*
38
- * @param ContainerInterface|RequestStack $requestStack A ContainerInterface instance or RequestStack
39
- * @param UrlGeneratorInterface $router The router service
40
- * @param TokenStorageInterface|null $tokenStorage The token storage service
40
+ * @param ContainerInterface|LogoutUrlGenerator $generator A ContainerInterface or LogoutUrlGenerator instance
41
+ * @param UrlGeneratorInterface|null $router The router service
42
+ * @param TokenStorageInterface|null $tokenStorage The token storage service
41
43
*
42
44
* @deprecated Passing a ContainerInterface as a first argument is deprecated since 2.7 and will be removed in 3.0.
45
+ * @deprecated Passing a second and third argument is deprecated since 2.7 and will be removed in 3.0.
43
46
*/
44
- public function __construct ($ requestStack , UrlGeneratorInterface $ router , TokenStorageInterface $ tokenStorage = null )
47
+ public function __construct ($ generator , UrlGeneratorInterface $ router = null , TokenStorageInterface $ tokenStorage = null )
45
48
{
46
49
if ($ requestStack instanceof ContainerInterface) {
47
- $ this ->requestStack = $ requestStack ->get ('request_stack ' );
50
+ $ this ->generator = $ container ->get ('security.logout_generator ' );
48
51
trigger_error ('The ' .__CLASS__ .' constructor will require a RequestStack instead of a ContainerInterface instance in 3.0. ' , E_USER_DEPRECATED );
49
- } elseif ($ requestStack instanceof RequestStack) {
50
- $ this ->requestStack = $ requestStack ;
51
52
} else {
52
- throw new \InvalidArgumentException (sprintf ('%s takes either a RequestStack or a ContainerInterface object as its first argument. ' , __METHOD__ ));
53
- }
54
-
55
- $ this ->router = $ router ;
56
- $ this ->tokenStorage = $ tokenStorage ;
57
- }
58
-
59
- /**
60
- * Registers a firewall's LogoutListener, allowing its URL to be generated.
61
- *
62
- * @param string $key The firewall key
63
- * @param string $logoutPath The path that starts the logout process
64
- * @param string $csrfTokenId The ID of the CSRF token
65
- * @param string $csrfParameter The CSRF token parameter name
66
- * @param CsrfTokenManagerInterface $csrfTokenManager A CsrfTokenManagerInterface instance
67
- */
68
- public function registerListener ($ key , $ logoutPath , $ csrfTokenId , $ csrfParameter , $ csrfTokenManager = null )
69
- {
70
- if ($ csrfTokenManager instanceof CsrfProviderInterface) {
71
- $ csrfTokenManager = new CsrfProviderAdapter ($ csrfTokenManager );
72
- } elseif (null !== $ csrfTokenManager && !$ csrfTokenManager instanceof CsrfTokenManagerInterface) {
73
- throw new \InvalidArgumentException ('The CSRF token manager should be an instance of CsrfProviderInterface or CsrfTokenManagerInterface. ' );
53
+ $ this ->generator = $ generator ;
74
54
}
75
-
76
- $ this ->listeners [$ key ] = array ($ logoutPath , $ csrfTokenId , $ csrfParameter , $ csrfTokenManager );
77
55
}
78
56
79
57
/**
@@ -85,7 +63,7 @@ public function registerListener($key, $logoutPath, $csrfTokenId, $csrfParameter
85
63
*/
86
64
public function getLogoutPath ($ key )
87
65
{
88
- return $ this ->generateLogoutUrl ($ key , UrlGeneratorInterface::ABSOLUTE_PATH );
66
+ return $ this ->generator -> getLogoutPath ($ key , UrlGeneratorInterface::ABSOLUTE_PATH );
89
67
}
90
68
91
69
/**
@@ -97,54 +75,7 @@ public function getLogoutPath($key)
97
75
*/
98
76
public function getLogoutUrl ($ key )
99
77
{
100
- return $ this ->generateLogoutUrl ($ key , UrlGeneratorInterface::ABSOLUTE_URL );
101
- }
102
-
103
- /**
104
- * Generates the logout URL for the firewall.
105
- *
106
- * @param string|null $key The firewall key or null to use the current firewall key
107
- * @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
108
- *
109
- * @return string The logout URL
110
- *
111
- * @throws \InvalidArgumentException if no LogoutListener is registered for the key or the key could not be found automatically.
112
- */
113
- private function generateLogoutUrl ($ key , $ referenceType )
114
- {
115
- // Fetch the current provider key from token, if possible
116
- if (null === $ key && null !== $ this ->tokenStorage ) {
117
- $ token = $ this ->tokenStorage ->getToken ();
118
- if (null !== $ token && method_exists ($ token , 'getProviderKey ' )) {
119
- $ key = $ token ->getProviderKey ();
120
- }
121
- }
122
-
123
- if (null === $ key ) {
124
- throw new \InvalidArgumentException ('Unable to find the current firewall LogoutListener, please provide the provider key manually. ' );
125
- }
126
-
127
- if (!array_key_exists ($ key , $ this ->listeners )) {
128
- throw new \InvalidArgumentException (sprintf ('No LogoutListener found for firewall key "%s". ' , $ key ));
129
- }
130
-
131
- list ($ logoutPath , $ csrfTokenId , $ csrfParameter , $ csrfTokenManager ) = $ this ->listeners [$ key ];
132
-
133
- $ parameters = null !== $ csrfTokenManager ? array ($ csrfParameter => (string ) $ csrfTokenManager ->getToken ($ csrfTokenId )) : array ();
134
-
135
- if ('/ ' === $ logoutPath [0 ]) {
136
- $ request = $ this ->requestStack ->getCurrentRequest ();
137
-
138
- $ url = UrlGeneratorInterface::ABSOLUTE_URL === $ referenceType ? $ request ->getUriForPath ($ logoutPath ) : $ request ->getBasePath ().$ logoutPath ;
139
-
140
- if (!empty ($ parameters )) {
141
- $ url .= '? ' .http_build_query ($ parameters );
142
- }
143
- } else {
144
- $ url = $ this ->router ->generate ($ logoutPath , $ parameters , $ referenceType );
145
- }
146
-
147
- return $ url ;
78
+ return $ this ->generator ->getLogoutUrl ($ key , UrlGeneratorInterface::ABSOLUTE_URL );
148
79
}
149
80
150
81
/**
0 commit comments