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

Cookie   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 10.17%

Importance

Changes 0
Metric Value
dl 0
loc 210
rs 10
c 0
b 0
f 0
ccs 6
cts 59
cp 0.1017
wmc 22
lcom 2
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __make() 0 4 1
A get() 0 4 1
A has() 0 4 1
B set() 0 21 6
A setCookie() 0 4 1
A forever() 0 10 3
A delete() 0 4 1
A getCookie() 0 4 1
A save() 0 17 4
A saveCookie() 0 15 2
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: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use DateTimeInterface;
16
17
/**
18
 * Cookie管理类
19
 * @package think
20
 */
21
class Cookie
22
{
23
    /**
24
     * 配置参数
25
     * @var array
26
     */
27
    protected $config = [
28
        // cookie 保存时间
29
        'expire'   => 0,
30
        // cookie 保存路径
31
        'path'     => '/',
32
        // cookie 有效域名
33
        'domain'   => '',
34
        //  cookie 启用安全传输
35
        'secure'   => false,
36
        // httponly设置
37
        'httponly' => false,
38
        // samesite 设置,支持 'strict' 'lax'
39
        'samesite' => '',
40
    ];
41
42
    /**
43
     * Cookie写入数据
44
     * @var array
45
     */
46
    protected $cookie = [];
47
48
    /**
49
     * 当前Request对象
50
     * @var Request
51
     */
52
    protected $request;
53
54
    /**
55
     * 构造方法
56
     * @access public
57
     */
58 18
    public function __construct(Request $request, array $config = [])
59
    {
60 18
        $this->request = $request;
61 18
        $this->config  = array_merge($this->config, array_change_key_case($config));
62 18
    }
63
64 18
    public static function __make(Request $request, Config $config)
65
    {
66 18
        return new static($request, $config->get('cookie'));
67
    }
68
69
    /**
70
     * 获取cookie
71
     * @access public
72
     * @param  mixed  $name 数据名称
73
     * @param  string $default 默认值
74
     * @return mixed
75
     */
76
    public function get(string $name = '', $default = null)
77
    {
78
        return $this->request->cookie($name, $default);
79
    }
80
81
    /**
82
     * 是否存在Cookie参数
83
     * @access public
84
     * @param  string $name 变量名
85
     * @return bool
86
     */
87
    public function has(string $name): bool
88
    {
89
        return $this->request->has($name, 'cookie');
90
    }
91
92
    /**
93
     * Cookie 设置
94
     *
95
     * @access public
96
     * @param  string $name  cookie名称
97
     * @param  string $value cookie值
98
     * @param  mixed  $option 可选参数
99
     * @return void
100
     */
101
    public function set(string $name, string $value, $option = null): void
102
    {
103
        // 参数设置(会覆盖黙认设置)
104
        if (!is_null($option)) {
105
            if (is_numeric($option) || $option instanceof DateTimeInterface) {
106
                $option = ['expire' => $option];
107
            }
108
109
            $config = array_merge($this->config, array_change_key_case($option));
110
        } else {
111
            $config = $this->config;
112
        }
113
114
        if ($config['expire'] instanceof DateTimeInterface) {
115
            $expire = $config['expire']->getTimestamp();
116
        } else {
117
            $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
118
        }
119
120
        $this->setCookie($name, $value, $expire, $config);
121
    }
122
123
    /**
124
     * Cookie 保存
125
     *
126
     * @access public
127
     * @param  string $name  cookie名称
128
     * @param  string $value cookie值
129
     * @param  int    $expire 有效期
130
     * @param  array  $option 可选参数
131
     * @return void
132
     */
133
    protected function setCookie(string $name, string $value, int $expire, array $option = []): void
134
    {
135
        $this->cookie[$name] = [$value, $expire, $option];
136
    }
137
138
    /**
139
     * 永久保存Cookie数据
140
     * @access public
141
     * @param  string $name  cookie名称
142
     * @param  string $value cookie值
143
     * @param  mixed  $option 可选参数 可能会是 null|integer|string
144
     * @return void
145
     */
146
    public function forever(string $name, string $value = '', $option = null): void
147
    {
148
        if (is_null($option) || is_numeric($option)) {
149
            $option = [];
150
        }
151
152
        $option['expire'] = 315360000;
153
154
        $this->set($name, $value, $option);
155
    }
156
157
    /**
158
     * Cookie删除
159
     * @access public
160
     * @param  string $name cookie名称
161
     * @return void
162
     */
163
    public function delete(string $name): void
164
    {
165
        $this->setCookie($name, '', time() - 3600, $this->config);
166
    }
167
168
    /**
169
     * 获取cookie保存数据
170
     * @access public
171
     * @return array
172
     */
173
    public function getCookie(): array
174
    {
175
        return $this->cookie;
176
    }
177
178
    /**
179
     * 保存Cookie
180
     * @access public
181
     * @return void
182
     */
183
    public function save(): void
184
    {
185
        foreach ($this->cookie as $name => $val) {
186
            [$value, $expire, $option] = $val;
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $expire does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $option does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
187
188
            $this->saveCookie(
189
                $name,
190
                $value,
191
                $expire,
192
                $option['path'],
193
                $option['domain'],
194
                $option['secure'] ? true : false,
195
                $option['httponly'] ? true : false,
196
                $option['samesite']
197
            );
198
        }
199
    }
200
201
    /**
202
     * 保存Cookie
203
     * @access public
204
     * @param  string $name cookie名称
205
     * @param  string $value cookie值
206
     * @param  int    $expire cookie过期时间
207
     * @param  string $path 有效的服务器路径
208
     * @param  string $domain 有效域名/子域名
209
     * @param  bool   $secure 是否仅仅通过HTTPS
210
     * @param  bool   $httponly 仅可通过HTTP访问
211
     * @param  string $samesite 防止CSRF攻击和用户追踪
212
     * @return void
213
     */
214
    protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly, string $samesite): void
215
    {
216
        if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
217
            setcookie($name, $value, [
218
                'expires'  => $expire,
219
                'path'     => $path,
220
                'domain'   => $domain,
221
                'secure'   => $secure,
222
                'httponly' => $httponly,
223
                'samesite' => $samesite,
224
            ]);
225
        } else {
226
            setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
227
        }
228
    }
229
230
}
231