Skip to content

Commit 3ab308f

Browse files
Introduce flashbag cache to Twig AppVariable
1 parent 5bf96bd commit 3ab308f

File tree

1 file changed

+50
-17
lines changed

1 file changed

+50
-17
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@
1717
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1818
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1919
use Symfony\Component\Security\Core\User\UserInterface;
20+
use Symfony\Contracts\Service\ResetInterface;
2021

2122
/**
2223
* Exposes some Symfony parameters and services as an "app" global variable.
2324
*
2425
* @author Fabien Potencier <fabien@symfony.com>
2526
*/
26-
class AppVariable
27+
class AppVariable implements ResetInterface
2728
{
2829
private TokenStorageInterface $tokenStorage;
2930
private RequestStack $requestStack;
3031
private string $environment;
3132
private bool $debug;
33+
private array $flashBagCache = [];
3234

3335
public function setTokenStorage(TokenStorageInterface $tokenStorage)
3436
{
@@ -135,25 +137,25 @@ public function getDebug(): bool
135137
*/
136138
public function getFlashes(string|array $types = null): array
137139
{
138-
try {
139-
if (null === $session = $this->getSession()) {
140-
return [];
141-
}
142-
} catch (\RuntimeException) {
143-
return [];
144-
}
145-
146-
if (null === $types || '' === $types || [] === $types) {
147-
return $session->getFlashBag()->all();
148-
}
140+
$flashes = $this->getFlashesUncached($types);
141+
$result = $flashes;
149142

150143
if (\is_string($types)) {
151-
return $session->getFlashBag()->get($types);
152-
}
144+
if (isset($this->flashBagCache[$types])) {
145+
$result = [...$result, ...$this->flashes[$types]];
146+
}
153147

154-
$result = [];
155-
foreach ($types as $type) {
156-
$result[$type] = $session->getFlashBag()->get($type);
148+
$this->flashBagCache[$types] = [...$flashes, ...($this->flashBagCache[$types] ?? [])];
149+
} else {
150+
foreach ($this->flashBagCache as $key => $values) {
151+
if ($types === null || \in_array($key, $types)) {
152+
$result[$key] = [...$values, ...$result[$key] ?? []];
153+
}
154+
}
155+
156+
foreach ($flashes as $key => $values) {
157+
$this->flashBagCache[$key] = [...$values, ...($this->flashBagCache[$key] ?? [])];
158+
}
157159
}
158160

159161
return $result;
@@ -179,4 +181,35 @@ public function getCurrent_route_parameters(): array
179181

180182
return $this->getRequest()?->attributes->get('_route_params') ?? [];
181183
}
184+
185+
public function reset(): void
186+
{
187+
$this->flashBagCache = [];
188+
}
189+
190+
protected function getFlashesUncached(string|array $types = null): array
191+
{
192+
try {
193+
if (null === $session = $this->getSession()) {
194+
return [];
195+
}
196+
} catch (\RuntimeException) {
197+
return [];
198+
}
199+
200+
if (null === $types || '' === $types || [] === $types) {
201+
return $session->getFlashBag()->all();
202+
}
203+
204+
if (\is_string($types)) {
205+
return $session->getFlashBag()->get($types);
206+
}
207+
208+
$result = [];
209+
foreach ($types as $type) {
210+
$result[$type] = $session->getFlashBag()->get($type);
211+
}
212+
213+
return $result;
214+
}
182215
}

0 commit comments

Comments
 (0)