1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\HttpFoundation\Session\Flash;
13:
14: /**
15: * AutoExpireFlashBag flash message container.
16: *
17: * @author Drak <drak@zikula.org>
18: */
19: class AutoExpireFlashBag implements FlashBagInterface
20: {
21: private $name = 'flashes';
22:
23: /**
24: * Flash messages.
25: *
26: * @var array
27: */
28: private $flashes = array();
29:
30: /**
31: * The storage key for flashes in the session
32: *
33: * @var string
34: */
35: private $storageKey;
36:
37: /**
38: * Constructor.
39: *
40: * @param string $storageKey The key used to store flashes in the session.
41: */
42: public function __construct($storageKey = '_sf2_flashes')
43: {
44: $this->storageKey = $storageKey;
45: $this->flashes = array('display' => array(), 'new' => array());
46: }
47:
48: /**
49: * {@inheritdoc}
50: */
51: public function getName()
52: {
53: return $this->name;
54: }
55:
56: public function setName($name)
57: {
58: $this->name = $name;
59: }
60:
61: /**
62: * {@inheritdoc}
63: */
64: public function initialize(array &$flashes)
65: {
66: $this->flashes = &$flashes;
67:
68: // The logic: messages from the last request will be stored in new, so we move them to previous
69: // This request we will show what is in 'display'. What is placed into 'new' this time round will
70: // be moved to display next time round.
71: $this->flashes['display'] = array_key_exists('new', $this->flashes) ? $this->flashes['new'] : array();
72: $this->flashes['new'] = array();
73: }
74:
75: /**
76: * {@inheritdoc}
77: */
78: public function add($type, $message)
79: {
80: $this->flashes['new'][$type][] = $message;
81: }
82:
83: /**
84: * {@inheritdoc}
85: */
86: public function peek($type, array $default = array())
87: {
88: return $this->has($type) ? $this->flashes['display'][$type] : $default;
89: }
90:
91: /**
92: * {@inheritdoc}
93: */
94: public function peekAll()
95: {
96: return array_key_exists('display', $this->flashes) ? (array) $this->flashes['display'] : array();
97: }
98:
99: /**
100: * {@inheritdoc}
101: */
102: public function get($type, array $default = array())
103: {
104: $return = $default;
105:
106: if (!$this->has($type)) {
107: return $return;
108: }
109:
110: if (isset($this->flashes['display'][$type])) {
111: $return = $this->flashes['display'][$type];
112: unset($this->flashes['display'][$type]);
113: }
114:
115: return $return;
116: }
117:
118: /**
119: * {@inheritdoc}
120: */
121: public function all()
122: {
123: $return = $this->flashes['display'];
124: $this->flashes = array('new' => array(), 'display' => array());
125:
126: return $return;
127: }
128:
129: /**
130: * {@inheritdoc}
131: */
132: public function setAll(array $messages)
133: {
134: $this->flashes['new'] = $messages;
135: }
136:
137: /**
138: * {@inheritdoc}
139: */
140: public function set($type, $messages)
141: {
142: $this->flashes['new'][$type] = (array) $messages;
143: }
144:
145: /**
146: * {@inheritdoc}
147: */
148: public function has($type)
149: {
150: return array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
151: }
152:
153: /**
154: * {@inheritdoc}
155: */
156: public function keys()
157: {
158: return array_keys($this->flashes['display']);
159: }
160:
161: /**
162: * {@inheritdoc}
163: */
164: public function getStorageKey()
165: {
166: return $this->storageKey;
167: }
168:
169: /**
170: * {@inheritdoc}
171: */
172: public function clear()
173: {
174: return $this->all();
175: }
176: }
177: