Completed
Branch 6.0 (d30585)
by yun
04:17
created

Store::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

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