1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Noxlogic\RateLimitBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; |
6
|
|
|
|
7
|
|
|
class PdoHandler extends PdoSessionHandler |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* No locking is done. This means sessions are prone to loss of data due to |
11
|
|
|
* race conditions of concurrent requests to the same session. The last session |
12
|
|
|
* write will win in this case. It might be useful when you implement your own |
13
|
|
|
* logic to deal with this like an optimistic approach. |
14
|
|
|
*/ |
15
|
|
|
const LOCK_NONE = 0; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Creates an application-level lock on a session. The disadvantage is that the |
19
|
|
|
* lock is not enforced by the database and thus other, unaware parts of the |
20
|
|
|
* application could still concurrently modify the session. The advantage is it |
21
|
|
|
* does not require a transaction. |
22
|
|
|
* This mode is not available for SQLite and not yet implemented for oci and sqlsrv. |
23
|
|
|
*/ |
24
|
|
|
const LOCK_ADVISORY = 1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Issues a real row lock. Since it uses a transaction between opening and |
28
|
|
|
* closing a session, you have to be careful when you use same database connection |
29
|
|
|
* that you also use for your application logic. This mode is the default because |
30
|
|
|
* it's the only reliable solution across DBMSs. |
31
|
|
|
*/ |
32
|
|
|
const LOCK_TRANSACTIONAL = 2; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \PDO|null PDO instance or null when not connected yet |
36
|
|
|
*/ |
37
|
|
|
private $pdo; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string|null|false DSN string or null for session.save_path or false when lazy connection disabled |
41
|
|
|
*/ |
42
|
|
|
private $dsn = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Database driver |
46
|
|
|
*/ |
47
|
|
|
private $driver; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string Table name |
51
|
|
|
*/ |
52
|
|
|
private $table; |
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string Column for cache id |
56
|
|
|
*/ |
57
|
|
|
private $idCol; |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string Column for cache data |
61
|
|
|
*/ |
62
|
|
|
private $dataCol; |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string Column for lifetime |
66
|
|
|
*/ |
67
|
|
|
private $lifetimeCol; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string Column for timestamp |
71
|
|
|
*/ |
72
|
|
|
private $timeCol; |
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string Username when lazy-connect |
76
|
|
|
*/ |
77
|
|
|
private $username = ''; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string Password when lazy-connect |
81
|
|
|
*/ |
82
|
|
|
private $password = ''; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var array Connection options when lazy-connect |
86
|
|
|
*/ |
87
|
|
|
private $connectionOptions = array(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var int The strategy for locking, see constants |
91
|
|
|
*/ |
92
|
|
|
private $lockMode = self::LOCK_TRANSACTIONAL; |
93
|
|
|
|
94
|
|
|
public function __construct($pdoOrDsn = null, array $options = array()) |
95
|
|
|
{ |
96
|
|
|
if ($pdoOrDsn instanceof \PDO) { |
97
|
|
|
if (\PDO::ERRMODE_EXCEPTION !== $pdoOrDsn->getAttribute(\PDO::ATTR_ERRMODE)) { |
98
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" requires PDO error mode attribute be set to throw Exceptions (i.e. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION))', __CLASS__)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->pdo = $pdoOrDsn; |
102
|
|
|
$this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); |
103
|
|
|
} else { |
104
|
|
|
$this->dsn = $pdoOrDsn; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->table = isset($options['db_table']) ? $options['db_table'] : $this->table; |
108
|
|
|
$this->idCol = isset($options['db_id_col']) ? $options['db_id_col'] : $this->idCol; |
109
|
|
|
$this->dataCol = isset($options['db_data_col']) ? $options['db_data_col'] : $this->dataCol; |
110
|
|
|
$this->lifetimeCol = isset($options['db_lifetime_col']) ? $options['db_lifetime_col'] : $this->lifetimeCol; |
111
|
|
|
$this->timeCol = isset($options['db_time_col']) ? $options['db_time_col'] : $this->timeCol; |
112
|
|
|
$this->username = isset($options['db_username']) ? $options['db_username'] : $this->username; |
113
|
|
|
$this->password = isset($options['db_password']) ? $options['db_password'] : $this->password; |
114
|
|
|
$this->connectionOptions = isset($options['db_connection_options']) ? $options['db_connection_options'] : $this->connectionOptions; |
115
|
|
|
$this->lockMode = isset($options['lock_mode']) ? $options['lock_mode'] : $this->lockMode; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function createTable() |
119
|
|
|
{ |
120
|
|
|
$this->getConnection(); |
|
|
|
|
121
|
|
|
|
122
|
|
|
switch ($this->driver) { |
123
|
|
|
case 'pgsql': |
124
|
|
|
$sql = "CREATE TABLE IF NOT EXISTS database_cache ( id VARCHAR(128) NOT NULL PRIMARY KEY, limit INTEGER NOT NULL, info VARCHAR(255) NOT NULL, period INTEGER NOT NULL, reset INTEGER NOT NULL )"; |
125
|
|
|
break; |
126
|
|
|
default: |
127
|
|
|
throw new \DomainException(sprintf('Creating the database cache table is currently not implemented for PDO driver "%s".', $this->driver)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$this->pdo->exec($sql); |
132
|
|
|
} catch (\PDOException $e) { |
133
|
|
|
$this->rollback(); |
|
|
|
|
134
|
|
|
|
135
|
|
|
throw $e; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function fetch($key){ |
140
|
|
|
$this->read($key); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function save($key, $info){ |
144
|
|
|
$this->write($key, $info); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function delete($key){ |
148
|
|
|
$this->destroy($key); |
149
|
|
|
} |
150
|
|
|
} |