FlashBag.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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
  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 peek($type, $default = null)
  62. {
  63. return $this->has($type) ? $this->flashes[$type] : $default;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function peekAll()
  69. {
  70. return $this->flashes;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function get($type, $default = null)
  76. {
  77. if (!$this->has($type)) {
  78. return $default;
  79. }
  80. $return = $this->flashes[$type];
  81. unset($this->flashes[$type]);
  82. return $return;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function all()
  88. {
  89. $return = $this->peekAll();
  90. $this->flashes = array();
  91. return $return;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function set($type, $message)
  97. {
  98. $this->flashes[$type] = $message;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function setAll(array $messages)
  104. {
  105. $this->flashes = $messages;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function has($type)
  111. {
  112. return array_key_exists($type, $this->flashes);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function keys()
  118. {
  119. return array_keys($this->flashes);
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function getStorageKey()
  125. {
  126. return $this->storageKey;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function clear()
  132. {
  133. return $this->all();
  134. }
  135. }