|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Kotori.php |
|
4
|
|
|
* |
|
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
|
6
|
|
|
* |
|
7
|
|
|
* This content is released under the Apache 2 License |
|
8
|
|
|
* |
|
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
|
10
|
|
|
* |
|
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
12
|
|
|
* you may not use this file except in compliance with the License. |
|
13
|
|
|
* You may obtain a copy of the License at |
|
14
|
|
|
* |
|
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
16
|
|
|
* |
|
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
20
|
|
|
* See the License for the specific language governing permissions and |
|
21
|
|
|
* limitations under the License. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Memcached Session Class |
|
26
|
|
|
* |
|
27
|
|
|
* @package Kotori |
|
28
|
|
|
* @subpackage Session |
|
29
|
|
|
* @author Kokororin |
|
30
|
|
|
* @link https://kotori.love |
|
31
|
|
|
*/ |
|
32
|
|
|
namespace Kotori\Http\Session; |
|
33
|
|
|
|
|
34
|
|
|
use Kotori\Core\Cache\Redis as RedisDriver; |
|
35
|
|
|
use SessionHandlerInterface; |
|
36
|
|
|
|
|
37
|
|
|
class Redis implements SessionHandlerInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Default configuration |
|
41
|
|
|
* |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $config = [ |
|
45
|
|
|
'host' => '127.0.0.1', |
|
46
|
|
|
'port' => 6379, |
|
47
|
|
|
'password' => null, |
|
48
|
|
|
'database' => 0, |
|
49
|
|
|
'expire' => 3600, |
|
50
|
|
|
'timeout' => 0, |
|
51
|
|
|
'prefix' => '', |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Class constructor |
|
56
|
|
|
* |
|
57
|
|
|
* Setup Redis |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $config |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct($config = []) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->config = array_merge($this->config, $config); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Open session |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $savePath |
|
70
|
|
|
* @param mixed $sessName |
|
71
|
|
|
* @return boolean |
|
72
|
|
|
*/ |
|
73
|
|
|
// @codingStandardsIgnoreStart |
|
74
|
|
|
public function open($savePath, $sessName) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->redisDriver = new RedisDriver($this->config); |
|
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
// @codingStandardsIgnoreEnd |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Close session |
|
83
|
|
|
* |
|
84
|
|
|
* @return boolean |
|
85
|
|
|
*/ |
|
86
|
|
|
public function close() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->gc(ini_get('session.gc_maxlifetime')); |
|
|
|
|
|
|
89
|
|
|
$this->redisDriver = null; |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Read session |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $sessID |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function read($sessID) |
|
100
|
|
|
{ |
|
101
|
|
|
return (string) $this->redisDriver->get($this->config['prefix'] . $sessID); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Write session |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $sessID |
|
108
|
|
|
* @param String $sessData |
|
109
|
|
|
* @return boolean |
|
110
|
|
|
*/ |
|
111
|
|
|
public function write($sessID, $sessData) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->redisDriver->set($this->config['prefix'] . $sessID, $sessData, $this->config['expire']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Delete session |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $sessID |
|
120
|
|
|
* @return boolean |
|
121
|
|
|
*/ |
|
122
|
|
|
public function destroy($sessID) |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->redisDriver->delete($this->config['prefix'] . $sessID); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* do garbage collection |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $sessMaxLifeTime |
|
131
|
|
|
* @return boolean |
|
132
|
|
|
*/ |
|
133
|
|
|
public function gc($sessMaxLifeTime) |
|
134
|
|
|
{ |
|
135
|
|
|
return true; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: