-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFlash.php
120 lines (90 loc) · 2.54 KB
/
Flash.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace Rakit\Session;
class Flash {
const KEY_LIFE = 'n';
const KEY_VALUE = 'v';
protected $session_manager;
protected $flash_key;
public function __construct(SessionManager $session_manager, $flash_key = '__flash')
{
$this->session_manager = $session_manager;
$this->flash_key = $flash_key;
$flash = $this->raw();
foreach($flash as $key => $data) {
$flash[$key][static::KEY_LIFE] = intval(@$data[static::KEY_LIFE]) - 1;
if($flash[$key][static::KEY_LIFE] < 0) {
unset($flash[$key]);
}
}
$this->session_manager->set($this->flash_key, $flash);
}
public function getSessionManager()
{
return $this->session_manager;
}
public function raw()
{
return $this->session_manager->get($this->flash_key, array());
}
public function all()
{
$raw_flash = $this->raw();
$flash = array();
foreach($raw_flash as $key => $data) {
$flash[$key] = $data[static::KEY_VALUE];
}
return $flash;
}
public function has($key)
{
$flash = $this->raw();
return array_key_exists($key, $flash);
}
public function get($key, $default = null)
{
$flash = $this->raw();
return array_key_exists($key, $flash)? @$flash[$key][static::KEY_VALUE] : $default;
}
public function set($key, $value, $life = 1)
{
$flashdata = array();
$flashdata[static::KEY_LIFE] = $life;
$flashdata[static::KEY_VALUE] = $value;
$flash = $this->raw();
$flash[$key] = $flashdata;
$this->session_manager->set($this->flash_key, $flash);
}
public function keep($key, $life = 1)
{
if(0 === func_get_args()) {
$flash = $this->raw();
foreach($flash as $key => $data) {
$this->set($key, $this->get($key), 1);
}
} else {
$this->set($key, $this->get($key), $life);
}
}
public function remove($key)
{
$flash = $this->raw();
unset($flash[$key]);
$this->session_manager->set($this->flash_key, $flash);
}
public function __get($key)
{
return $this->get($key);
}
public function __set($key, $value)
{
return $this->set($key, $value);
}
public function __isset($key)
{
return $this->has($key);
}
public function __unset($key)
{
return $this->remove($key);
}
}