FlashBag.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Flash;
  11. /**
  12. * FlashBag flash message container.
  13. *
  14. * @author Drak <drak@zikula.org>
  15. */
  16. class FlashBag implements FlashBagInterface, \IteratorAggregate
  17. {
  18. private $name = 'flashes';
  19. /**
  20. * Flash messages.
  21. *
  22. * @var array
  23. */
  24. private $flashes = array();
  25. /**
  26. * The storage key for flashes in the session
  27. *
  28. * @var string
  29. */
  30. private $storageKey;
  31. /**
  32. * Constructor.
  33. *
  34. * @param string $storageKey The key used to store flashes in the session.
  35. */
  36. public function __construct($storageKey = '_sf2_flashes')
  37. {
  38. $this->storageKey = $storageKey;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function getName()
  44. {
  45. return $this->name;
  46. }
  47. public function setName($name)
  48. {
  49. $this->name = $name;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function initialize(array &$flashes)
  55. {
  56. $this->flashes = &$flashes;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function add($type, $message)
  62. {
  63. $this->flashes[$type][] = $message;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function peek($type, array $default =array())
  69. {
  70. return $this->has($type) ? $this->flashes[$type] : $default;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function peekAll()
  76. {
  77. return $this->flashes;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function get($type, array $default = array())
  83. {
  84. if (!$this->has($type)) {
  85. return $default;
  86. }
  87. $return = $this->flashes[$type];
  88. unset($this->flashes[$type]);
  89. return $return;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function all()
  95. {
  96. $return = $this->peekAll();
  97. $this->flashes = array();
  98. return $return;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function set($type, $messages)
  104. {
  105. $this->flashes[$type] = (array) $messages;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function setAll(array $messages)
  111. {
  112. $this->flashes = $messages;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function has($type)
  118. {
  119. return array_key_exists($type, $this->flashes) && $this->flashes[$type];
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function keys()
  125. {
  126. return array_keys($this->flashes);
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getStorageKey()
  132. {
  133. return $this->storageKey;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function clear()
  139. {
  140. return $this->all();
  141. }
  142. /**
  143. * Returns an iterator for flashes.
  144. *
  145. * @return \ArrayIterator An \ArrayIterator instance
  146. */
  147. public function getIterator()
  148. {
  149. return new \ArrayIterator($this->all());
  150. }
  151. }