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

Store::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
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
     * 获取所有数据
134
     * @return array
135
     */
136
    public function all()
137
    {
138
        return $this->data;
139
    }
140
141
    /**
142
     * session设置
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
143
     * @access public
144
     * @param string $name  session名称
145
     * @param mixed  $value session值
146
     * @return void
147
     */
148 5
    public function set(string $name, $value): void
149
    {
150 5
        Arr::set($this->data, $name, $value);
151 5
    }
152
153
    /**
154
     * session获取
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
155
     * @access public
156
     * @param string $name    session名称
157
     * @param mixed  $default 默认值
158
     * @return mixed
159
     */
160 4
    public function get(string $name, $default = null)
161
    {
162 4
        return Arr::get($this->data, $name, $default);
163
    }
164
165
    /**
166
     * session获取并删除
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
167
     * @access public
168
     * @param string $name session名称
169
     * @return mixed
170
     */
171 1
    public function pull(string $name)
172
    {
173 1
        return Arr::pull($this->data, $name);
174
    }
175
176
    /**
177
     * 添加数据到一个session数组
178
     * @access public
179
     * @param string $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
180
     * @param mixed  $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
181
     * @return void
182
     */
183 1
    public function push(string $key, $value): void
184
    {
185 1
        $array = $this->get($key, []);
186
187 1
        $array[] = $value;
188
189 1
        $this->set($key, $array);
190 1
    }
191
192
    /**
193
     * 判断session数据
194
     * @access public
195
     * @param string $name session名称
196
     * @return bool
197
     */
198 4
    public function has(string $name): bool
199
    {
200 4
        return Arr::exists($this->data, $name);
201
    }
202
203
    /**
204
     * 删除session数据
205
     * @access public
206
     * @param string $name session名称
207
     * @return void
208
     */
209 2
    public function delete(string $name): void
210
    {
211 2
        Arr::forget($this->data, $name);
212 2
    }
213
214
    /**
215
     * 清空session数据
216
     * @access public
217
     * @return void
218
     */
219 2
    public function clear(): void
220
    {
221 2
        $this->data = [];
222 2
    }
223
224
    /**
225
     * 销毁session
226
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
227 1
    public function destroy(): void
228
    {
229 1
        $this->clear();
230
231 1
        $this->regenerate(true);
232 1
    }
233
234
    /**
235
     * 重新生成session id
236
     * @param bool $destroy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
237
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
238 1
    public function regenerate(bool $destroy = false): void
239
    {
240 1
        if ($destroy) {
241 1
            $this->handler->delete($this->getId());
242
        }
243
244 1
        $this->setId();
245 1
    }
246
247
    /**
248
     * 保存session数据
249
     * @access public
250
     * @return void
251
     */
252 1
    public function save()
253
    {
254 1
        $this->clearFlashData();
255
256 1
        $sessionId = $this->getId();
257
258 1
        if (!empty($this->data)) {
259 1
            $data = $this->serialize($this->data);
260
261 1
            $this->handler->write($sessionId, $data);
262
        } else {
263
            $this->handler->delete($sessionId);
264
        }
265
266 1
        $this->init = false;
267 1
    }
268
269
    /**
270
     * session设置 下一次请求有效
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
271
     * @access public
272
     * @param string $name  session名称
273
     * @param mixed  $value session值
274
     * @return void
275
     */
276 1
    public function flash(string $name, $value): void
277
    {
278 1
        $this->set($name, $value);
279 1
        $this->push('__flash__.__next__', $name);
280 1
        $this->set('__flash__.__current__', Arr::except($this->get('__flash__.__current__', []), $name));
281 1
    }
282
283
    /**
284
     * 将本次闪存数据推迟到下次请求
285
     *
286
     * @return void
287
     */
288 1
    public function reflash()
289
    {
290 1
        $keys   = $this->get('__flash__.__current__', []);
291 1
        $values = array_unique(array_merge($this->get('__flash__.__next__', []), $keys));
292 1
        $this->set('__flash__.__next__', $values);
293 1
        $this->set('__flash__.__current__', []);
294 1
    }
295
296
    /**
297
     * 清空当前请求的session数据
298
     * @access public
299
     * @return void
300
     */
301 2
    public function clearFlashData()
302
    {
303 2
        Arr::forget($this->data, $this->get('__flash__.__current__', []));
304 2
        if (!empty($next = $this->get('__flash__.__next__', []))) {
305 1
            $this->set('__flash__.__current__', $next);
306
        } else {
307 2
            $this->delete('__flash__.__current__');
308
        }
309 2
        $this->delete('__flash__.__next__');
310 2
    }
311
312
    /**
313
     * 序列化数据
314
     * @access protected
315
     * @param mixed $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
316
     * @return string
317
     */
318 1
    protected function serialize($data): string
319
    {
320 1
        $serialize = $this->serialize[0] ?? 'serialize';
321
322 1
        return $serialize($data);
323
    }
324
325
    /**
326
     * 反序列化数据
327
     * @access protected
328
     * @param string $data
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
329
     * @return array
330
     */
331 1
    protected function unserialize(string $data): array
332
    {
333 1
        $unserialize = $this->serialize[1] ?? 'unserialize';
334
335 1
        return (array) $unserialize($data);
336
    }
337
338
}
339