AutoExpireFlashBag.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  20. * Flash messages.
  21. *
  22. * @var array
  23. */
  24. private $flashes = array();
  25. /**
  26. * The storage key for flashes in the session
  27. *
  28. * @var string
  29. */
  30. private $storageKey;
  31. /**
  32. * Constructor.
  33. *
  34. * @param string $storageKey The key used to store flashes in the session.
  35. */
  36. public function __construct($storageKey = '_sf2_flashes')
  37. {
  38. $this->storageKey = $storageKey;
  39. $this->flashes = array('display' => array(), 'new' => array());
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getName()
  45. {
  46. return $this->name;
  47. }
  48. public function setName($name)
  49. {
  50. $this->name = $name;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function initialize(array &$flashes)
  56. {
  57. $this->flashes = &$flashes;
  58. // The logic: messages from the last request will be stored in new, so we move them to previous
  59. // This request we will show what is in 'display'. What is placed into 'new' this time round will
  60. // be moved to display next time round.
  61. $this->flashes['display'] = array_key_exists('new', $this->flashes) ? $this->flashes['new'] : array();
  62. $this->flashes['new'] = array();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function add($type, $message)
  68. {
  69. $this->flashes['new'][$type][] = $message;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function peek($type, array $default = array())
  75. {
  76. return $this->has($type) ? $this->flashes['display'][$type] : $default;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function peekAll()
  82. {
  83. return array_key_exists('display', $this->flashes) ? (array) $this->flashes['display'] : array();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function get($type, array $default = array())
  89. {
  90. $return = $default;
  91. if (!$this->has($type)) {
  92. return $return;
  93. }
  94. if (isset($this->flashes['display'][$type])) {
  95. $return = $this->flashes['display'][$type];
  96. unset($this->flashes['display'][$type]);
  97. }
  98. return $return;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function all()
  104. {
  105. $return = $this->flashes['display'];
  106. $this->flashes = array('new' => array(), 'display' => array());
  107. return $return;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function setAll(array $messages)
  113. {
  114. $this->flashes['new'] = $messages;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function set($type, $messages)
  120. {
  121. $this->flashes['new'][$type] = (array) $messages;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function has($type)
  127. {
  128. return array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function keys()
  134. {
  135. return array_keys($this->flashes['display']);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getStorageKey()
  141. {
  142. return $this->storageKey;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function clear()
  148. {
  149. return $this->all();
  150. }
  151. }