chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
 
namespace think\session;
 
use think\contract\SessionHandlerInterface;
use think\helper\Arr;
 
class Store
{
    /**
     * Session数据
     * @var array
     */
    protected $data = [];
 
    /**
     * 是否初始化
     * @var bool
     */
    protected $init = null;
 
    /**
     * 记录Session Id
     * @var string
     */
    protected $id;
 
    /** @var array */
    protected $serialize = [];
 
    public function __construct(protected string $name, protected SessionHandlerInterface $handler, ?array $serialize = null)
    {
        if (!empty($serialize)) {
            $this->serialize = $serialize;
        }
 
        $this->setId();
    }
 
    /**
     * 设置数据
     * @access public
     * @param array $data
     * @return void
     */
    public function setData(array $data): void
    {
        $this->data = $data;
    }
 
    /**
     * session初始化
     * @access public
     * @return void
     */
    public function init(): void
    {
        // 读取缓存数据
        $data = $this->handler->read($this->getId());
 
        if (!empty($data)) {
            $this->data = array_merge($this->data, $this->unserialize($data));
        }
 
        $this->init = true;
    }
 
    /**
     * 设置SessionName
     * @access public
     * @param string $name session_name
     * @return void
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }
 
    /**
     * 获取sessionName
     * @access public
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }
 
    /**
     * session_id设置
     * @access public
     * @param string $id session_id
     * @return void
     */
    public function setId(?string $id = null): void
    {
        $this->id = is_string($id) && strlen($id) === 32 && ctype_alnum($id) ? $id : md5(microtime(true) . session_create_id());
    }
 
    /**
     * 获取session_id
     * @access public
     * @return string
     */
    public function getId(): string
    {
        return $this->id;
    }
 
    /**
     * 获取所有数据
     * @return array
     */
    public function all(): array
    {
        return $this->data;
    }
 
    /**
     * session设置
     * @access public
     * @param string $name  session名称
     * @param mixed  $value session值
     * @return void
     */
    public function set(string $name, $value): void
    {
        Arr::set($this->data, $name, $value);
    }
 
    /**
     * session获取
     * @access public
     * @param string $name    session名称
     * @param mixed  $default 默认值
     * @return mixed
     */
    public function get(string $name, $default = null)
    {
        return Arr::get($this->data, $name, $default);
    }
 
    /**
     * session获取并删除
     * @access public
     * @param string $name session名称
     * @param mixed  $default 默认值
     * @return mixed
     */
    public function pull(string $name, $default = null)
    {
        return Arr::pull($this->data, $name, $default);
    }
 
    /**
     * 添加数据到一个session数组
     * @access public
     * @param string $key
     * @param mixed  $value
     * @return void
     */
    public function push(string $key, $value): void
    {
        $array = $this->get($key, []);
 
        $array[] = $value;
 
        $this->set($key, $array);
    }
 
    /**
     * 判断session数据
     * @access public
     * @param string $name session名称
     * @return bool
     */
    public function has(string $name): bool
    {
        return Arr::has($this->data, $name);
    }
 
    /**
     * 删除session数据
     * @access public
     * @param string $name session名称
     * @return void
     */
    public function delete(string $name): void
    {
        Arr::forget($this->data, $name);
    }
 
    /**
     * 清空session数据
     * @access public
     * @return void
     */
    public function clear(): void
    {
        $this->data = [];
    }
 
    /**
     * 销毁session
     */
    public function destroy(): void
    {
        $this->clear();
 
        $this->regenerate(true);
    }
 
    /**
     * 重新生成session id
     * @param bool $destroy
     */
    public function regenerate(bool $destroy = false): void
    {
        if ($destroy) {
            $this->handler->delete($this->getId());
        }
 
        $this->setId();
    }
 
    /**
     * 保存session数据
     * @access public
     * @return void
     */
    public function save(): void
    {
        $this->clearFlashData();
 
        $sessionId = $this->getId();
 
        if (!empty($this->data)) {
            $data = $this->serialize($this->data);
 
            $this->handler->write($sessionId, $data);
        } else {
            $this->handler->delete($sessionId);
        }
 
        $this->init = false;
    }
 
    /**
     * session设置 下一次请求有效
     * @access public
     * @param string $name  session名称
     * @param mixed  $value session值
     * @return void
     */
    public function flash(string $name, $value): void
    {
        $this->set($name, $value);
        $this->push('__flash__.__next__', $name);
        $this->set('__flash__.__current__', Arr::except($this->get('__flash__.__current__', []), $name));
    }
 
    /**
     * 将本次闪存数据推迟到下次请求
     *
     * @return void
     */
    public function reflash(): void
    {
        $keys   = $this->get('__flash__.__current__', []);
        $values = array_unique(array_merge($this->get('__flash__.__next__', []), $keys));
        $this->set('__flash__.__next__', $values);
        $this->set('__flash__.__current__', []);
    }
 
    /**
     * 清空当前请求的session数据
     * @access public
     * @return void
     */
    public function clearFlashData(): void
    {
        Arr::forget($this->data, $this->get('__flash__.__current__', []));
        if (!empty($next = $this->get('__flash__.__next__', []))) {
            $this->set('__flash__.__current__', $next);
        } else {
            $this->delete('__flash__.__current__');
        }
        $this->delete('__flash__.__next__');
    }
 
    /**
     * 序列化数据
     * @access protected
     * @param mixed $data
     * @return string
     */
    protected function serialize($data): string
    {
        $serialize = $this->serialize[0] ?? 'serialize';
 
        return $serialize($data);
    }
 
    /**
     * 反序列化数据
     * @access protected
     * @param string $data
     * @return array
     */
    protected function unserialize(string $data): array
    {
        $unserialize = $this->serialize[1] ?? 'unserialize';
 
        return (array) $unserialize($data);
    }
}