Description
Hello,
I have a private library for handling translations, which works in a way where you have a translatable entity and a collection of translation entities for it, each for a different locale. Each time the translatable entity is being created or modified via Doctrine's Unit of Work, the current locale is being checked and either a new translation is created or an existing one is modified. The locale is checked like so, through a dedicated service:
final class LocaleProvider
{
private const SESSION_KEY = 'key';
private RequestStack $requestStack;
private string $defaultLocale; // `%kernel.default_locale%`
public function getLocale(): string
{
$savedLocale = $this->getSavedLocale();
if (null !== $savedLocale) {
return $savedLocale;
}
$request = $this->requestStack->getCurrentRequest();
if (null !== $request) {
return $request->getLocale();
}
return $this->defaultLocale;
}
private function getSavedLocale(): ?string
{
$session = $this->getSession();
if (null === $session) {
return null;
}
return $session->get(self::SESSION_KEY);
}
private function getSession(): ?SessionInterface
{
$currentRequest = $this->requestStack->getCurrentRequest();
if (null === $currentRequest) {
return null;
}
return $currentRequest->getSession();
}
}
The library worked just fine until I switched from accessing the session
service directly from the container to accessing the instance from the request_stack
, as seen above. Basically, $this->requestStack->getCurrentRequest()
(or any other of it's methods) never returns an actual request. From analyzing the code it seems that there is a RequestStack
being created in the HttpKernel
used by Symfony module, but from what I have checked it is not the same instance that gets injected into my LocaleProvider
service. Is there anything that I am missing here?