You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
2.7 KiB

3 years ago
  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(string $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(string $type, $message)
  50. {
  51. $this->flashes[$type][] = $message;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function peek(string $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(string $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(string $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(string $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. }