1111
1212final class Session
1313{
14- public const string VALIDATION_ERRORS = 'validation_errors ' ;
14+ public const string VALIDATION_ERRORS = '# validation_errors ' ;
1515
16- public const string ORIGINAL_VALUES = 'original_values ' ;
16+ public const string ORIGINAL_VALUES = '# original_values ' ;
1717
18- public const string PREVIOUS_URL = '_previous_url ' ;
18+ public const string PREVIOUS_URL = '#previous_url ' ;
1919
20- public const string CSRF_TOKEN_KEY = '_csrf_token ' ;
20+ public const string CSRF_TOKEN_KEY = '#csrf_token ' ;
2121
2222 private array $ expiredKeys = [];
2323
24+ private SessionManager $ manager {
25+ get => get (SessionManager::class);
26+ }
27+
2428 /**
2529 * Session token used for cross-site request forgery protection.
2630 */
@@ -43,17 +47,23 @@ public function __construct(
4347
4448 public function set (string $ key , mixed $ value ): void
4549 {
46- $ this ->getSessionManager () ->set ($ this ->id , $ key , $ value );
50+ $ this ->manager ->set ($ this ->id , $ key , $ value );
4751 }
4852
53+ /**
54+ * Stores a value in the session that will be available for the next request only.
55+ */
4956 public function flash (string $ key , mixed $ value ): void
5057 {
51- $ this ->getSessionManager () ->set ($ this ->id , $ key , new FlashValue ($ value ));
58+ $ this ->manager ->set ($ this ->id , $ key , new FlashValue ($ value ));
5259 }
5360
61+ /**
62+ * Reflashes all flash values in the session, making them available for the next request.
63+ */
5464 public function reflash (): void
5565 {
56- foreach ($ this ->getSessionManager () ->all ($ this ->id ) as $ key => $ value ) {
66+ foreach ($ this ->manager ->all ($ this ->id ) as $ key => $ value ) {
5767 if (! ($ value instanceof FlashValue))
5868 continue ;
5969
@@ -63,7 +73,7 @@ public function reflash(): void
6373
6474 public function get (string $ key , mixed $ default = null ): mixed
6575 {
66- $ value = $ this ->getSessionManager () ->get ($ this ->id , $ key , $ default );
76+ $ value = $ this ->manager ->get ($ this ->id , $ key , $ default );
6777
6878 if ($ value instanceof FlashValue) {
6979 $ this ->expiredKeys [$ key ] = $ key ;
@@ -75,14 +85,17 @@ public function get(string $key, mixed $default = null): mixed
7585
7686 public function getPreviousUrl (): string
7787 {
78- return $ this ->get (self ::PREVIOUS_URL , '' );
88+ return $ this ->get (self ::PREVIOUS_URL , default: '' );
7989 }
8090
8191 public function setPreviousUrl (string $ url ): void
8292 {
8393 $ this ->set (self ::PREVIOUS_URL , $ url );
8494 }
8595
96+ /**
97+ * Retrieves the value for the given key and removes it from the session.
98+ */
8699 public function consume (string $ key , mixed $ default = null ): mixed
87100 {
88101 $ value = $ this ->get ($ key , $ default );
@@ -94,33 +107,28 @@ public function consume(string $key, mixed $default = null): mixed
94107
95108 public function all (): array
96109 {
97- return $ this ->getSessionManager () ->all ($ this ->id );
110+ return $ this ->manager ->all ($ this ->id );
98111 }
99112
100113 public function remove (string $ key ): void
101114 {
102- $ this ->getSessionManager () ->remove ($ this ->id , $ key );
115+ $ this ->manager ->remove ($ this ->id , $ key );
103116 }
104117
105118 public function destroy (): void
106119 {
107- $ this ->getSessionManager () ->destroy ($ this ->id );
120+ $ this ->manager ->destroy ($ this ->id );
108121 }
109122
110123 public function isValid (): bool
111124 {
112- return $ this ->getSessionManager () ->isValid ($ this ->id );
125+ return $ this ->manager ->isValid ($ this ->id );
113126 }
114127
115128 public function cleanup (): void
116129 {
117130 foreach ($ this ->expiredKeys as $ key ) {
118- $ this ->getSessionManager () ->remove ($ this ->id , $ key );
131+ $ this ->manager ->remove ($ this ->id , $ key );
119132 }
120133 }
121-
122- private function getSessionManager (): SessionManager
123- {
124- return get (SessionManager::class);
125- }
126134}
0 commit comments