22
33namespace Leaf ;
44
5+ use Leaf \Http \Session ;
6+
57/**
68 * Leaf Flash
79 * -----
810 * Simple flash messages for your leaf apps
9- *
11+ *
1012 * @author Michael Darko <[email protected] > 1113 * @since 2.5.0
1214 */
1315class Flash
1416{
1517 private static $ config = [
16- " key " => " leaf.flash " ,
17- " default " => " message " ,
18- " saved " => " leaf.flash.saved " ,
18+ ' key ' => ' leaf.flash ' ,
19+ ' default ' => ' message ' ,
20+ ' saved ' => ' leaf.flashSaved ' ,
1921 ];
2022
2123 /**
2224 * Configure leaf flash
23- *
25+ *
2426 * @param array $config Configuration for leaf flash
2527 */
2628 public static function config (array $ config )
@@ -30,61 +32,59 @@ public static function config(array $config)
3032
3133 /**
3234 * Set a new flash message
33- *
34- * @param string $message The flash message to set
35+ *
36+ * @param mixed $message The flash message to set
3537 * @param string $key The key to save message
3638 */
37- public static function set (string $ message , string $ key = " default " )
39+ public static function set ($ message , string $ key = ' default ' )
3840 {
39- static ::session ();
40-
41- if ($ key === "default " ) {
42- $ key = static ::$ config ["default " ];
41+ if ($ key === 'default ' ) {
42+ $ key = static ::$ config ['default ' ];
4343 }
4444
45- $ _SESSION [ static ::$ config [" key " ]] [$ key] = $ message ;
45+ Session:: set ( static ::$ config [' key ' ], [$ key => $ message]) ;
4646 }
4747
4848 /**
4949 * Remove a flash message
50- *
50+ *
5151 * @param string|null $key The key of message to remove
5252 */
5353 public static function unset (string $ key = null )
5454 {
55- static :: session ( );
55+ $ data = Session:: get ( static :: $ config [ ' key ' ], null , false );
5656
57- if (!$ key ) {
58- Http \Session::unset (static ::$ config ["key " ]);
59- } else {
60- if ($ key === "default " ) {
61- $ key = static ::$ config ["default " ];
62- }
57+ if ($ key === 'default ' ) {
58+ $ key = static ::$ config ['default ' ];
59+ }
6360
64- $ _SESSION [static ::$ config ["key " ]][$ key ] = null ;
61+ if ($ key ) {
62+ unset($ data [$ key ]);
63+ } else {
64+ $ data = [];
6565 }
66+
67+ Session::set (static ::$ config ['key ' ], $ data );
6668 }
6769
6870 /**
6971 * Get the flash array
70- *
72+ *
7173 * @param string|null $key The key of message to get
7274 * @return string|array
7375 */
74- private static function get (string $ key = null )
76+ protected static function get (string $ key = null )
7577 {
76- static ::session ();
77-
7878 if (!$ key ) {
79- return Http \ Session::get (static ::$ config [" key " ]);
79+ return Session::get (static ::$ config [' key ' ]);
8080 }
8181
82- if ($ key === " default " ) {
83- $ key = static ::$ config [" default " ];
82+ if ($ key === ' default ' ) {
83+ $ key = static ::$ config [' default ' ];
8484 }
8585
8686 $ item = null ;
87- $ items = Http \ Session::get (static ::$ config [" key " ], false );
87+ $ items = Session::get (static ::$ config [' key ' ], false );
8888
8989 if (isset ($ items [$ key ])) {
9090 $ item = $ items [$ key ];
@@ -99,54 +99,39 @@ private static function get(string $key = null)
9999
100100 /**
101101 * Display a flash message
102- *
102+ *
103103 * @param string $key The key of message to display
104104 * @return string
105105 */
106- public static function display (string $ key = " default " )
106+ public static function display (string $ key = ' default ' )
107107 {
108- static ::session ();
109-
110108 return static ::get ($ key );
111109 }
112110
113111 /**
114112 * Save a flash message (won't delete after view).
115113 * You can save only one message at a time.
116- *
114+ *
117115 * @param string $message The flash message to save
118116 */
119117 public static function save (string $ message )
120118 {
121- static ::session ();
122-
123- Http \Session::set (static ::$ config ["saved " ], $ message );
119+ Session::set (static ::$ config ['saved ' ], $ message );
124120 }
125121
126122 /**
127123 * Clear the saved flash message
128124 */
129125 public static function clearSaved ()
130126 {
131- static ::session ();
132-
133- Http \Session::set (static ::$ config ["saved " ], null );
127+ Session::set (static ::$ config ['saved ' ], null );
134128 }
135129
136130 /**
137131 * Display the saved flash message
138132 */
139133 public static function displaySaved ()
140134 {
141- static ::session ();
142-
143- return Http \Session::get (static ::$ config ["saved " ]);
144- }
145-
146- private static function session ()
147- {
148- if (!session_id ()) {
149- session_start ();
150- }
135+ return Session::get (static ::$ config ['saved ' ]);
151136 }
152137}
0 commit comments