Complex classes like Session often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
75 | class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable |
||
76 | { |
||
77 | /** |
||
78 | * @var string the name of the session variable that stores the flash message data. |
||
79 | */ |
||
80 | public $flashParam = '__flash'; |
||
81 | /** |
||
82 | * @var \SessionHandlerInterface|array an object implementing the SessionHandlerInterface or a configuration array. If set, will be used to provide persistency instead of build-in methods. |
||
83 | */ |
||
84 | public $handler; |
||
85 | |||
86 | /** |
||
87 | * @var array parameter-value pairs to override default session cookie parameters that are used for session_set_cookie_params() function |
||
88 | * Array may have the following possible keys: 'lifetime', 'path', 'domain', 'secure', 'httponly' |
||
89 | * @see http://www.php.net/manual/en/function.session-set-cookie-params.php |
||
90 | */ |
||
91 | private $_cookieParams = ['httponly' => true]; |
||
92 | /** |
||
93 | * @var $frozenSessionData array|null is used for saving session between recreations due to session parameters update. |
||
94 | */ |
||
95 | private $frozenSessionData; |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Initializes the application component. |
||
100 | * This method is required by IApplicationComponent and is invoked by application. |
||
101 | */ |
||
102 | 69 | public function init() |
|
103 | { |
||
104 | 69 | parent::init(); |
|
105 | 69 | register_shutdown_function([$this, 'close']); |
|
106 | 69 | if ($this->getIsActive()) { |
|
107 | 3 | Yii::warning('Session is already started', __METHOD__); |
|
108 | 3 | $this->updateFlashCounters(); |
|
109 | } |
||
110 | 69 | } |
|
111 | |||
112 | /** |
||
113 | * Returns a value indicating whether to use custom session storage. |
||
114 | * This method should be overridden to return true by child classes that implement custom session storage. |
||
115 | * To implement custom session storage, override these methods: [[openSession()]], [[closeSession()]], |
||
116 | * [[readSession()]], [[writeSession()]], [[destroySession()]] and [[gcSession()]]. |
||
117 | * @return bool whether to use custom storage. |
||
118 | */ |
||
119 | 37 | public function getUseCustomStorage() |
|
120 | { |
||
121 | 37 | return false; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Starts the session. |
||
126 | */ |
||
127 | 38 | public function open() |
|
128 | { |
||
129 | 38 | if ($this->getIsActive()) { |
|
130 | 37 | return; |
|
131 | } |
||
132 | |||
133 | 38 | $this->registerSessionHandler(); |
|
134 | |||
135 | 38 | $this->setCookieParamsInternal(); |
|
136 | |||
137 | 38 | YII_DEBUG ? session_start() : @session_start(); |
|
138 | |||
139 | 38 | if ($this->getIsActive()) { |
|
140 | 38 | Yii::info('Session started', __METHOD__); |
|
141 | 38 | $this->updateFlashCounters(); |
|
142 | } else { |
||
143 | $error = error_get_last(); |
||
144 | $message = $error['message'] ?? 'Failed to start session.'; |
||
145 | Yii::error($message, __METHOD__); |
||
146 | } |
||
147 | 38 | } |
|
148 | |||
149 | /** |
||
150 | * Registers session handler. |
||
151 | * @throws \yii\base\InvalidConfigException |
||
152 | */ |
||
153 | 38 | protected function registerSessionHandler() |
|
154 | { |
||
155 | 38 | if ($this->handler !== null) { |
|
156 | if (!is_object($this->handler)) { |
||
157 | $this->handler = Yii::createObject($this->handler); |
||
158 | } |
||
159 | if (!$this->handler instanceof \SessionHandlerInterface) { |
||
160 | throw new InvalidConfigException('"' . get_class($this) . '::handler" must implement the SessionHandlerInterface.'); |
||
161 | } |
||
162 | YII_DEBUG ? session_set_save_handler($this->handler, false) : @session_set_save_handler($this->handler, false); |
||
163 | 38 | } elseif ($this->getUseCustomStorage()) { |
|
164 | 1 | if (YII_DEBUG) { |
|
165 | 1 | session_set_save_handler( |
|
166 | 1 | [$this, 'openSession'], |
|
167 | 1 | [$this, 'closeSession'], |
|
168 | 1 | [$this, 'readSession'], |
|
169 | 1 | [$this, 'writeSession'], |
|
170 | 1 | [$this, 'destroySession'], |
|
171 | 1 | [$this, 'gcSession'] |
|
172 | ); |
||
173 | } else { |
||
174 | @session_set_save_handler( |
||
|
|||
175 | [$this, 'openSession'], |
||
176 | [$this, 'closeSession'], |
||
177 | [$this, 'readSession'], |
||
178 | [$this, 'writeSession'], |
||
179 | [$this, 'destroySession'], |
||
180 | [$this, 'gcSession'] |
||
181 | ); |
||
182 | } |
||
183 | } |
||
184 | 38 | } |
|
185 | |||
186 | /** |
||
187 | * Ends the current session and store session data. |
||
188 | */ |
||
189 | 51 | public function close() |
|
190 | { |
||
191 | 51 | if ($this->getIsActive()) { |
|
192 | 37 | YII_DEBUG ? session_write_close() : @session_write_close(); |
|
193 | } |
||
194 | 51 | } |
|
195 | |||
196 | /** |
||
197 | * Frees all session variables and destroys all data registered to a session. |
||
198 | * |
||
199 | * This method has no effect when session is not [[getIsActive()|active]]. |
||
200 | * Make sure to call [[open()]] before calling it. |
||
201 | * @see open() |
||
202 | * @see isActive |
||
203 | */ |
||
204 | 2 | public function destroy() |
|
205 | { |
||
206 | 2 | if ($this->getIsActive()) { |
|
207 | 2 | $sessionId = session_id(); |
|
208 | 2 | $this->close(); |
|
209 | 2 | $this->setId($sessionId); |
|
210 | 2 | $this->open(); |
|
211 | 2 | session_unset(); |
|
212 | 2 | session_destroy(); |
|
213 | 2 | $this->setId($sessionId); |
|
214 | } |
||
215 | 2 | } |
|
216 | |||
217 | /** |
||
218 | * @return bool whether the session has started |
||
219 | */ |
||
220 | 69 | public function getIsActive() |
|
221 | { |
||
222 | 69 | return session_status() === PHP_SESSION_ACTIVE; |
|
223 | } |
||
224 | |||
225 | private $_hasSessionId; |
||
226 | |||
227 | /** |
||
228 | * Returns a value indicating whether the current request has sent the session ID. |
||
229 | * The default implementation will check cookie and $_GET using the session name. |
||
230 | * If you send session ID via other ways, you may need to override this method |
||
231 | * or call [[setHasSessionId()]] to explicitly set whether the session ID is sent. |
||
232 | * @return bool whether the current request has sent the session ID. |
||
233 | */ |
||
234 | 15 | public function getHasSessionId() |
|
235 | { |
||
236 | 15 | if ($this->_hasSessionId === null) { |
|
237 | 15 | $name = $this->getName(); |
|
238 | 15 | $request = Yii::$app->getRequest(); |
|
239 | // unable to use `Request::$cookies` since CSRF protection feature exclude the session one from them |
||
240 | 15 | if (!empty($_COOKIE[$name]) && ini_get('session.use_cookies')) { |
|
241 | $this->_hasSessionId = true; |
||
242 | 15 | } elseif (!ini_get('session.use_only_cookies') && ini_get('session.use_trans_sid')) { |
|
243 | $this->_hasSessionId = $request->get($name) != ''; |
||
244 | } else { |
||
245 | 15 | $this->_hasSessionId = false; |
|
246 | } |
||
247 | } |
||
248 | |||
249 | 15 | return $this->_hasSessionId; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * Sets the value indicating whether the current request has sent the session ID. |
||
254 | * This method is provided so that you can override the default way of determining |
||
255 | * whether the session ID is sent. |
||
256 | * @param bool $value whether the current request has sent the session ID. |
||
257 | */ |
||
258 | public function setHasSessionId($value) |
||
259 | { |
||
260 | $this->_hasSessionId = $value; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Gets the session ID. |
||
265 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
266 | * @return string the current session ID |
||
267 | */ |
||
268 | 1 | public function getId() |
|
269 | { |
||
270 | 1 | return session_id(); |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Sets the session ID. |
||
275 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
276 | * @param string $value the session ID for the current session |
||
277 | */ |
||
278 | 2 | public function setId($value) |
|
279 | { |
||
280 | 2 | session_id($value); |
|
281 | 2 | } |
|
282 | |||
283 | /** |
||
284 | * Updates the current session ID with a newly generated one. |
||
285 | * |
||
286 | * Please refer to <http://php.net/session_regenerate_id> for more details. |
||
287 | * |
||
288 | * This method has no effect when session is not [[getIsActive()|active]]. |
||
289 | * Make sure to call [[open()]] before calling it. |
||
290 | * |
||
291 | * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
||
292 | * @see open() |
||
293 | * @see isActive |
||
294 | */ |
||
295 | public function regenerateID($deleteOldSession = false) |
||
296 | { |
||
297 | if ($this->getIsActive()) { |
||
298 | // add @ to inhibit possible warning due to race condition |
||
299 | // https://github.com/yiisoft/yii2/pull/1812 |
||
300 | if (YII_DEBUG && !headers_sent()) { |
||
301 | session_regenerate_id($deleteOldSession); |
||
302 | } else { |
||
303 | @session_regenerate_id($deleteOldSession); |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Gets the name of the current session. |
||
310 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
311 | * @return string the current session name |
||
312 | */ |
||
313 | 16 | public function getName() |
|
314 | { |
||
315 | 16 | return session_name(); |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Sets the name for the current session. |
||
320 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
321 | * @param string $value the session name for the current session, must be an alphanumeric string. |
||
322 | * It defaults to "PHPSESSID". |
||
323 | */ |
||
324 | 1 | public function setName($value) |
|
325 | { |
||
326 | 1 | $this->freeze(); |
|
327 | 1 | session_name($value); |
|
328 | 1 | $this->unfreeze(); |
|
329 | 1 | } |
|
330 | |||
331 | /** |
||
332 | * Gets the current session save path. |
||
333 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
334 | * @return string the current session save path, defaults to '/tmp'. |
||
335 | */ |
||
336 | public function getSavePath() |
||
340 | |||
341 | /** |
||
342 | * Sets the current session save path. |
||
343 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
344 | * @param string $value the current session save path. This can be either a directory name or a [path alias](guide:concept-aliases). |
||
345 | * @throws InvalidArgumentException if the path is not a valid directory |
||
346 | */ |
||
347 | public function setSavePath($value) |
||
356 | |||
357 | /** |
||
358 | * @return array the session cookie parameters. |
||
359 | * @see http://php.net/manual/en/function.session-get-cookie-params.php |
||
360 | */ |
||
361 | 38 | public function getCookieParams() |
|
365 | |||
366 | /** |
||
367 | * Sets the session cookie parameters. |
||
368 | * The cookie parameters passed to this method will be merged with the result |
||
369 | * of `session_get_cookie_params()`. |
||
370 | * @param array $value cookie parameters, valid keys include: `lifetime`, `path`, `domain`, `secure` and `httponly`. |
||
371 | * @throws InvalidArgumentException if the parameters are incomplete. |
||
372 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
373 | */ |
||
374 | public function setCookieParams(array $value) |
||
378 | |||
379 | /** |
||
380 | * Sets the session cookie parameters. |
||
381 | * This method is called by [[open()]] when it is about to open the session. |
||
382 | * @throws InvalidArgumentException if the parameters are incomplete. |
||
383 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
384 | */ |
||
385 | 38 | private function setCookieParamsInternal() |
|
394 | |||
395 | /** |
||
396 | * Returns the value indicating whether cookies should be used to store session IDs. |
||
397 | * @return bool|null the value indicating whether cookies should be used to store session IDs. |
||
398 | * @see setUseCookies() |
||
399 | */ |
||
400 | 1 | public function getUseCookies() |
|
401 | { |
||
402 | 1 | if (ini_get('session.use_cookies') === '0') { |
|
403 | 1 | return false; |
|
404 | 1 | } elseif (ini_get('session.use_only_cookies') === '1') { |
|
405 | 1 | return true; |
|
406 | } |
||
407 | |||
410 | |||
411 | /** |
||
412 | * Sets the value indicating whether cookies should be used to store session IDs. |
||
413 | * |
||
414 | * Three states are possible: |
||
415 | * |
||
416 | * - true: cookies and only cookies will be used to store session IDs. |
||
417 | * - false: cookies will not be used to store session IDs. |
||
418 | * - null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter) |
||
419 | * |
||
420 | * @param bool|null $value the value indicating whether cookies should be used to store session IDs. |
||
421 | */ |
||
422 | 4 | public function setUseCookies($value) |
|
437 | |||
438 | /** |
||
439 | * @return float the probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. |
||
440 | */ |
||
441 | 1 | public function getGCProbability() |
|
445 | |||
446 | /** |
||
447 | * @param float $value the probability (percentage) that the GC (garbage collection) process is started on every session initialization. |
||
448 | * @throws InvalidArgumentException if the value is not between 0 and 100. |
||
449 | */ |
||
450 | 1 | public function setGCProbability($value) |
|
462 | |||
463 | /** |
||
464 | * @return bool whether transparent sid support is enabled or not, defaults to false. |
||
465 | */ |
||
466 | 1 | public function getUseTransparentSessionID() |
|
470 | |||
471 | /** |
||
472 | * @param bool $value whether transparent sid support is enabled or not. |
||
473 | */ |
||
474 | 1 | public function setUseTransparentSessionID($value) |
|
480 | |||
481 | /** |
||
482 | * @return int the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
483 | * The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini). |
||
484 | */ |
||
485 | 22 | public function getTimeout() |
|
489 | |||
490 | /** |
||
491 | * @param int $value the number of seconds after which data will be seen as 'garbage' and cleaned up |
||
492 | */ |
||
493 | 4 | public function setTimeout($value) |
|
499 | |||
500 | /** |
||
501 | * Session open handler. |
||
502 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
503 | * @internal Do not call this method directly. |
||
504 | * @param string $savePath session save path |
||
505 | * @param string $sessionName session name |
||
506 | * @return bool whether session is opened successfully |
||
507 | */ |
||
508 | 4 | public function openSession($savePath, $sessionName) |
|
512 | |||
513 | /** |
||
514 | * Session close handler. |
||
515 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
516 | * @internal Do not call this method directly. |
||
517 | * @return bool whether session is closed successfully |
||
518 | */ |
||
519 | 3 | public function closeSession() |
|
523 | |||
524 | /** |
||
525 | * Session read handler. |
||
526 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
527 | * @internal Do not call this method directly. |
||
528 | * @param string $id session ID |
||
529 | * @return string the session data |
||
530 | */ |
||
531 | public function readSession($id) |
||
535 | |||
536 | /** |
||
537 | * Session write handler. |
||
538 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
539 | * @internal Do not call this method directly. |
||
540 | * @param string $id session ID |
||
541 | * @param string $data session data |
||
542 | * @return bool whether session write is successful |
||
543 | */ |
||
544 | public function writeSession($id, $data) |
||
548 | |||
549 | /** |
||
550 | * Session destroy handler. |
||
551 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
552 | * @internal Do not call this method directly. |
||
553 | * @param string $id session ID |
||
554 | * @return bool whether session is destroyed successfully |
||
555 | */ |
||
556 | public function destroySession($id) |
||
560 | |||
561 | /** |
||
562 | * Session GC (garbage collection) handler. |
||
563 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
564 | * @internal Do not call this method directly. |
||
565 | * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
566 | * @return bool whether session is GCed successfully |
||
567 | */ |
||
568 | 2 | public function gcSession($maxLifetime) |
|
572 | |||
573 | /** |
||
574 | * Returns an iterator for traversing the session variables. |
||
575 | * This method is required by the interface [[\IteratorAggregate]]. |
||
576 | * @return SessionIterator an iterator for traversing the session variables. |
||
577 | */ |
||
578 | public function getIterator() |
||
583 | |||
584 | /** |
||
585 | * Returns the number of items in the session. |
||
586 | * @return int the number of session variables |
||
587 | */ |
||
588 | public function getCount() |
||
593 | |||
594 | /** |
||
595 | * Returns the number of items in the session. |
||
596 | * This method is required by [[\Countable]] interface. |
||
597 | * @return int number of items in the session. |
||
598 | */ |
||
599 | public function count() |
||
603 | |||
604 | /** |
||
605 | * Returns the session variable value with the session variable name. |
||
606 | * If the session variable does not exist, the `$defaultValue` will be returned. |
||
607 | * @param string $key the session variable name |
||
608 | * @param mixed $defaultValue the default value to be returned when the session variable does not exist. |
||
609 | * @return mixed the session variable value, or $defaultValue if the session variable does not exist. |
||
610 | */ |
||
611 | 37 | public function get($key, $defaultValue = null) |
|
612 | { |
||
613 | 37 | $this->open(); |
|
614 | 37 | return $_SESSION[$key] ?? $defaultValue; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * Adds a session variable. |
||
619 | * If the specified name already exists, the old value will be overwritten. |
||
620 | * @param string $key session variable name |
||
621 | * @param mixed $value session variable value |
||
622 | */ |
||
623 | 22 | public function set($key, $value) |
|
628 | |||
629 | /** |
||
630 | * Removes a session variable. |
||
631 | * @param string $key the name of the session variable to be removed |
||
632 | * @return mixed the removed value, null if no such session variable. |
||
633 | */ |
||
634 | 18 | public function remove($key) |
|
646 | |||
647 | /** |
||
648 | * Removes all session variables. |
||
649 | */ |
||
650 | 15 | public function removeAll() |
|
657 | |||
658 | /** |
||
659 | * @param mixed $key session variable name |
||
660 | * @return bool whether there is the named session variable |
||
661 | */ |
||
662 | public function has($key) |
||
667 | |||
668 | /** |
||
669 | * Updates the counters for flash messages and removes outdated flash messages. |
||
670 | * This method should only be called once in [[init()]]. |
||
671 | */ |
||
672 | 38 | protected function updateFlashCounters() |
|
689 | |||
690 | /** |
||
691 | * Returns a flash message. |
||
692 | * @param string $key the key identifying the flash message |
||
693 | * @param mixed $defaultValue value to be returned if the flash message does not exist. |
||
694 | * @param bool $delete whether to delete this flash message right after this method is called. |
||
695 | * If false, the flash message will be automatically deleted in the next request. |
||
696 | * @return mixed the flash message or an array of messages if addFlash was used |
||
697 | * @see setFlash() |
||
698 | * @see addFlash() |
||
699 | * @see hasFlash() |
||
700 | * @see getAllFlashes() |
||
701 | * @see removeFlash() |
||
702 | */ |
||
703 | public function getFlash($key, $defaultValue = null, $delete = false) |
||
721 | |||
722 | /** |
||
723 | * Returns all flash messages. |
||
724 | * |
||
725 | * You may use this method to display all the flash messages in a view file: |
||
726 | * |
||
727 | * ```php |
||
728 | * <?php |
||
729 | * foreach (Yii::$app->session->getAllFlashes() as $key => $message) { |
||
730 | * echo '<div class="alert alert-' . $key . '">' . $message . '</div>'; |
||
731 | * } ?> |
||
732 | * ``` |
||
733 | * |
||
734 | * With the above code you can use the [bootstrap alert][] classes such as `success`, `info`, `danger` |
||
735 | * as the flash message key to influence the color of the div. |
||
736 | * |
||
737 | * Note that if you use [[addFlash()]], `$message` will be an array, and you will have to adjust the above code. |
||
738 | * |
||
739 | * [bootstrap alert]: http://getbootstrap.com/components/#alerts |
||
740 | * |
||
741 | * @param bool $delete whether to delete the flash messages right after this method is called. |
||
742 | * If false, the flash messages will be automatically deleted in the next request. |
||
743 | * @return array flash messages (key => message or key => [message1, message2]). |
||
744 | * @see setFlash() |
||
745 | * @see addFlash() |
||
746 | * @see getFlash() |
||
747 | * @see hasFlash() |
||
748 | * @see removeFlash() |
||
749 | */ |
||
750 | public function getAllFlashes($delete = false) |
||
772 | |||
773 | /** |
||
774 | * Sets a flash message. |
||
775 | * A flash message will be automatically deleted after it is accessed in a request and the deletion will happen |
||
776 | * in the next request. |
||
777 | * If there is already an existing flash message with the same key, it will be overwritten by the new one. |
||
778 | * @param string $key the key identifying the flash message. Note that flash messages |
||
779 | * and normal session variables share the same name space. If you have a normal |
||
780 | * session variable using the same name, its value will be overwritten by this method. |
||
781 | * @param mixed $value flash message |
||
782 | * @param bool $removeAfterAccess whether the flash message should be automatically removed only if |
||
783 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
784 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
785 | * it is accessed. |
||
786 | * @see getFlash() |
||
787 | * @see addFlash() |
||
788 | * @see removeFlash() |
||
789 | */ |
||
790 | public function setFlash($key, $value = true, $removeAfterAccess = true) |
||
797 | |||
798 | /** |
||
799 | * Adds a flash message. |
||
800 | * If there are existing flash messages with the same key, the new one will be appended to the existing message array. |
||
801 | * @param string $key the key identifying the flash message. |
||
802 | * @param mixed $value flash message |
||
803 | * @param bool $removeAfterAccess whether the flash message should be automatically removed only if |
||
804 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
805 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
806 | * it is accessed. |
||
807 | * @see getFlash() |
||
808 | * @see setFlash() |
||
809 | * @see removeFlash() |
||
810 | */ |
||
811 | public function addFlash($key, $value = true, $removeAfterAccess = true) |
||
826 | |||
827 | /** |
||
828 | * Removes a flash message. |
||
829 | * @param string $key the key identifying the flash message. Note that flash messages |
||
830 | * and normal session variables share the same name space. If you have a normal |
||
831 | * session variable using the same name, it will be removed by this method. |
||
832 | * @return mixed the removed flash message. Null if the flash message does not exist. |
||
833 | * @see getFlash() |
||
834 | * @see setFlash() |
||
835 | * @see addFlash() |
||
836 | * @see removeAllFlashes() |
||
837 | */ |
||
838 | public function removeFlash($key) |
||
847 | |||
848 | /** |
||
849 | * Removes all flash messages. |
||
850 | * Note that flash messages and normal session variables share the same name space. |
||
851 | * If you have a normal session variable using the same name, it will be removed |
||
852 | * by this method. |
||
853 | * @see getFlash() |
||
854 | * @see setFlash() |
||
855 | * @see addFlash() |
||
856 | * @see removeFlash() |
||
857 | */ |
||
858 | public function removeAllFlashes() |
||
866 | |||
867 | /** |
||
868 | * Returns a value indicating whether there are flash messages associated with the specified key. |
||
869 | * @param string $key key identifying the flash message type |
||
870 | * @return bool whether any flash messages exist under specified key |
||
871 | */ |
||
872 | public function hasFlash($key) |
||
876 | |||
877 | /** |
||
878 | * This method is required by the interface [[\ArrayAccess]]. |
||
879 | * @param mixed $offset the offset to check on |
||
880 | * @return bool |
||
881 | */ |
||
882 | public function offsetExists($offset) |
||
888 | |||
889 | /** |
||
890 | * This method is required by the interface [[\ArrayAccess]]. |
||
891 | * @param int $offset the offset to retrieve element. |
||
892 | * @return mixed the element at the offset, null if no element is found at the offset |
||
893 | */ |
||
894 | public function offsetGet($offset) |
||
895 | { |
||
896 | $this->open(); |
||
897 | |||
898 | return $_SESSION[$offset] ?? null; |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * This method is required by the interface [[\ArrayAccess]]. |
||
903 | * @param int $offset the offset to set element |
||
904 | * @param mixed $item the element value |
||
905 | */ |
||
906 | public function offsetSet($offset, $item) |
||
911 | |||
912 | /** |
||
913 | * This method is required by the interface [[\ArrayAccess]]. |
||
914 | * @param mixed $offset the offset to unset element |
||
915 | */ |
||
916 | public function offsetUnset($offset) |
||
921 | |||
922 | /** |
||
923 | * If session is started it's not possible to edit session ini settings. In PHP7.2+ it throws exception. |
||
924 | * This function saves session data to temporary variable and stop session. |
||
925 | * @since 2.0.14 |
||
926 | */ |
||
927 | 9 | protected function freeze() |
|
937 | |||
938 | /** |
||
939 | * Starts session and restores data from temporary variable |
||
940 | * @since 2.0.14 |
||
941 | */ |
||
942 | 9 | protected function unfreeze() |
|
943 | { |
||
944 | 9 | if (null !== $this->frozenSessionData) { |
|
945 | |||
946 | 2 | YII_DEBUG ? session_start() : @session_start(); |
|
947 | |||
948 | 2 | if ($this->getIsActive()) { |
|
949 | 2 | Yii::info('Session unfrozen', __METHOD__); |
|
950 | } else { |
||
951 | $error = error_get_last(); |
||
952 | $message = $error['message'] ?? 'Failed to unfreeze session.'; |
||
953 | Yii::error($message, __METHOD__); |
||
954 | } |
||
955 | |||
956 | 2 | $_SESSION = $this->frozenSessionData; |
|
957 | 2 | $this->frozenSessionData = null; |
|
958 | } |
||
959 | 9 | } |
|
960 | |||
961 | /** |
||
962 | * Set cache limiter |
||
963 | * |
||
964 | * @param string $cacheLimiter |
||
965 | * @since 2.0.14 |
||
966 | */ |
||
967 | 1 | public function setCacheLimiter($cacheLimiter) |
|
973 | |||
974 | /** |
||
975 | * Returns current cache limiter |
||
976 | * |
||
977 | * @return string current cache limiter |
||
978 | * @since 2.0.14 |
||
979 | */ |
||
980 | public function getCacheLimiter() |
||
984 | } |
||
985 |
If you suppress an error, we recommend checking for the error condition explicitly: