Commit 81706728 authored by Florian Vick's avatar Florian Vick
Browse files

try to ensure component locale persistence using session

parent bd9a66b8
<?php
declare(strict_types=1);
namespace RoflCopter24\SymfonyLivewireBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LocaleSubscriber implements EventSubscriberInterface
{
private string $defaultLocale;
public function __construct(string $defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
// try to see if the locale has been set as a _locale routing parameter
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
// if no explicit locale has been set on this request, use one from the session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents(): array
{
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 19]],
];
}
}
......@@ -12,6 +12,8 @@ use RoflCopter24\SymfonyLivewireBundle\Registry\HydrationMiddlewareRegistryInter
use RoflCopter24\SymfonyLivewireBundle\Registry\LivewireComponentRegistryInterface;
use RoflCopter24\SymfonyLivewireBundle\Util\UuidUtil;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class LifecycleManager
{
......@@ -29,13 +31,20 @@ class LifecycleManager
* @var LoggerInterface
*/
private LoggerInterface $logger;
private string $locale;
/**
* @var SessionInterface
*/
private SessionInterface $session;
public function __construct(LivewireComponentRegistryInterface $registry,
HydrationMiddlewareRegistryInterface $hydrationMiddlewareRegistry,
HydrationMiddlewareRegistryInterface $initialHydrationMiddlewareRegistry,
HydrationMiddlewareRegistryInterface $initialDehydrationMiddlewareRegistry,
ContainerInterface $container,
LoggerInterface $logger)
LoggerInterface $logger,
RequestStack $requestStack,
SessionInterface $session)
{
$this->componentRegistry = $registry;
$this->hydrationRegistry = $hydrationMiddlewareRegistry;
......@@ -43,6 +52,8 @@ class LifecycleManager
$this->initialDehydrationRegistry = $initialDehydrationMiddlewareRegistry;
$this->container = $container;
$this->logger = $logger;
$this->locale = $requestStack->getMasterRequest()->getLocale();
$this->session = $session;
}
/**
......@@ -72,7 +83,10 @@ class LifecycleManager
}
$component->setId(UuidUtil::generate());
$initialRequestData = LivewireRequestData::empty($component->getId(), $componentName);
$this->session->set('_locale', $this->locale);
$initialRequestData = LivewireRequestData::empty($component->getId(), $componentName, $this->locale ?? 'en');
$this->logger->debug("[Livewire] Initial hydration for $componentName (ServiceId $componentServiceId).");
$this->initialHydrate($component, $initialRequestData);
......
......@@ -30,7 +30,9 @@ services:
'@livewire.middleware.initial_hydration_registry',
'@livewire.middleware.initial_dehydration_registry',
'@Symfony\Component\DependencyInjection\ContainerInterface',
'@logger'
'@logger',
'@request_stack',
'@session'
]
RoflCopter24\SymfonyLivewireBundle\Manager\LifecycleManager:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment