Skip to content

Navigation Menu

Sign in
Appearance settings
Sign up
Appearance settings

Commit 698c773

Browse filesBrowse the repository at this point in the historyBrowse files
lazergnicolas-grekas
authored andcommitted
[HttpKernel] Fix regression when a locale aware service is never initialized
1 parent 38bc4c0 commit 698c773
Copy full SHA for 698c773

2 files changed

+65-1Lines changed: 65 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Symfony/Component/HttpKernel/EventListener/LocaleAwareListener.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/LocaleAwareListener.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class LocaleAwareListener implements EventSubscriberInterface
2828
private iterable $localeAwareServices;
2929
private RequestStack $requestStack;
3030
private array $storedLocales = [];
31+
private array $initializedServices = [];
3132

3233
/**
3334
* @param iterable<mixed, LocaleAwareInterface> $localeAwareServices
@@ -44,7 +45,10 @@ public function onKernelRequest(RequestEvent $event): void
4445
$locales = [];
4546

4647
foreach ($this->localeAwareServices as $key => $service) {
47-
$locales[$key] = $service->getLocale();
48+
// a service the listener never set can hold no locale to restore
49+
if (isset($this->initializedServices[$key])) {
50+
$locales[$key] = $service->getLocale();
51+
}
4852
}
4953

5054
$this->storedLocales[spl_object_id($event->getRequest())] = $locales;
@@ -86,6 +90,8 @@ private function setLocale(string $locale, string $defaultLocale, array $storedL
8690
} catch (\InvalidArgumentException) {
8791
$service->setLocale($defaultLocale);
8892
}
93+
94+
$this->initializedServices[$key] = true;
8995
}
9096
}
9197
}
Collapse file

‎src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleAwareListenerTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/EventListener/LocaleAwareListenerTest.php
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,64 @@ public function getLocale(): string
148148
$this->assertSame('fr', $service->getLocale());
149149
}
150150

151+
public function testSubRequestDoesNotFailOnAServiceWithAnUninitializedLocale()
152+
{
153+
$service = new class implements LocaleAwareInterface {
154+
private string $locale;
155+
156+
public function setLocale(string $locale): void
157+
{
158+
$this->locale = $locale;
159+
}
160+
161+
public function getLocale(): string
162+
{
163+
return $this->locale;
164+
}
165+
};
166+
$listener = new LocaleAwareListener(new \ArrayIterator([$service]), $this->requestStack);
167+
$kernel = $this->createStub(HttpKernelInterface::class);
168+
169+
$this->requestStack->push($mainRequest = $this->createRequest('fr'));
170+
$this->requestStack->push($subRequest = $this->createRequest('de'));
171+
$listener->onKernelRequest(new RequestEvent($kernel, $subRequest, HttpKernelInterface::SUB_REQUEST));
172+
173+
$this->assertSame('de', $service->getLocale());
174+
175+
$listener->onKernelFinishRequest(new FinishRequestEvent($kernel, $subRequest, HttpKernelInterface::SUB_REQUEST));
176+
$this->requestStack->pop();
177+
178+
$this->assertSame('fr', $service->getLocale());
179+
}
180+
181+
public function testSubRequestDoesNotHideAnUnrelatedErrorThrownByGetLocale()
182+
{
183+
$service = new class implements LocaleAwareInterface {
184+
private ?LocaleAwareInterface $inner = null;
185+
186+
public function setLocale(string $locale): void
187+
{
188+
}
189+
190+
public function getLocale(): string
191+
{
192+
return $this->inner->getLocale();
193+
}
194+
};
195+
$listener = new LocaleAwareListener(new \ArrayIterator([$service]), $this->requestStack);
196+
$kernel = $this->createStub(HttpKernelInterface::class);
197+
198+
$this->requestStack->push($mainRequest = $this->createRequest('fr'));
199+
$listener->onKernelRequest(new RequestEvent($kernel, $mainRequest, HttpKernelInterface::MAIN_REQUEST));
200+
201+
$this->requestStack->push($subRequest = $this->createRequest('de'));
202+
203+
$this->expectException(\Error::class);
204+
$this->expectExceptionMessage('Call to a member function getLocale() on null');
205+
206+
$listener->onKernelRequest(new RequestEvent($kernel, $subRequest, HttpKernelInterface::SUB_REQUEST));
207+
}
208+
151209
private function createRequest(string $locale): Request
152210
{
153211
$request = new Request();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.