Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

86 lines
1.9KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. /**
  9. * SessionIterator implements an [[\Iterator|iterator]] for traversing session variables managed by [[Session]].
  10. *
  11. * @author Qiang Xue <qiang.xue@gmail.com>
  12. * @since 2.0
  13. */
  14. class SessionIterator implements \Iterator
  15. {
  16. /**
  17. * @var array list of keys in the map
  18. */
  19. private $_keys;
  20. /**
  21. * @var mixed current key
  22. */
  23. private $_key;
  24. /**
  25. * Constructor.
  26. */
  27. public function __construct()
  28. {
  29. $this->_keys = array_keys($_SESSION);
  30. }
  31. /**
  32. * Rewinds internal array pointer.
  33. * This method is required by the interface [[\Iterator]].
  34. */
  35. public function rewind()
  36. {
  37. $this->_key = reset($this->_keys);
  38. }
  39. /**
  40. * Returns the key of the current array element.
  41. * This method is required by the interface [[\Iterator]].
  42. * @return mixed the key of the current array element
  43. */
  44. public function key()
  45. {
  46. return $this->_key;
  47. }
  48. /**
  49. * Returns the current array element.
  50. * This method is required by the interface [[\Iterator]].
  51. * @return mixed the current array element
  52. */
  53. public function current()
  54. {
  55. return isset($_SESSION[$this->_key]) ? $_SESSION[$this->_key] : null;
  56. }
  57. /**
  58. * Moves the internal pointer to the next array element.
  59. * This method is required by the interface [[\Iterator]].
  60. */
  61. public function next()
  62. {
  63. do {
  64. $this->_key = next($this->_keys);
  65. } while (!isset($_SESSION[$this->_key]) && $this->_key !== false);
  66. }
  67. /**
  68. * Returns whether there is an element at current position.
  69. * This method is required by the interface [[\Iterator]].
  70. * @return boolean
  71. */
  72. public function valid()
  73. {
  74. return $this->_key !== false;
  75. }
  76. }