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