Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c969423

Browse files
author
Drak
committed
[HttpFoundation] Added FlashBagInterface and concrete implementation.
This commit outsources the flash message processing to it's own interface. Overall flash messages now can have multiple flash types and each type can store multiple messages. For convenience there are now four flash types by default, INFO, NOTICE, WARNING and ERROR. There are two concrete implementations: one preserving the old behaviour of flash messages expiring exactly after one page load, regardless of being displayed or not; and the other where flash messages persist until explicitly popped.
1 parent 39288bc commit c969423

File tree

5 files changed

+746
-0
lines changed

5 files changed

+746
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
/**
15+
* AutoExpireFlashBag flash message container.
16+
*
17+
* @author Drak <[email protected]>
18+
*/
19+
class AutoExpireFlashBag implements FlashBagInterface
20+
{
21+
/**
22+
* Flash messages.
23+
*
24+
* @var array
25+
*/
26+
private $flashes = array();
27+
28+
/**
29+
* The storage key for flashes in the session
30+
*
31+
* @var string
32+
*/
33+
private $storageKey;
34+
35+
/**
36+
* Constructor.
37+
*
38+
* @param type $storageKey The key used to store flashes in the session.
39+
*/
40+
public function __construct($storageKey = '_sf2_flashes')
41+
{
42+
$this->storageKey = $storageKey;
43+
$this->flashes = array('display' => array(), 'new' => array());
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function initialize(array &$flashes)
50+
{
51+
$this->flashes = &$flashes;
52+
53+
// The logic: messages from the last request will be stored in new, so we move them to previous
54+
// This request we will show what is in 'display'. What is placed into 'new' this time round will
55+
// be moved to display next time round.
56+
$this->flashes['display'] = array_key_exists('new', $this->flashes) ? $this->flashes['new'] : array();
57+
$this->flashes['new'] = array();
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function add($message, $type = self::NOTICE)
64+
{
65+
$this->flashes['new'][$type][] = $message;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function get($type)
72+
{
73+
if (!$this->has($type)) {
74+
return array();
75+
}
76+
77+
return $this->flashes['display'][$type];
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function pop($type)
84+
{
85+
if (!$this->has($type)) {
86+
return array();
87+
}
88+
89+
return $this->clear($type);
90+
}
91+
92+
/**
93+
* {@inheritdoc}
94+
*/
95+
public function popAll()
96+
{
97+
return $this->clearAll();
98+
}
99+
100+
/**
101+
* {@inheritdoc}
102+
*/
103+
public function set($type, array $array)
104+
{
105+
$this->flashes['new'][$type] = $array;
106+
}
107+
108+
/**
109+
* {@inheritdoc}
110+
*/
111+
public function has($type)
112+
{
113+
return array_key_exists($type, $this->flashes['display']);
114+
}
115+
116+
/**
117+
* {@inheritdoc}
118+
*/
119+
public function keys()
120+
{
121+
return array_keys($this->flashes['display']);
122+
}
123+
124+
/**
125+
* {@inheritdoc}
126+
*/
127+
public function all()
128+
{
129+
return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array();
130+
}
131+
132+
/**
133+
* {@inheritdoc}
134+
*/
135+
public function clear($type)
136+
{
137+
$return = array();
138+
if (isset($this->flashes['new'][$type])) {
139+
unset($this->flashes['new'][$type]);
140+
}
141+
142+
if (isset($this->flashes['display'][$type])) {
143+
$return = $this->flashes['display'][$type];
144+
unset($this->flashes['display'][$type]);
145+
}
146+
147+
return $return;
148+
}
149+
150+
/**
151+
* {@inheritdoc}
152+
*/
153+
public function clearAll()
154+
{
155+
$return = $this->flashes['display'];
156+
$this->flashes = array('new' => array(), 'display' => array());
157+
158+
return $return;
159+
}
160+
161+
/**
162+
* {@inheritdoc}
163+
*/
164+
public function getStorageKey()
165+
{
166+
return $this->storageKey;
167+
}
168+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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;
13+
14+
/**
15+
* FlashBag flash message container.
16+
*
17+
* @author Drak <[email protected]>
18+
*/
19+
class FlashBag implements FlashBagInterface
20+
{
21+
/**
22+
* Flash messages.
23+
*
24+
* @var array
25+
*/
26+
private $flashes = array();
27+
28+
/**
29+
* The storage key for flashes in the session
30+
*
31+
* @var string
32+
*/
33+
private $storageKey;
34+
35+
/**
36+
* Constructor.
37+
*
38+
* @param type $storageKey The key used to store flashes in the session.
39+
*/
40+
public function __construct($storageKey = '_sf2_flashes')
41+
{
42+
$this->storageKey = $storageKey;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function initialize(array &$flashes)
49+
{
50+
$this->flashes = &$flashes;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function add($message, $type = self::NOTICE)
57+
{
58+
$this->flashes[$type][] = $message;
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function get($type)
65+
{
66+
if (!$this->has($type)) {
67+
return array();
68+
}
69+
70+
return $this->flashes[$type];
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function pop($type)
77+
{
78+
if (!$this->has($type)) {
79+
return array();
80+
}
81+
82+
return $this->clear($type);
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public function popAll()
89+
{
90+
return $this->clearAll();
91+
}
92+
93+
/**
94+
* {@inheritdoc}
95+
*/
96+
public function set($type, array $array)
97+
{
98+
$this->flashes[$type] = $array;
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function has($type)
105+
{
106+
return array_key_exists($type, $this->flashes);
107+
}
108+
109+
/**
110+
* {@inheritdoc}
111+
*/
112+
public function keys()
113+
{
114+
return array_keys($this->flashes);
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function all()
121+
{
122+
return $this->flashes;
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function clear($type)
129+
{
130+
$return = array();
131+
if (isset($this->flashes[$type])) {
132+
$return = $this->flashes[$type];
133+
unset($this->flashes[$type]);
134+
}
135+
136+
return $return;
137+
}
138+
139+
/**
140+
* {@inheritdoc}
141+
*/
142+
public function clearAll()
143+
{
144+
$return = $this->flashes;
145+
$this->flashes = array();
146+
147+
return $return;
148+
}
149+
150+
/**
151+
* {@inheritdoc}
152+
*/
153+
public function getStorageKey()
154+
{
155+
return $this->storageKey;
156+
}
157+
}

0 commit comments

Comments
 (0)