FlashBag.php 3.1 KB

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