|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Bluz Framework Component |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Bluz PHP Team |
|
7
|
|
|
* @link https://github.com/bluzphp/framework |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Bluz\Session\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use Bluz\Common\Exception\ComponentException; |
|
15
|
|
|
use Bluz\Common\Exception\ConfigurationException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Redis session handler |
|
19
|
|
|
* |
|
20
|
|
|
* @package Bluz\Session\Adapter |
|
21
|
|
|
*/ |
|
22
|
|
|
class Redis extends AbstractAdapter implements \SessionHandlerInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array default Redis settings |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $settings = [ |
|
28
|
|
|
'host' => '127.0.0.1', |
|
29
|
|
|
'port' => 6379, |
|
30
|
|
|
'timeout' => 0, |
|
31
|
|
|
'persistence' => false, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Check and setup Redis server |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $settings |
|
38
|
|
|
* |
|
39
|
|
|
* @throws ComponentException |
|
40
|
|
|
* @throws ConfigurationException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(array $settings = []) |
|
43
|
|
|
{ |
|
44
|
|
|
// check Redis extension |
|
45
|
|
|
if (!extension_loaded('redis')) { |
|
46
|
|
|
throw new ComponentException( |
|
47
|
|
|
'Redis extension not installed/enabled. |
|
48
|
|
|
Install and/or enable Redis extension [http://pecl.php.net/package/redis]. |
|
49
|
|
|
See phpinfo() for more information' |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// check Redis settings |
|
54
|
|
|
if (!is_array($settings) || empty($settings)) { |
|
|
|
|
|
|
55
|
|
|
throw new ConfigurationException( |
|
56
|
|
|
'Redis configuration is missed. Please check `session` configuration section' |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Update settings |
|
61
|
|
|
$this->settings = array_replace_recursive($this->settings, $settings); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Initialize session |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $savePath |
|
68
|
|
|
* @param string $sessionName |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function open($savePath, $sessionName): bool |
|
73
|
|
|
{ |
|
74
|
|
|
parent::open($savePath, $sessionName); |
|
75
|
|
|
|
|
76
|
|
|
$this->handler = new \Redis(); |
|
77
|
|
|
|
|
78
|
|
|
if ($this->settings['persistence']) { |
|
79
|
|
|
$this->handler->pconnect($this->settings['host'], $this->settings['port'], $this->settings['timeout']); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->handler->connect($this->settings['host'], $this->settings['port'], $this->settings['timeout']); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (isset($this->settings['options'])) { |
|
85
|
|
|
foreach ($this->settings['options'] as $key => $value) { |
|
86
|
|
|
$this->handler->setOption($key, $value); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Read session data |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $id |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
public function read($id): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->handler->get($this->prepareId($id)) ?: ''; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Write session data |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $id |
|
109
|
|
|
* @param string $data |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function write($id, $data): bool |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->handler->set($this->prepareId($id), $data, (int)$this->ttl); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Destroy a session |
|
120
|
|
|
* |
|
121
|
|
|
* @param integer $id |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public function destroy($id): bool |
|
126
|
|
|
{ |
|
127
|
|
|
$this->handler->del($this->prepareId($id)); |
|
128
|
|
|
return true; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|