Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba55b1e

Browse files
committed
minor #10743 [Doctrine Bridge] simplify session handler (Tobion)
This PR was merged into the 2.3 branch. Discussion ---------- [Doctrine Bridge] simplify session handler | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no because the driver connection could not be used anyway before | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT Use main connection as discussed in #10720 Commits ------- 6518b74 [Doctrine Bridge] simplify session handler by using main connection
2 parents 567213f + 6518b74 commit ba55b1e

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed

src/Symfony/Bridge/Doctrine/HttpFoundation/DbalSessionHandler.php

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,12 @@
1212
namespace Symfony\Bridge\Doctrine\HttpFoundation;
1313

1414
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;
2415

2516
/**
2617
* DBAL based session storage.
2718
*
2819
* 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.
3021
*
3122
* @author Fabien Potencier <[email protected]>
3223
* @author Johannes M. Schmitt <[email protected]>
@@ -35,7 +26,7 @@
3526
class DbalSessionHandler implements \SessionHandlerInterface
3627
{
3728
/**
38-
* @var DriverConnection
29+
* @var Connection
3930
*/
4031
private $con;
4132

@@ -62,10 +53,10 @@ class DbalSessionHandler implements \SessionHandlerInterface
6253
/**
6354
* Constructor.
6455
*
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
6758
*/
68-
public function __construct(DriverConnection $con, $tableName = 'sessions')
59+
public function __construct(Connection $con, $tableName = 'sessions')
6960
{
7061
$this->con = $con;
7162
$this->table = $tableName;
@@ -74,7 +65,7 @@ public function __construct(DriverConnection $con, $tableName = 'sessions')
7465
/**
7566
* {@inheritdoc}
7667
*/
77-
public function open($path = null, $name = null)
68+
public function open($savePath, $sessionName)
7869
{
7970
return true;
8071
}
@@ -90,14 +81,14 @@ public function close()
9081
/**
9182
* {@inheritdoc}
9283
*/
93-
public function destroy($id)
84+
public function destroy($sessionId)
9485
{
9586
// delete the record associated with this id
9687
$sql = "DELETE FROM $this->table WHERE $this->idCol = :id";
9788

9889
try {
9990
$stmt = $this->con->prepare($sql);
100-
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
91+
$stmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
10192
$stmt->execute();
10293
} catch (\Exception $e) {
10394
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)
109100
/**
110101
* {@inheritdoc}
111102
*/
112-
public function gc($lifetime)
103+
public function gc($maxlifetime)
113104
{
114105
// delete the session records that have expired
115106
$sql = "DELETE FROM $this->table WHERE $this->timeCol < :time";
116107

117108
try {
118109
$stmt = $this->con->prepare($sql);
119-
$stmt->bindValue(':time', time() - $lifetime, \PDO::PARAM_INT);
110+
$stmt->bindValue(':time', time() - $maxlifetime, \PDO::PARAM_INT);
120111
$stmt->execute();
121112
} catch (\Exception $e) {
122113
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)
128119
/**
129120
* {@inheritdoc}
130121
*/
131-
public function read($id)
122+
public function read($sessionId)
132123
{
133124
$sql = "SELECT $this->dataCol FROM $this->table WHERE $this->idCol = :id";
134125

135126
try {
136127
$stmt = $this->con->prepare($sql);
137-
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
128+
$stmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
138129
$stmt->execute();
139130

140131
// We use fetchAll instead of fetchColumn to make sure the DB cursor gets closed
@@ -153,7 +144,7 @@ public function read($id)
153144
/**
154145
* {@inheritdoc}
155146
*/
156-
public function write($id, $data)
147+
public function write($sessionId, $data)
157148
{
158149
// Session data can contain non binary safe characters so we need to encode it.
159150
$encoded = base64_encode($data);
@@ -166,7 +157,7 @@ public function write($id, $data)
166157

167158
if (null !== $mergeSql) {
168159
$mergeStmt = $this->con->prepare($mergeSql);
169-
$mergeStmt->bindParam(':id', $id, \PDO::PARAM_STR);
160+
$mergeStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
170161
$mergeStmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
171162
$mergeStmt->bindValue(':time', time(), \PDO::PARAM_INT);
172163
$mergeStmt->execute();
@@ -180,13 +171,13 @@ public function write($id, $data)
180171
$deleteStmt = $this->con->prepare(
181172
"DELETE FROM $this->table WHERE $this->idCol = :id"
182173
);
183-
$deleteStmt->bindParam(':id', $id, \PDO::PARAM_STR);
174+
$deleteStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
184175
$deleteStmt->execute();
185176

186177
$insertStmt = $this->con->prepare(
187178
"INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time)"
188179
);
189-
$insertStmt->bindParam(':id', $id, \PDO::PARAM_STR);
180+
$insertStmt->bindParam(':id', $sessionId, \PDO::PARAM_STR);
190181
$insertStmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
191182
$insertStmt->bindValue(':time', time(), \PDO::PARAM_INT);
192183
$insertStmt->execute();
@@ -211,32 +202,24 @@ public function write($id, $data)
211202
*/
212203
private function getMergeSql()
213204
{
214-
$platform = $pdoDriver = null;
205+
$platform = $this->con->getDatabasePlatform()->getName();
215206

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':
224209
return "INSERT INTO $this->table ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time) " .
225210
"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':
227212
// DUAL is Oracle specific dummy table
228213
return "MERGE INTO $this->table USING DUAL ON ($this->idCol = :id) " .
229214
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time) " .
230215
"WHEN MATCHED THEN UPDATE SET $this->dataCol = :data";
231-
case $this->con instanceof SQLSrvConnection || $platform instanceof SQLServerPlatform || 'sqlsrv' === $pdoDriver:
216+
case 'mssql':
232217
// MS SQL Server requires MERGE be terminated by semicolon
233218
return "MERGE INTO $this->table USING (SELECT 'x' AS dummy) AS src ON ($this->idCol = :id) " .
234219
"WHEN NOT MATCHED THEN INSERT ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time) " .
235220
"WHEN MATCHED THEN UPDATE SET $this->dataCol = :data;";
236-
case $platform instanceof SqlitePlatform || 'sqlite' === $pdoDriver:
221+
case 'sqlite':
237222
return "INSERT OR REPLACE INTO $this->table ($this->idCol, $this->dataCol, $this->timeCol) VALUES (:id, :data, :time)";
238223
}
239-
240-
return null;
241224
}
242225
}

src/Symfony/Bridge/Doctrine/Tests/HttpFoundation/DbalSessionHandlerTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ protected function setUp()
2929

3030
public function testConstruct()
3131
{
32-
$this->connection = $this->getMock('Doctrine\DBAL\Driver\Connection');
33-
$mock = $this->getMockBuilder('Symfony\Bridge\Doctrine\HttpFoundation\DbalSessionHandler');
34-
$mock->setConstructorArgs(array($this->connection));
35-
$this->driver = $mock->getMock();
32+
$connection = $this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock();
33+
$handler = new DbalSessionHandler($connection);
3634
}
3735
}

0 commit comments

Comments
 (0)