12
12
namespace Symfony \Bridge \Doctrine \HttpFoundation ;
13
13
14
14
use Doctrine \DBAL \Connection ;
15
- use Doctrine \DBAL \Driver \Connection as DriverConnection ;
16
- use Doctrine \DBAL \Driver \Mysqli \MysqliConnection ;
17
- use Doctrine \DBAL \Driver \OCI8 \OCI8Connection ;
18
- use Doctrine \DBAL \Driver \PDOConnection ;
19
- use Doctrine \DBAL \Driver \SQLSrv \SQLSrvConnection ;
20
- use Doctrine \DBAL \Platforms \MySqlPlatform ;
21
- use Doctrine \DBAL \Platforms \OraclePlatform ;
22
- use Doctrine \DBAL \Platforms \SqlitePlatform ;
23
- use Doctrine \DBAL \Platforms \SQLServerPlatform ;
24
15
25
16
/**
26
17
* DBAL based session storage.
27
18
*
28
19
* This implementation is very similar to Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
29
- * but uses the Doctrine driver connection interface and thus also works with non-PDO-based drivers like mysqli and OCI8.
20
+ * but uses a Doctrine connection and thus also works with non-PDO-based drivers like mysqli and OCI8.
30
21
*
31
22
* @author Fabien Potencier <[email protected] >
32
23
* @author Johannes M. Schmitt <[email protected] >
35
26
class DbalSessionHandler implements \SessionHandlerInterface
36
27
{
37
28
/**
38
- * @var DriverConnection
29
+ * @var Connection
39
30
*/
40
31
private $ con ;
41
32
@@ -62,10 +53,10 @@ class DbalSessionHandler implements \SessionHandlerInterface
62
53
/**
63
54
* Constructor.
64
55
*
65
- * @param DriverConnection $con A driver connection, preferably a wrapper Doctrine\DBAL\Connection for lazy connections
66
- * @param string $tableName Table name
56
+ * @param Connection $con A connection
57
+ * @param string $tableName Table name
67
58
*/
68
- public function __construct (DriverConnection $ con , $ tableName = 'sessions ' )
59
+ public function __construct (Connection $ con , $ tableName = 'sessions ' )
69
60
{
70
61
$ this ->con = $ con ;
71
62
$ this ->table = $ tableName ;
@@ -74,7 +65,7 @@ public function __construct(DriverConnection $con, $tableName = 'sessions')
74
65
/**
75
66
* {@inheritdoc}
76
67
*/
77
- public function open ($ path = null , $ name = null )
68
+ public function open ($ savePath , $ sessionName )
78
69
{
79
70
return true ;
80
71
}
@@ -90,14 +81,14 @@ public function close()
90
81
/**
91
82
* {@inheritdoc}
92
83
*/
93
- public function destroy ($ id )
84
+ public function destroy ($ sessionId )
94
85
{
95
86
// delete the record associated with this id
96
87
$ sql = "DELETE FROM $ this ->table WHERE $ this ->idCol = :id " ;
97
88
98
89
try {
99
90
$ stmt = $ this ->con ->prepare ($ sql );
100
- $ stmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
91
+ $ stmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
101
92
$ stmt ->execute ();
102
93
} catch (\Exception $ e ) {
103
94
throw new \RuntimeException (sprintf ('Exception was thrown when trying to delete a session: %s ' , $ e ->getMessage ()), 0 , $ e );
@@ -109,14 +100,14 @@ public function destroy($id)
109
100
/**
110
101
* {@inheritdoc}
111
102
*/
112
- public function gc ($ lifetime )
103
+ public function gc ($ maxlifetime )
113
104
{
114
105
// delete the session records that have expired
115
106
$ sql = "DELETE FROM $ this ->table WHERE $ this ->timeCol < :time " ;
116
107
117
108
try {
118
109
$ stmt = $ this ->con ->prepare ($ sql );
119
- $ stmt ->bindValue (':time ' , time () - $ lifetime , \PDO ::PARAM_INT );
110
+ $ stmt ->bindValue (':time ' , time () - $ maxlifetime , \PDO ::PARAM_INT );
120
111
$ stmt ->execute ();
121
112
} catch (\Exception $ e ) {
122
113
throw new \RuntimeException (sprintf ('Exception was thrown when trying to delete expired sessions: %s ' , $ e ->getMessage ()), 0 , $ e );
@@ -128,13 +119,13 @@ public function gc($lifetime)
128
119
/**
129
120
* {@inheritdoc}
130
121
*/
131
- public function read ($ id )
122
+ public function read ($ sessionId )
132
123
{
133
124
$ sql = "SELECT $ this ->dataCol FROM $ this ->table WHERE $ this ->idCol = :id " ;
134
125
135
126
try {
136
127
$ stmt = $ this ->con ->prepare ($ sql );
137
- $ stmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
128
+ $ stmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
138
129
$ stmt ->execute ();
139
130
140
131
// We use fetchAll instead of fetchColumn to make sure the DB cursor gets closed
@@ -153,7 +144,7 @@ public function read($id)
153
144
/**
154
145
* {@inheritdoc}
155
146
*/
156
- public function write ($ id , $ data )
147
+ public function write ($ sessionId , $ data )
157
148
{
158
149
// Session data can contain non binary safe characters so we need to encode it.
159
150
$ encoded = base64_encode ($ data );
@@ -166,7 +157,7 @@ public function write($id, $data)
166
157
167
158
if (null !== $ mergeSql ) {
168
159
$ mergeStmt = $ this ->con ->prepare ($ mergeSql );
169
- $ mergeStmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
160
+ $ mergeStmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
170
161
$ mergeStmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
171
162
$ mergeStmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
172
163
$ mergeStmt ->execute ();
@@ -180,13 +171,13 @@ public function write($id, $data)
180
171
$ deleteStmt = $ this ->con ->prepare (
181
172
"DELETE FROM $ this ->table WHERE $ this ->idCol = :id "
182
173
);
183
- $ deleteStmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
174
+ $ deleteStmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
184
175
$ deleteStmt ->execute ();
185
176
186
177
$ insertStmt = $ this ->con ->prepare (
187
178
"INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) "
188
179
);
189
- $ insertStmt ->bindParam (':id ' , $ id , \PDO ::PARAM_STR );
180
+ $ insertStmt ->bindParam (':id ' , $ sessionId , \PDO ::PARAM_STR );
190
181
$ insertStmt ->bindParam (':data ' , $ encoded , \PDO ::PARAM_STR );
191
182
$ insertStmt ->bindValue (':time ' , time (), \PDO ::PARAM_INT );
192
183
$ insertStmt ->execute ();
@@ -211,32 +202,24 @@ public function write($id, $data)
211
202
*/
212
203
private function getMergeSql ()
213
204
{
214
- $ platform = $ pdoDriver = null ;
205
+ $ platform = $ this -> con -> getDatabasePlatform ()-> getName () ;
215
206
216
- if ($ this ->con instanceof Connection) {
217
- $ platform = $ this ->con ->getDatabasePlatform ();
218
- } elseif ($ this ->con instanceof PDOConnection) {
219
- $ pdoDriver = $ this ->con ->getAttribute (\PDO ::ATTR_DRIVER_NAME );
220
- }
221
-
222
- switch (true ) {
223
- case $ this ->con instanceof MysqliConnection || $ platform instanceof MySqlPlatform || 'mysql ' === $ pdoDriver :
207
+ switch ($ platform ) {
208
+ case 'mysql ' :
224
209
return "INSERT INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
225
210
"ON DUPLICATE KEY UPDATE $ this ->dataCol = VALUES( $ this ->dataCol ), $ this ->timeCol = VALUES( $ this ->timeCol ) " ;
226
- case $ this -> con instanceof OCI8Connection || $ platform instanceof OraclePlatform || ' oci ' === $ pdoDriver :
211
+ case ' oracle ' :
227
212
// DUAL is Oracle specific dummy table
228
213
return "MERGE INTO $ this ->table USING DUAL ON ( $ this ->idCol = :id) " .
229
214
"WHEN NOT MATCHED THEN INSERT ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
230
215
"WHEN MATCHED THEN UPDATE SET $ this ->dataCol = :data " ;
231
- case $ this -> con instanceof SQLSrvConnection || $ platform instanceof SQLServerPlatform || ' sqlsrv ' === $ pdoDriver :
216
+ case ' mssql ' :
232
217
// MS SQL Server requires MERGE be terminated by semicolon
233
218
return "MERGE INTO $ this ->table USING (SELECT 'x' AS dummy) AS src ON ( $ this ->idCol = :id) " .
234
219
"WHEN NOT MATCHED THEN INSERT ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " .
235
220
"WHEN MATCHED THEN UPDATE SET $ this ->dataCol = :data; " ;
236
- case $ platform instanceof SqlitePlatform || 'sqlite ' === $ pdoDriver :
221
+ case 'sqlite ' :
237
222
return "INSERT OR REPLACE INTO $ this ->table ( $ this ->idCol , $ this ->dataCol , $ this ->timeCol ) VALUES (:id, :data, :time) " ;
238
223
}
239
-
240
- return null ;
241
224
}
242
225
}
0 commit comments