|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Xervice\Redis\Communication\Plugin; |
|
5
|
|
|
|
|
6
|
|
|
use DataProvider\RedisSessionDataProvider; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler; |
|
8
|
|
|
use Xervice\Core\Business\Model\Locator\Dynamic\Business\DynamicBusinessLocator; |
|
9
|
|
|
use Xervice\Redis\Business\Exception\RedisException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @method \Xervice\Redis\Business\RedisFacade getFacade() |
|
13
|
|
|
*/ |
|
14
|
|
|
class RedisSessionHandler extends AbstractSessionHandler |
|
15
|
|
|
{ |
|
16
|
|
|
use DynamicBusinessLocator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $prefix; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
private $ttl; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var RedisSessionDataProvider |
|
30
|
|
|
*/ |
|
31
|
|
|
private $sessionDataProvider; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* RedisSessionHandler constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $options |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function __construct(array $options = []) |
|
39
|
|
|
{ |
|
40
|
1 |
|
$this->prefix = $options['prefix'] ?? 'session:xervice.'; |
|
41
|
1 |
|
$this->ttl = $options['ttl'] ?? 86400; |
|
42
|
1 |
|
$this->sessionDataProvider = new RedisSessionDataProvider(); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $sessionId |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
1 |
|
protected function doRead($sessionId): string |
|
51
|
|
|
{ |
|
52
|
|
|
try { |
|
53
|
1 |
|
$this->sessionDataProvider = $this->getFacade()->get($this->prefix . $sessionId); |
|
54
|
1 |
|
} catch (RedisException $exception) { |
|
55
|
1 |
|
$this->sessionDataProvider->setData(''); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
return $this->sessionDataProvider->getData(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $sessionId |
|
63
|
|
|
* @param string $data |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function doWrite($sessionId, $data): bool |
|
68
|
|
|
{ |
|
69
|
|
|
$this->sessionDataProvider->setData($data); |
|
70
|
|
|
$this->getFacade()->setEx($this->prefix . $sessionId, $this->sessionDataProvider, 'ex', $this->ttl); |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $sessionId |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function doDestroy($sessionId): bool |
|
81
|
|
|
{ |
|
82
|
|
|
$this->getFacade()->delete($this->prefix . $sessionId); |
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function close(): bool |
|
91
|
|
|
{ |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param int $maxlifetime |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function gc($maxlifetime): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $session_id |
|
107
|
|
|
* @param string $session_data |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
|
|
public function updateTimestamp($session_id, $session_data): bool |
|
112
|
|
|
{ |
|
113
|
|
|
$this->getFacade()->expire($this->prefix . $session_id, $this->ttl); |
|
114
|
|
|
|
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|