Completed
Push — 6.0 ( e2b752...6d69f3 )
by yun
05:35
created

Store::setId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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