Completed
Push — 6.0 ( b72c2e...58dbb9 )
by yun
05:58
created

Store::clearFlashData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
12
namespace think\session;
13
14
use think\contract\SessionHandlerInterface;
15
use think\helper\Arr;
16
17
class Store
18
{
19
20
    /**
21
     * Session数据
22
     * @var array
23
     */
24
    protected $data = [];
25
26
    /**
27
     * 是否初始化
28
     * @var bool
29
     */
30
    protected $init = null;
31
32
    /**
33
     * 记录Session name
34
     * @var string
35
     */
36
    protected $name = 'PHPSESSID';
37
38
    /**
39
     * 记录Session Id
40
     * @var string
41
     */
42
    protected $id;
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var SessionHandlerInterface
46
     */
47
    protected $handler;
48
49
    /** @var array */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
50
    protected $serialize = [];
51
52 6
    public function __construct($name, SessionHandlerInterface $handler, array $serialize = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
53
    {
54 6
        $this->name    = $name;
55 6
        $this->handler = $handler;
56
57 6
        if (!empty($serialize)) {
58
            $this->serialize = $serialize;
59
        }
60 6
    }
61
62
    /**
63
     * 设置数据
64
     * @access public
65
     * @param array $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     * @return void
67
     */
68
    public function setData(array $data): void
69
    {
70
        $this->data = $data;
71
    }
72
73
    /**
74
     * session初始化
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
75
     * @access public
76
     * @return void
77
     */
78 3
    public function init(): void
79
    {
80
        // 读取缓存数据
81 3
        $data = $this->handler->read($this->getId());
82
83 3
        if (!empty($data)) {
84 1
            $this->data = array_merge($this->data, $this->unserialize($data));
85
        }
86
87 3
        $this->init = true;
88 3
    }
89
90
    /**
91
     * 设置SessionName
92
     * @access public
93
     * @param string $name session_name
94
     * @return void
95
     */
96 1
    public function setName(string $name): void
97
    {
98 1
        $this->name = $name;
99 1
    }
100
101
    /**
102
     * 获取sessionName
103
     * @access public
104
     * @return string
105
     */
106 1
    public function getName(): string
107
    {
108 1
        return $this->name;
109
    }
110
111
    /**
112
     * session_id设置
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
113
     * @access public
114
     * @param string $id session_id
115
     * @return void
116
     */
117 3
    public function setId($id = null): void
118
    {
119 3
        $this->id = is_string($id) ? $id : md5(microtime(true) . uniqid());
120 3
    }
121
122
    /**
123
     * 获取session_id
124
     * @access public
125
     * @return string
126
     */
127 3
    public function getId(): string
128
    {
129 3
        return $this->id;
130
    }
131
132
    /**
133
     * session设置
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
134
     * @access public
135
     * @param string $name  session名称
136
     * @param mixed  $value session值
137
     * @return void
138
     */
139 5
    public function set(string $name, $value): void
140
    {
141 5
        Arr::set($this->data, $name, $value);
142 5
    }
143
144
    /**
145
     * session获取
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
146
     * @access public
147
     * @param string $name    session名称
148
     * @param mixed  $default 默认值
149
     * @return mixed
150
     */
151 4
    public function get(string $name = '', $default = null)
152
    {
153 4
        return Arr::get($this->data, $name, $default);
154
    }
155
156
    /**
157
     * session获取并删除
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
158
     * @access public
159
     * @param string $name session名称
160
     * @return mixed
161
     */
162 1
    public function pull(string $name)
163
    {
164 1
        return Arr::pull($this->data, $name);
165
    }
166
167
    /**
168
     * 添加数据到一个session数组
169
     * @access public
170
     * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
171
     * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
172
     * @return void
173
     */
174 1
    public function push(string $key, $value): void
175
    {
176 1
        $array = $this->get($key, []);
177
178 1
        $array[] = $value;
179
180 1
        $this->set($key, $array);
181 1
    }
182
183
    /**
184
     * 判断session数据
185
     * @access public
186
     * @param string $name session名称
187
     * @return bool
188
     */
189 4
    public function has(string $name): bool
190
    {
191 4
        return Arr::exists($this->data, $name);
192
    }
193
194
    /**
195
     * 删除session数据
196
     * @access public
197
     * @param string $name session名称
198
     * @return void
199
     */
200 2
    public function delete(string $name): void
201
    {
202 2
        Arr::forget($this->data, $name);
203 2
    }
204
205
    /**
206
     * 清空session数据
207
     * @access public
208
     * @return void
209
     */
210 2
    public function clear(): void
211
    {
212 2
        $this->data = [];
213 2
    }
214
215
    /**
216
     * 销毁session
217
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
218 1
    public function destroy(): void
219
    {
220 1
        $this->clear();
221
222 1
        $this->regenerate(true);
223 1
    }
224
225
    /**
226
     * 重新生成session id
227
     * @param bool $destroy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
228
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
229 1
    public function regenerate(bool $destroy = false): void
230
    {
231 1
        if ($destroy) {
232 1
            $this->handler->delete($this->getId());
233
        }
234
235 1
        $this->setId();
236 1
    }
237
238
    /**
239
     * 保存session数据
240
     * @access public
241
     * @return void
242
     */
243 1
    public function save()
244
    {
245 1
        $this->clearFlashData();
246
247 1
        $sessionId = $this->getId();
248
249 1
        if (!empty($this->data)) {
250 1
            $data = $this->serialize($this->data);
251
252 1
            $this->handler->write($sessionId, $data);
253
        } else {
254
            $this->handler->delete($sessionId);
255
        }
256
257 1
        $this->init = false;
258 1
    }
259
260
    /**
261
     * session设置 下一次请求有效
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
262
     * @access public
263
     * @param string $name  session名称
264
     * @param mixed  $value session值
265
     * @return void
266
     */
267 1
    public function flash(string $name, $value): void
268
    {
269 1
        $this->set($name, $value);
270 1
        $this->push('__flash__.__next__', $name);
271 1
        $this->set('__flash__.__current__', Arr::except($this->get('__flash__.__current__', []), $name));
272 1
    }
273
274
    /**
275
     * 将本次闪存数据推迟到下次请求
276
     *
277
     * @return void
278
     */
279 1
    public function reflash()
280
    {
281 1
        $keys   = $this->get('__flash__.__current__', []);
282 1
        $values = array_unique(array_merge($this->get('__flash__.__next__', []), $keys));
283 1
        $this->set('__flash__.__next__', $values);
284 1
        $this->set('__flash__.__current__', []);
285 1
    }
286
287
    /**
288
     * 清空当前请求的session数据
289
     * @access public
290
     * @return void
291
     */
292 2
    public function clearFlashData()
293
    {
294 2
        Arr::forget($this->data, $this->get('__flash__.__current__', []));
295 2
        if (!empty($next = $this->get('__flash__.__next__', []))) {
296 1
            $this->set('__flash__.__current__', $next);
297
        } else {
298 2
            $this->delete('__flash__.__current__');
299
        }
300 2
        $this->delete('__flash__.__next__');
301 2
    }
302
303
    /**
304
     * 序列化数据
305
     * @access protected
306
     * @param mixed $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
307
     * @return string
308
     */
309 1
    protected function serialize($data): string
310
    {
311 1
        $serialize = $this->serialize[0] ?? 'serialize';
312
313 1
        return $serialize($data);
314
    }
315
316
    /**
317
     * 反序列化数据
318
     * @access protected
319
     * @param string $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
320
     * @return array
321
     */
322 1
    protected function unserialize(string $data): array
323
    {
324 1
        $unserialize = $this->serialize[1] ?? 'unserialize';
325
326 1
        return (array) $unserialize($data);
327
    }
328
329
}
330