AutoExpireFlashBag.php 3.0 KB

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