Passed
Push — master ( f9244b...ce4141 )
by 世昌
02:39 queued 14s
created

PHPSession::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace suda\framework\session;
3
4
use Exception;
5
use suda\framework\Request;
6
use suda\framework\Session;
7
use suda\framework\Response;
8
use suda\framework\filesystem\FileSystem;
9
10
/**
11
 * PHP Session
12
 */
13
class PHPSession implements Session
14
{
15
    /**
16
     * 会话ID
17
     *
18
     * @var string
19
     */
20
    protected $id;
21
22
    /**
23
     * Session对象配置
24
     *
25
     * @var array
26
     */
27
    protected $config;
28
29
    /**
30
     * 请求
31
     *
32
     * @var Request
33
     */
34
    protected $request;
35
36
    /**
37
     * @var Response
38
     */
39
    protected $response;
40
41
    /**
42
     * 创建Session
43
     *
44
     * @param Request $request 请求
45
     * @param Response $response 响应
46
     * @param array $config 配置属性
47
     * @throws Exception
48
     */
49
    public function __construct(Request $request, Response $response, array $config = [])
50
    {
51
        $this->config = $config;
52
        $this->request = $request;
53
        $this->response = $response;
54
        $response->getContext()->getEvent()->register('response::before-send', [$this, 'write']);
55
        if (session_status() === PHP_SESSION_NONE) {
56
            $this->init($request, $config);
57
        } else {
58
            $this->update();
59
        }
60
    }
61
62
    /**
63
     * @param Request $request
64
     * @param array $config
65
     * @throws Exception
66
     */
67
    protected function init(Request $request, array $config)
68
    {
69
        if (array_key_exists('path', $config)) {
70
            $path = $config['path'];
71
        } elseif (defined('SUDA_DATA')) {
72
            $path = constant('SUDA_DATA').'/session';
73
        } else {
74
            throw new Exception('php session save path missing');
75
        }
76
        $name = $config['name'] ?? 'php_session';
77
        FileSystem::make($path);
78
79
        if (is_string($id = $request->getCookie($name))) {
80
            session_id($id);
81
        } else {
82
            $id = md5($path.$request->getRemoteAddr().uniqid());
83
            session_id($id);
84
        }
85
        session_save_path($path);
86
        session_name($name);
87
        session_cache_limiter($config['limiter'] ?? 'private');
88
        session_cache_expire($config['expire'] ?? 0);
89
        session_start();
90
        $this->id = $id;
91
    }
92
93
    /**
94
     * 设置Session
95
     *
96
     * @param string $name
97
     * @param mixed $value
98
     * @return bool
99
     */
100
    public function set(string $name, $value):bool
101
    {
102
        $_SESSION[$name] = $value;
103
        return $this->has($name);
104
    }
105
106
    /**
107
     * 获取Session
108
     *
109
     * @param string $name
110
     * @param mixed $default
111
     * @return mixed
112
     */
113
    public function get(string $name = null, $default = null)
114
    {
115
        if ($name !== null) {
116
            return $this->has($name) ?$_SESSION[$name]:$default;
117
        } else {
118
            return $_SESSION;
119
        }
120
    }
121
122
    /**
123
     * 删除一个或者全部Session数据
124
     *
125
     * @param string|null $name
126
     * @return bool
127
     */
128
    public function delete(?string $name = null):bool
129
    {
130
        if (null === $name) {
131
            session_unset();
132
        } else {
133
            unset($_SESSION[$name]);
134
        }
135
        return true;
136
    }
137
138
    /**
139
     * 检测是否存在Session
140
     *
141
     * @param string $name
142
     * @return boolean
143
     */
144
    public function has(string $name):bool
145
    {
146
        return array_key_exists($name, $_SESSION);
147
    }
148
149
    /**
150
     * 获取Session唯一ID
151
     *
152
     * @return string
153
     */
154
    public function id():string
155
    {
156
        return $this->id;
157
    }
158
159
    /**
160
     * 销毁Session
161
     *
162
     * @return boolean
163
     */
164
    public function destroy():bool
165
    {
166
        session_unset();
167
        session_destroy();
168
        return true;
169
    }
170
171
    /**
172
     * 更新SessionId
173
     *
174
     * @return boolean
175
     * @throws Exception
176
     */
177
    public function update():bool
178
    {
179
        $this->destroy();
180
        $this->init($this->request, $this->config);
181
        return true;
182
    }
183
184
    /**
185
     * 写入Session到响应
186
     */
187
    public function write()
188
    {
189
        if (session_status() !== PHP_SESSION_NONE) {
190
            session_write_close();
191
        }
192
    }
193
}
194