mirror of
https://github.com/SuiteCRM/SuiteCRM-Core.git
synced 2025-09-02 08:09:19 +08:00
Symfony 6.4 - Update Session dependency with RequestStack dependency
This commit is contained in:
parent
57865bfc95
commit
6841cd8582
4 changed files with 84 additions and 31 deletions
|
@ -130,7 +130,7 @@ class UserHandler extends LegacyHandler
|
|||
*/
|
||||
public function getSessionLanguage(): string
|
||||
{
|
||||
return $this->session->get('ui_language', '');
|
||||
return $this->requestStack->get('ui_language', '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -142,7 +142,7 @@ class UserHandler extends LegacyHandler
|
|||
set_current_language($language);
|
||||
$this->close();
|
||||
|
||||
$this->session->set('ui_language', $language);
|
||||
$this->requestStack->set('ui_language', $language);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -34,7 +34,12 @@ use RuntimeException;
|
|||
use SugarApplication;
|
||||
use SugarController;
|
||||
use SugarThemeRegistry;
|
||||
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
|
||||
use User;
|
||||
|
||||
/**
|
||||
|
@ -74,7 +79,7 @@ abstract class LegacyHandler
|
|||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $session;
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* LegacyHandler constructor.
|
||||
|
@ -83,7 +88,7 @@ abstract class LegacyHandler
|
|||
* @param string $legacySessionName
|
||||
* @param string $defaultSessionName
|
||||
* @param LegacyScopeState $legacyScopeState
|
||||
* @param RequestStack $session
|
||||
* @param RequestStack $requestStack
|
||||
*/
|
||||
public function __construct(
|
||||
string $projectDir,
|
||||
|
@ -91,14 +96,14 @@ abstract class LegacyHandler
|
|||
string $legacySessionName,
|
||||
string $defaultSessionName,
|
||||
LegacyScopeState $legacyScopeState,
|
||||
RequestStack $session
|
||||
RequestStack $requestStack
|
||||
) {
|
||||
$this->projectDir = $projectDir;
|
||||
$this->legacyDir = $legacyDir;
|
||||
$this->legacySessionName = $legacySessionName;
|
||||
$this->defaultSessionName = $defaultSessionName;
|
||||
$this->state = $legacyScopeState;
|
||||
$this->session = $session;
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -294,36 +299,40 @@ abstract class LegacyHandler
|
|||
|
||||
protected function startSymfonySession(): void
|
||||
{
|
||||
if ($this->session->isStarted()) {
|
||||
$this->session->save();
|
||||
$session = $this->getSession();
|
||||
|
||||
if ($session->isStarted()) {
|
||||
$session->save();
|
||||
session_write_close();
|
||||
}
|
||||
|
||||
$this->session->setName($this->defaultSessionName);
|
||||
$session->setName($this->defaultSessionName);
|
||||
|
||||
if (isset($_COOKIE[$this->defaultSessionName])) {
|
||||
$this->session->setId($_COOKIE[$this->defaultSessionName]);
|
||||
$session->setId($_COOKIE[$this->defaultSessionName]);
|
||||
}
|
||||
|
||||
$this->session->start();
|
||||
$session->start();
|
||||
}
|
||||
|
||||
protected function startLegacySession(): void
|
||||
{
|
||||
if ($this->session->isStarted()) {
|
||||
$this->session->save();
|
||||
$session = $this->getSession();
|
||||
|
||||
if ($session->isStarted()) {
|
||||
$session->save();
|
||||
}
|
||||
|
||||
if (session_status() === PHP_SESSION_ACTIVE) {
|
||||
return;
|
||||
}
|
||||
$this->session->setName($this->legacySessionName);
|
||||
$session->setName($this->legacySessionName);
|
||||
|
||||
if (!isset($_COOKIE[$this->legacySessionName])) {
|
||||
$_COOKIE[$this->legacySessionName] = session_create_id();
|
||||
}
|
||||
$this->session->setId($_COOKIE[$this->legacySessionName]);
|
||||
$this->session->start();
|
||||
$session->setId($_COOKIE[$this->legacySessionName]);
|
||||
$session->start();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -341,4 +350,48 @@ abstract class LegacyHandler
|
|||
|
||||
$app_strings = disable_translations($app_strings);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SessionInterface
|
||||
*/
|
||||
protected function getSession(): SessionInterface
|
||||
{
|
||||
$request = Request::createFromGlobals();
|
||||
$requestSession = null;
|
||||
try {
|
||||
$requestSession = $request->getSession();
|
||||
} catch (SessionNotFoundException $e) {
|
||||
}
|
||||
|
||||
$session = null;
|
||||
|
||||
if($requestSession === null || session_id() == '' || !isset($_SESSION) || session_status() === PHP_SESSION_NONE) {
|
||||
// session isn't started
|
||||
session_start();
|
||||
|
||||
// Get Symfony to interface with this existing session
|
||||
$session = new Session(new PhpBridgeSessionStorage());
|
||||
|
||||
// symfony will now interface with the existing PHP session
|
||||
$session->start();
|
||||
$request->setSession($session);
|
||||
}
|
||||
|
||||
|
||||
$stack = $this->requestStack ?? null;
|
||||
if ($stack === null) {
|
||||
$stack = new RequestStack();
|
||||
}
|
||||
|
||||
if ($requestSession === null) {
|
||||
$stack->push($request);
|
||||
return $session;
|
||||
}
|
||||
|
||||
|
||||
$session = $this->requestStack->getMainRequest()->getSession();
|
||||
|
||||
|
||||
return $session;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ abstract class BaseCommand extends Command
|
|||
/**
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $session;
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
|
@ -86,11 +86,11 @@ abstract class BaseCommand extends Command
|
|||
|
||||
/**
|
||||
* @required
|
||||
* @param RequestStack $session
|
||||
* @param RequestStack $requestStack
|
||||
*/
|
||||
public function setSession(RequestStack $session): void
|
||||
public function setRequestStack(RequestStack $requestStack): void
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -163,13 +163,13 @@ abstract class BaseCommand extends Command
|
|||
*/
|
||||
protected function startSession(): void
|
||||
{
|
||||
if ($this->session->isStarted()) {
|
||||
if ($this->requestStack->getSession()->isStarted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->session->setName($this->defaultSessionName);
|
||||
$this->requestStack->getSession()->setName($this->defaultSessionName);
|
||||
|
||||
$this->session->start();
|
||||
$this->requestStack->getSession()->start();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -78,7 +78,7 @@ class InstallHandler extends LegacyHandler
|
|||
* @param string $legacySessionName
|
||||
* @param string $defaultSessionName
|
||||
* @param LegacyScopeState $legacyScopeState
|
||||
* @param RequestStack $session
|
||||
* @param RequestStack $requestStack
|
||||
* @param LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(
|
||||
|
@ -87,7 +87,7 @@ class InstallHandler extends LegacyHandler
|
|||
string $legacySessionName,
|
||||
string $defaultSessionName,
|
||||
LegacyScopeState $legacyScopeState,
|
||||
RequestStack $session,
|
||||
RequestStack $requestStack,
|
||||
LoggerInterface $logger
|
||||
)
|
||||
{
|
||||
|
@ -97,7 +97,7 @@ class InstallHandler extends LegacyHandler
|
|||
$legacySessionName,
|
||||
$defaultSessionName,
|
||||
$legacyScopeState,
|
||||
$session
|
||||
$requestStack
|
||||
);
|
||||
$this->legacyDir = $legacyDir;
|
||||
$this->logger = $logger;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue