AutoExpireFlashBag.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 peek($type, $default = null)
  68. {
  69. return $this->has($type) ? $this->flashes['display'][$type] : $default;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function peekAll()
  75. {
  76. return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function get($type, $default = null)
  82. {
  83. $return = $default;
  84. if (!$this->has($type)) {
  85. return $return;
  86. }
  87. if (isset($this->flashes['display'][$type])) {
  88. $return = $this->flashes['display'][$type];
  89. unset($this->flashes['display'][$type]);
  90. }
  91. return $return;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function all()
  97. {
  98. $return = $this->flashes['display'];
  99. $this->flashes = array('new' => array(), 'display' => array());
  100. return $return;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function setAll(array $messages)
  106. {
  107. $this->flashes['new'] = $messages;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function set($type, $message)
  113. {
  114. $this->flashes['new'][$type] = $message;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function has($type)
  120. {
  121. return array_key_exists($type, $this->flashes['display']);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function keys()
  127. {
  128. return array_keys($this->flashes['display']);
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getStorageKey()
  134. {
  135. return $this->storageKey;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function clear()
  141. {
  142. $return = $this->all();
  143. $this->flashes = array('display' => array(), 'new' => array());
  144. return $return;
  145. }
  146. }