Skip to content

Commit f25da14

Browse files
committed
Allows URL DSN in Lock and Cache
1 parent 03d9a1d commit f25da14

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Store/PdoStore.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\DBALException;
16+
use Doctrine\DBAL\DriverManager;
1617
use Doctrine\DBAL\Schema\Schema;
1718
use Symfony\Component\Lock\Exception\InvalidArgumentException;
1819
use Symfony\Component\Lock\Exception\InvalidTtlException;
@@ -229,8 +230,15 @@ private function getUniqueToken(Key $key): string
229230
private function getConnection()
230231
{
231232
if (null === $this->conn) {
232-
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
233-
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
233+
if (strpos($this->dsn, '://')) {
234+
if (!class_exists(DriverManager::class)) {
235+
throw new InvalidArgumentException(sprintf('Failed to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
236+
}
237+
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
238+
} else {
239+
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
240+
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
241+
}
234242
}
235243

236244
return $this->conn;

Tests/Store/PdoStoreTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,34 @@ public function testInvalidTtlConstruct()
7373

7474
return new PdoStore('sqlite:'.self::$dbFile, [], 0.1, 0.1);
7575
}
76+
77+
/**
78+
* @dataProvider provideDsn
79+
*/
80+
public function testDsn(string $dsn, string $file = null)
81+
{
82+
$key = new Key(uniqid(__METHOD__, true));
83+
84+
try {
85+
$store = new PdoStore($dsn);
86+
$store->createTable();
87+
88+
$store->save($key);
89+
$this->assertTrue($store->exists($key));
90+
} finally {
91+
if (null !== $file) {
92+
@unlink($file);
93+
}
94+
}
95+
}
96+
97+
public function provideDsn()
98+
{
99+
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
100+
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
101+
yield ['sqlite:'.$dbFile, ''.$dbFile];
102+
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
103+
yield ['sqlite://localhost/:memory:'];
104+
yield ['sqlite::memory:'];
105+
}
76106
}

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
"psr/log": "~1.0"
2121
},
2222
"require-dev": {
23-
"doctrine/dbal": "~2.4",
23+
"doctrine/dbal": "~2.5",
2424
"mongodb/mongodb": "~1.1",
2525
"predis/predis": "~1.0"
2626
},
27+
"conflict": {
28+
"doctrine/dbal": "<2.5"
29+
},
2730
"autoload": {
2831
"psr-4": { "Symfony\\Component\\Lock\\": "" },
2932
"exclude-from-classmap": [

0 commit comments

Comments
 (0)