AutoExpireFlashBag.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * AutoExpireFlashBag flash message container.
  13. *
  14. * @author Drak <drak@zikula.org>
  15. */
  16. class AutoExpireFlashBag implements FlashBagInterface
  17. {
  18. private string $name = 'flashes';
  19. private array $flashes = ['display' => [], 'new' => []];
  20. private string $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. public function getName(): string
  29. {
  30. return $this->name;
  31. }
  32. /**
  33. * @return void
  34. */
  35. public function setName(string $name)
  36. {
  37. $this->name = $name;
  38. }
  39. /**
  40. * @return void
  41. */
  42. public function initialize(array &$flashes)
  43. {
  44. $this->flashes = &$flashes;
  45. // The logic: messages from the last request will be stored in new, so we move them to previous
  46. // This request we will show what is in 'display'. What is placed into 'new' this time round will
  47. // be moved to display next time round.
  48. $this->flashes['display'] = \array_key_exists('new', $this->flashes) ? $this->flashes['new'] : [];
  49. $this->flashes['new'] = [];
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function add(string $type, mixed $message)
  55. {
  56. $this->flashes['new'][$type][] = $message;
  57. }
  58. public function peek(string $type, array $default = []): array
  59. {
  60. return $this->has($type) ? $this->flashes['display'][$type] : $default;
  61. }
  62. public function peekAll(): array
  63. {
  64. return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : [];
  65. }
  66. public function get(string $type, array $default = []): array
  67. {
  68. $return = $default;
  69. if (!$this->has($type)) {
  70. return $return;
  71. }
  72. if (isset($this->flashes['display'][$type])) {
  73. $return = $this->flashes['display'][$type];
  74. unset($this->flashes['display'][$type]);
  75. }
  76. return $return;
  77. }
  78. public function all(): array
  79. {
  80. $return = $this->flashes['display'];
  81. $this->flashes['display'] = [];
  82. return $return;
  83. }
  84. /**
  85. * @return void
  86. */
  87. public function setAll(array $messages)
  88. {
  89. $this->flashes['new'] = $messages;
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function set(string $type, string|array $messages)
  95. {
  96. $this->flashes['new'][$type] = (array) $messages;
  97. }
  98. public function has(string $type): bool
  99. {
  100. return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
  101. }
  102. public function keys(): array
  103. {
  104. return array_keys($this->flashes['display']);
  105. }
  106. public function getStorageKey(): string
  107. {
  108. return $this->storageKey;
  109. }
  110. public function clear(): mixed
  111. {
  112. return $this->all();
  113. }
  114. }