FlashBag.php 2.6 KB

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