Repository navigation
Expand file tree
/
Copy pathDatabase.php
More file actions
131 lines (114 loc) · 2.58 KB
/
Copy pathDatabase.php
File metadata and controls
131 lines (114 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types=1);
namespace Rancoud\Session;
use Rancoud\Database\Configurator;
use Rancoud\Database\Database as Db;
use SessionHandlerInterface;
/**
* Class Database.
*/
class Database implements SessionHandlerInterface
{
/** @var \Rancoud\Database\Database */
protected $db;
protected $userId = null;
/**
* @param $configuration
*
* @throws \Exception
*/
public function setNewDatabase($configuration)
{
if ($configuration instanceof Configurator) {
$this->db = new Db($configuration);
} else {
$this->db = new Db(new Configurator($configuration));
}
}
/**
* @param $database
*/
public function setCurrentDatabase($database)
{
$this->db = $database;
}
/**
* @param int $userId
*/
public function setUserId(?int $userId): void
{
$this->userId = $userId;
}
/**
* @param $savePath
* @param $sessionName
*
* @return bool
*/
public function open($savePath, $sessionName): bool
{
return true;
}
/**
* @return bool
*/
public function close(): bool
{
return true;
}
/**
* @param $sessionId
*
* @throws \Exception
*
* @return string
*/
public function read($sessionId): string
{
$sql = 'SELECT content FROM sessions WHERE id = :id';
$params = ['id' => $sessionId];
return (string) $this->db->selectVar($sql, $params);
}
/**
* @param $sessionId
* @param $data
*
* @throws \Exception
*
* @return bool
*/
public function write($sessionId, $data): bool
{
$sql = 'REPLACE INTO sessions VALUES(:id, :id_user, NOW(), :content)';
$params = ['id' => $sessionId, 'id_user' => $this->userId, 'content' => $data];
return $this->db->exec($sql, $params);
}
/**
* @param $sessionId
*
* @throws \Exception
*
* @return bool
*/
public function destroy($sessionId): bool
{
$sql = 'DELETE FROM sessions WHERE id = :id';
$params = ['id' => $sessionId];
$this->db->delete($sql, $params);
return true;
}
/**
* @param $lifetime
*
* @throws \Exception
*
* @return bool
*/
public function gc($lifetime): bool
{
$sql = 'DELETE FROM sessions WHERE DATE_ADD(last_access, INTERVAL :seconds second) < NOW()';
$params = ['seconds' => $lifetime];
$this->db->delete($sql, $params);
return true;
}
}