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: * FlashBag flash message container.
16: *
17: * \IteratorAggregate implementation is deprecated and will be removed in 3.0.
18: *
19: * @author Drak <drak@zikula.org>
20: */
21: class FlashBag implements FlashBagInterface, \IteratorAggregate
22: {
23: private $name = 'flashes';
24:
25: /**
26: * Flash messages.
27: *
28: * @var array
29: */
30: private $flashes = array();
31:
32: /**
33: * The storage key for flashes in the session.
34: *
35: * @var string
36: */
37: private $storageKey;
38:
39: /**
40: * Constructor.
41: *
42: * @param string $storageKey The key used to store flashes in the session.
43: */
44: public function __construct($storageKey = '_sf2_flashes')
45: {
46: $this->storageKey = $storageKey;
47: }
48:
49: /**
50: * {@inheritdoc}
51: */
52: public function getName()
53: {
54: return $this->name;
55: }
56:
57: public function setName($name)
58: {
59: $this->name = $name;
60: }
61:
62: /**
63: * {@inheritdoc}
64: */
65: public function initialize(array &$flashes)
66: {
67: $this->flashes = &$flashes;
68: }
69:
70: /**
71: * {@inheritdoc}
72: */
73: public function add($type, $message)
74: {
75: $this->flashes[$type][] = $message;
76: }
77:
78: /**
79: * {@inheritdoc}
80: */
81: public function peek($type, array $default = array())
82: {
83: return $this->has($type) ? $this->flashes[$type] : $default;
84: }
85:
86: /**
87: * {@inheritdoc}
88: */
89: public function peekAll()
90: {
91: return $this->flashes;
92: }
93:
94: /**
95: * {@inheritdoc}
96: */
97: public function get($type, array $default = array())
98: {
99: if (!$this->has($type)) {
100: return $default;
101: }
102:
103: $return = $this->flashes[$type];
104:
105: unset($this->flashes[$type]);
106:
107: return $return;
108: }
109:
110: /**
111: * {@inheritdoc}
112: */
113: public function all()
114: {
115: $return = $this->peekAll();
116: $this->flashes = array();
117:
118: return $return;
119: }
120:
121: /**
122: * {@inheritdoc}
123: */
124: public function set($type, $messages)
125: {
126: $this->flashes[$type] = (array) $messages;
127: }
128:
129: /**
130: * {@inheritdoc}
131: */
132: public function setAll(array $messages)
133: {
134: $this->flashes = $messages;
135: }
136:
137: /**
138: * {@inheritdoc}
139: */
140: public function has($type)
141: {
142: return array_key_exists($type, $this->flashes) && $this->flashes[$type];
143: }
144:
145: /**
146: * {@inheritdoc}
147: */
148: public function keys()
149: {
150: return array_keys($this->flashes);
151: }
152:
153: /**
154: * {@inheritdoc}
155: */
156: public function getStorageKey()
157: {
158: return $this->storageKey;
159: }
160:
161: /**
162: * {@inheritdoc}
163: */
164: public function clear()
165: {
166: return $this->all();
167: }
168:
169: /**
170: * Returns an iterator for flashes.
171: *
172: * @deprecated Will be removed in 3.0.
173: *
174: * @return \ArrayIterator An \ArrayIterator instance
175: */
176: public function getIterator()
177: {
178: return new \ArrayIterator($this->all());
179: }
180: }
181: