3 namespace Rapsys\UserBundle\Handler
;
5 use Symfony\Component\DependencyInjection\ContainerInterface
;
6 use Symfony\Component\HttpFoundation\RedirectResponse
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\Routing\Exception\ResourceNotFoundException
;
9 use Symfony\Component\Routing\RequestContext
;
10 use Symfony\Component\Routing\RouterInterface
;
11 use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler
;
13 use Rapsys\UserBundle\RapsysUserBundle
;
18 class LogoutSuccessHandler
extends DefaultLogoutSuccessHandler
{
37 public function __construct(ContainerInterface
$container, string $targetUrl = '/', RouterInterface
$router) {
39 $this->config
= $container->getParameter(self
::getAlias());
42 $this->targetUrl
= $targetUrl;
45 $this->router
= $router;
51 public function onLogoutSuccess(Request
$request) {
52 //Retrieve logout route
53 $logout = $request->get('_route');
55 //Extract and process referer
56 if (($referer = $request->headers
->get('referer'))) {
57 //Create referer request instance
58 $req = Request
::create($referer);
61 $path = $req->getPathInfo();
63 //Get referer query string
64 $query = $req->getQueryString();
67 $path = str_replace($request->getScriptName(), '', $path);
69 //Try with referer path
72 $oldContext = $this->router
->getContext();
75 //XXX: prevent MethodNotAllowedException on GET only routes because our context method is POST
76 //XXX: see vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php +42
77 $this->router
->setContext(new RequestContext());
79 //Retrieve route matching path
80 $route = $this->router
->match($path);
83 $this->router
->setContext($oldContext);
88 //Without logout route name
89 if (($name = $route['_route']) != $logout) {
90 //Remove route and controller from route defaults
91 unset($route['_route'], $route['_controller'], $route['_canonical_route']);
94 $url = $this->router
->generate($name, $route);
96 //Return generated route
97 return new RedirectResponse($url, 302);
98 //With logout route name
100 //Unset referer and route
101 unset($referer, $route);
104 } catch (ResourceNotFoundException
$e) {
105 //Unset referer and route
106 unset($referer, $route);
110 //With index route from config
111 if (!empty($name = $this->config
['route']['index']['name']) && is_array($context = $this->config
['route']['index']['context'])) {
112 //Without logout route name
113 if (($name = $route['_route']) != $logout) {
117 $url = $this->router
->generate($name, $context);
119 //Return generated route
120 return new RedirectResponse($url, 302);
122 } catch (ResourceNotFoundException
$e) {
123 //Unset name and context
124 unset($name, $context);
126 //With logout route name
128 //Unset name and context
129 unset($name, $context);
136 $oldContext = $this->router
->getContext();
138 //Force clean context
139 //XXX: prevent MethodNotAllowedException on GET only routes because our context method is POST
140 //XXX: see vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php +42
141 $this->router
->setContext(new RequestContext());
143 //Retrieve route matching target url
144 $route = $this->router
->match($this->targetUrl
);
147 $this->router
->setContext($oldContext);
152 //Without logout route name
153 if (($name = $route['_route']) != $logout) {
154 //Remove route and controller from route defaults
155 unset($route['_route'], $route['_controller'], $route['_canonical_route']);
158 $url = $this->router
->generate($name, $route);
160 //Return generated route
161 return new RedirectResponse($url, 302);
162 //With logout route name
164 //Unset name and route
165 unset($name, $route);
167 //Get first route from route collection if / path was not matched
168 } catch (ResourceNotFoundException
$e) {
169 //Unset name and route
170 unset($name, $route);
174 throw new \
RuntimeException('You must provide a valid logout target url or route name');
180 public function getAlias(): string {
181 return RapsysUserBundle
::getAlias();