AutoExpireFlashBag.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 $name = 'flashes';
  19. private $flashes = array('display' => array(), 'new' => array());
  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. // 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'] : array();
  49. $this->flashes['new'] = array();
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function add($type, $message)
  55. {
  56. $this->flashes['new'][$type][] = $message;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function peek($type, array $default = array())
  62. {
  63. return $this->has($type) ? $this->flashes['display'][$type] : $default;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function peekAll()
  69. {
  70. return array_key_exists('display', $this->flashes) ? (array) $this->flashes['display'] : array();
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function get($type, array $default = array())
  76. {
  77. $return = $default;
  78. if (!$this->has($type)) {
  79. return $return;
  80. }
  81. if (isset($this->flashes['display'][$type])) {
  82. $return = $this->flashes['display'][$type];
  83. unset($this->flashes['display'][$type]);
  84. }
  85. return $return;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function all()
  91. {
  92. $return = $this->flashes['display'];
  93. $this->flashes['display'] = array();
  94. return $return;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function setAll(array $messages)
  100. {
  101. $this->flashes['new'] = $messages;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function set($type, $messages)
  107. {
  108. $this->flashes['new'][$type] = (array) $messages;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function has($type)
  114. {
  115. return array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function keys()
  121. {
  122. return array_keys($this->flashes['display']);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getStorageKey()
  128. {
  129. return $this->storageKey;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function clear()
  135. {
  136. return $this->all();
  137. }
  138. }