4
4
5
5
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
6
6
use Symfony \Component \HttpFoundation \RequestStack ;
7
+ use Symfony \Component \HttpFoundation \Session \FlashBagAwareSessionInterface ;
7
8
use Symfony \Component \HttpKernel \Event \ResponseEvent ;
8
9
use Symfony \Component \HttpKernel \HttpKernelInterface ;
9
10
use Symfony \Component \HttpKernel \KernelEvents ;
@@ -17,20 +18,20 @@ class FlashMessageSubscriber implements EventSubscriberInterface
17
18
public function __construct (RequestStack $ requestStack , TranslatorInterface $ translator )
18
19
{
19
20
$ this ->requestStack = $ requestStack ;
20
- $ this ->translator = $ translator ;
21
+ $ this ->translator = $ translator ;
21
22
}
22
23
23
- public static function getSubscribedEvents ()
24
+ public static function getSubscribedEvents (): array
24
25
{
25
26
return [
26
27
// we want that to run early (before the Session->Cookie transformation)
27
- KernelEvents::RESPONSE => ['onKernelResponse ' , 10 ]
28
+ KernelEvents::RESPONSE => ['onKernelResponse ' , 10 ],
28
29
];
29
30
}
30
31
31
32
public function onKernelResponse (ResponseEvent $ event )
32
33
{
33
- // @todo remove this when we drop support for SF4, see https://github.com/symfony/http-kernel/blob/7.1/CHANGELOG.md#53
34
+ // @todo remove this when we drop support for SF4, see https://github.com/symfony/http-kernel/blob/7.1/CHANGELOG.md#53
34
35
if (defined ('HttpKernelInterface::MASTER_REQUEST ' ) && HttpKernelInterface::MASTER_REQUEST !== $ event ->getRequestType ()) {
35
36
return ;
36
37
}
@@ -57,9 +58,11 @@ public function onKernelResponse(ResponseEvent $event)
57
58
return ;
58
59
}
59
60
61
+ assert ($ session instanceof FlashBagAwareSessionInterface);
62
+
60
63
$ flashBag = $ session ->getFlashBag ();
61
64
// Use peekAll because we want to keep existing messages in the bag
62
- $ flashes = $ flashBag ->peekAll ();
65
+ $ flashes = $ flashBag ->peekAll ();
63
66
64
67
if (empty ($ flashes )) {
65
68
return ;
@@ -68,30 +71,30 @@ public function onKernelResponse(ResponseEvent $event)
68
71
$ titleKeys = [
69
72
'success ' => 'successes ' ,
70
73
'warning ' => 'warnings ' ,
71
- 'error ' => 'errors ' ,
72
- 'info ' => 'info '
74
+ 'error ' => 'errors ' ,
75
+ 'info ' => 'info ' ,
73
76
];
74
77
75
78
$ existingTitles = array_keys (
76
79
array_filter (
77
80
$ flashes ,
78
- fn (string $ key ) => strpos ($ key , 'title- ' ) === 0 ,
81
+ fn (string $ key ) => 0 === strpos ($ key , 'title- ' ),
79
82
ARRAY_FILTER_USE_KEY
80
83
)
81
84
);
82
85
83
- foreach (array_keys ($ flashes ) as $ type ) {
86
+ foreach (array_keys ($ flashes ) as $ type ) {
84
87
// $type is not a title and its corresponding title doesn't already exist
85
88
if (
86
- strpos ($ type , 'title- ' ) === false &&
87
- !in_array ('title- ' . $ type , $ existingTitles ) &&
88
- array_key_exists ($ type , $ titleKeys )
89
+ false === strpos ($ type , 'title- ' )
90
+ && !in_array ('title- ' . $ type , $ existingTitles )
91
+ && array_key_exists ($ type , $ titleKeys )
89
92
) {
90
93
$ flashBag ->add (
91
- 'title- ' . $ type ,
92
- $ this ->translator ->trans ('notes. ' . $ titleKeys [$ type ] . '.default_title ' , [], 'w3c_website_templates_bundle ' )
94
+ 'title- ' . $ type ,
95
+ $ this ->translator ->trans ('notes. ' . $ titleKeys [$ type ]. '.default_title ' , [], 'w3c_website_templates_bundle ' )
93
96
);
94
- $ existingTitles [] = 'title- ' . $ type ;
97
+ $ existingTitles [] = 'title- ' . $ type ;
95
98
}
96
99
}
97
100
}
0 commit comments