AutoExpireFlashBag.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  21. * @param string $storageKey The key used to store flashes in the session
  22. */
  23. public function __construct(
  24. private string $storageKey = '_symfony_flashes',
  25. ) {
  26. }
  27. public function getName(): string
  28. {
  29. return $this->name;
  30. }
  31. public function setName(string $name): void
  32. {
  33. $this->name = $name;
  34. }
  35. public function initialize(array &$flashes): void
  36. {
  37. $this->flashes = &$flashes;
  38. // The logic: messages from the last request will be stored in new, so we move them to previous
  39. // This request we will show what is in 'display'. What is placed into 'new' this time round will
  40. // be moved to display next time round.
  41. $this->flashes['display'] = \array_key_exists('new', $this->flashes) ? $this->flashes['new'] : [];
  42. $this->flashes['new'] = [];
  43. }
  44. public function add(string $type, mixed $message): void
  45. {
  46. $this->flashes['new'][$type][] = $message;
  47. }
  48. public function peek(string $type, array $default = []): array
  49. {
  50. return $this->has($type) ? $this->flashes['display'][$type] : $default;
  51. }
  52. public function peekAll(): array
  53. {
  54. return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : [];
  55. }
  56. public function get(string $type, array $default = []): array
  57. {
  58. $return = $default;
  59. if (!$this->has($type)) {
  60. return $return;
  61. }
  62. if (isset($this->flashes['display'][$type])) {
  63. $return = $this->flashes['display'][$type];
  64. unset($this->flashes['display'][$type]);
  65. }
  66. return $return;
  67. }
  68. public function all(): array
  69. {
  70. $return = $this->flashes['display'];
  71. $this->flashes['display'] = [];
  72. return $return;
  73. }
  74. public function setAll(array $messages): void
  75. {
  76. $this->flashes['new'] = $messages;
  77. }
  78. public function set(string $type, string|array $messages): void
  79. {
  80. $this->flashes['new'][$type] = (array) $messages;
  81. }
  82. public function has(string $type): bool
  83. {
  84. return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
  85. }
  86. public function keys(): array
  87. {
  88. return array_keys($this->flashes['display']);
  89. }
  90. public function getStorageKey(): string
  91. {
  92. return $this->storageKey;
  93. }
  94. public function clear(): mixed
  95. {
  96. return $this->all();
  97. }
  98. }