No Description

FlashBag.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * \IteratorAggregate implementation is deprecated and will be removed in 3.0.
  15. *
  16. * @author Drak <drak@zikula.org>
  17. */
  18. class FlashBag implements FlashBagInterface, \IteratorAggregate
  19. {
  20. private $name = 'flashes';
  21. /**
  22. * Flash messages.
  23. *
  24. * @var array
  25. */
  26. private $flashes = array();
  27. /**
  28. * The storage key for flashes in the session.
  29. *
  30. * @var string
  31. */
  32. private $storageKey;
  33. /**
  34. * Constructor.
  35. *
  36. * @param string $storageKey The key used to store flashes in the session.
  37. */
  38. public function __construct($storageKey = '_sf2_flashes')
  39. {
  40. $this->storageKey = $storageKey;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function getName()
  46. {
  47. return $this->name;
  48. }
  49. public function setName($name)
  50. {
  51. $this->name = $name;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function initialize(array &$flashes)
  57. {
  58. $this->flashes = &$flashes;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function add($type, $message)
  64. {
  65. $this->flashes[$type][] = $message;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function peek($type, array $default = array())
  71. {
  72. return $this->has($type) ? $this->flashes[$type] : $default;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function peekAll()
  78. {
  79. return $this->flashes;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function get($type, array $default = array())
  85. {
  86. if (!$this->has($type)) {
  87. return $default;
  88. }
  89. $return = $this->flashes[$type];
  90. unset($this->flashes[$type]);
  91. return $return;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function all()
  97. {
  98. $return = $this->peekAll();
  99. $this->flashes = array();
  100. return $return;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function set($type, $messages)
  106. {
  107. $this->flashes[$type] = (array) $messages;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function setAll(array $messages)
  113. {
  114. $this->flashes = $messages;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function has($type)
  120. {
  121. return array_key_exists($type, $this->flashes) && $this->flashes[$type];
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function keys()
  127. {
  128. return array_keys($this->flashes);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getStorageKey()
  134. {
  135. return $this->storageKey;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function clear()
  141. {
  142. return $this->all();
  143. }
  144. /**
  145. * Returns an iterator for flashes.
  146. *
  147. * @deprecated Will be removed in 3.0.
  148. *
  149. * @return \ArrayIterator An \ArrayIterator instance
  150. */
  151. public function getIterator()
  152. {
  153. return new \ArrayIterator($this->all());
  154. }
  155. }