17
17
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
18
18
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
19
19
use Symfony \Component \Security \Core \User \UserInterface ;
20
+ use Symfony \Contracts \Service \ResetInterface ;
20
21
21
22
/**
22
23
* Exposes some Symfony parameters and services as an "app" global variable.
23
24
*
24
25
* @author Fabien Potencier <fabien@symfony.com>
25
26
*/
26
- class AppVariable
27
+ class AppVariable implements ResetInterface
27
28
{
28
29
private TokenStorageInterface $ tokenStorage ;
29
30
private RequestStack $ requestStack ;
30
31
private string $ environment ;
31
32
private bool $ debug ;
33
+ private array $ flashBagCache = [];
32
34
33
35
public function setTokenStorage (TokenStorageInterface $ tokenStorage )
34
36
{
@@ -135,25 +137,25 @@ public function getDebug(): bool
135
137
*/
136
138
public function getFlashes (string |array $ types = null ): array
137
139
{
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 ;
149
142
150
143
if (\is_string ($ types )) {
151
- return $ session ->getFlashBag ()->get ($ types );
152
- }
144
+ if (isset ($ this ->flashBagCache [$ types ])) {
145
+ $ result = [...$ result , ...$ this ->flashes [$ types ]];
146
+ }
153
147
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
+ }
157
159
}
158
160
159
161
return $ result ;
@@ -179,4 +181,35 @@ public function getCurrent_route_parameters(): array
179
181
180
182
return $ this ->getRequest ()?->attributes->get ('_route_params ' ) ?? [];
181
183
}
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
+ }
182
215
}
0 commit comments