Last active
December 1, 2023 09:52
-
-
Save bendavies/536a20f7a2d5db85db093dce1897f758 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use Symfony\Component\HttpKernel\Event\ControllerEvent; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
class SessionListener | |
{ | |
public function __construct( | |
private TokenStorageInterface $tokenStorage, | |
) { | |
} | |
public function onKernelController(ControllerEvent $event): void | |
{ | |
if ($this->canCloseSession($event)) { | |
$event->getRequest()->getSession()->save(); | |
} | |
} | |
protected function canCloseSession(ControllerEvent $event): bool | |
{ | |
$request = $event->getRequest(); | |
return $request->hasSession() | |
&& $request->getSession()->isStarted() | |
&& 'GET' === $request->server->get('REQUEST_METHOD') | |
&& 'logout' !== $request->get('_route') | |
&& $this->tokenStorage->getToken(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment