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