@@ -28,12 +28,6 @@ class SessionSection implements \IteratorAggregate, \ArrayAccess
2828 /** @var string */
2929 private $ name ;
3030
31- /** @var array|null session data storage */
32- private $ data ;
33-
34- /** @var array|bool session metadata storage */
35- private $ meta = false ;
36-
3731
3832 /**
3933 * Do not call directly. Use Session::getSection().
@@ -45,23 +39,13 @@ public function __construct(Session $session, string $name)
4539 }
4640
4741
48- private function start (): void
49- {
50- if ($ this ->meta === false ) {
51- $ this ->session ->start ();
52- $ this ->data = &$ _SESSION ['__NF ' ]['DATA ' ][$ this ->name ];
53- $ this ->meta = &$ _SESSION ['__NF ' ]['META ' ][$ this ->name ];
54- }
55- }
56-
57-
5842 /**
5943 * Returns an iterator over all section variables.
6044 */
6145 public function getIterator (): \Iterator
6246 {
63- $ this ->start ( );
64- return new \ArrayIterator ($ this -> data ?? []);
47+ $ data = $ this ->getData ( false );
48+ return new \ArrayIterator ($ data ?? []);
6549 }
6650
6751
@@ -70,8 +54,7 @@ public function getIterator(): \Iterator
7054 */
7155 public function __set (string $ name , $ value ): void
7256 {
73- $ this ->start ();
74- $ this ->data [$ name ] = $ value ;
57+ $ this ->getData (true )[$ name ] = $ value ;
7558 }
7659
7760
@@ -81,12 +64,12 @@ public function __set(string $name, $value): void
8164 */
8265 public function &__get (string $ name )
8366 {
84- $ this ->start ( );
85- if ($ this ->warnOnUndefined && !array_key_exists ($ name , $ this -> data )) {
67+ $ data = $ this ->getData ( false );
68+ if ($ this ->warnOnUndefined && !array_key_exists ($ name , $ data ?? [] )) {
8669 trigger_error ("The variable ' $ name' does not exist in session section " );
8770 }
8871
89- return $ this -> data [$ name ];
72+ return $ data [$ name ];
9073 }
9174
9275
@@ -95,10 +78,11 @@ public function &__get(string $name)
9578 */
9679 public function __isset (string $ name ): bool
9780 {
98- if ($ this ->session ->exists ()) {
99- $ this -> start () ;
81+ if (! $ this ->session ->exists ()) {
82+ return false ;
10083 }
101- return isset ($ this ->data [$ name ]);
84+ $ data = $ this ->getData (false );
85+ return isset ($ data [$ name ]);
10286 }
10387
10488
@@ -107,8 +91,9 @@ public function __isset(string $name): bool
10791 */
10892 public function __unset (string $ name ): void
10993 {
110- $ this ->start ();
111- unset($ this ->data [$ name ], $ this ->meta [$ name ]);
94+ $ data = &$ this ->getData (true );
95+ $ meta = &$ this ->getMeta ();
96+ unset($ data [$ name ], $ meta [$ name ]);
11297 }
11398
11499
@@ -157,7 +142,7 @@ public function offsetUnset($name): void
157142 */
158143 public function setExpiration ($ time , $ variables = null )
159144 {
160- $ this ->start ();
145+ $ meta = & $ this ->getMeta ();
161146 if ($ time ) {
162147 $ time = Nette \Utils \DateTime::from ($ time )->format ('U ' );
163148 $ max = (int ) ini_get ('session.gc_maxlifetime ' );
@@ -170,7 +155,7 @@ public function setExpiration($time, $variables = null)
170155 }
171156
172157 foreach (is_array ($ variables ) ? $ variables : [$ variables ] as $ variable ) {
173- $ this -> meta [$ variable ]['T ' ] = $ time ?: null ;
158+ $ meta [$ variable ]['T ' ] = $ time ?: null ;
174159 }
175160 return $ this ;
176161 }
@@ -182,9 +167,9 @@ public function setExpiration($time, $variables = null)
182167 */
183168 public function removeExpiration ($ variables = null ): void
184169 {
185- $ this ->start ();
170+ $ meta = & $ this ->getMeta ();
186171 foreach (is_array ($ variables ) ? $ variables : [$ variables ] as $ variable ) {
187- unset($ this -> meta [$ variable ]['T ' ]);
172+ unset($ meta [$ variable ]['T ' ]);
188173 }
189174 }
190175
@@ -194,8 +179,23 @@ public function removeExpiration($variables = null): void
194179 */
195180 public function remove (): void
196181 {
197- $ this ->start ();
198- $ this ->data = null ;
199- $ this ->meta = null ;
182+ $ this ->session ->start ();
183+ unset($ _SESSION ['__NF ' ]['DATA ' ][$ this ->name ], $ _SESSION ['__NF ' ]['META ' ][$ this ->name ]);
184+ }
185+
186+
187+ private function &getData (bool $ write )
188+ {
189+ if ($ write || !session_id ()) {
190+ $ this ->session ->start ();
191+ }
192+ return $ _SESSION ['__NF ' ]['DATA ' ][$ this ->name ];
193+ }
194+
195+
196+ private function &getMeta ()
197+ {
198+ $ this ->session ->start ();
199+ return $ _SESSION ['__NF ' ]['META ' ][$ this ->name ];
200200 }
201201}
0 commit comments