Skip to content

Commit 3824bae

Browse files
committed
Allows URL DSN in Lock and Cache
1 parent 3f759d7 commit 3824bae

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

src/Symfony/Component/Cache/Tests/Adapter/PdoAdapterTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,33 @@ public function testCleanupExpiredItems()
7171
$this->assertFalse($newItem->isHit());
7272
$this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
7373
}
74+
75+
/**
76+
* @dataProvider provideDsn
77+
*/
78+
public function testDsn(string $dsn, string $file = null)
79+
{
80+
try {
81+
$pool = new PdoAdapter($dsn);
82+
$pool->createTable();
83+
84+
$item = $pool->getItem('key');
85+
$item->set('value');
86+
$this->assertTrue($pool->save($item));
87+
} finally {
88+
if (null !== $file) {
89+
@unlink($file);
90+
}
91+
}
92+
}
93+
94+
public function provideDsn()
95+
{
96+
$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
97+
yield ['sqlite://localhost/'.$dbFile, ''.$dbFile];
98+
yield ['sqlite:'.$dbFile, ''.$dbFile];
99+
yield ['sqlite3:///'.$dbFile, ''.$dbFile];
100+
yield ['sqlite://localhost/:memory:'];
101+
yield ['sqlite::memory:'];
102+
}
74103
}

src/Symfony/Component/Cache/Traits/PdoTrait.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\Driver\ServerInfoAwareConnection;
1718
use Doctrine\DBAL\Exception\TableNotFoundException;
1819
use Doctrine\DBAL\Schema\Schema;
@@ -370,8 +371,15 @@ protected function doSave(array $values, $lifetime)
370371
private function getConnection()
371372
{
372373
if (null === $this->conn) {
373-
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
374-
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
374+
if (strpos($this->dsn, '://')) {
375+
if (!class_exists(DriverManager::class)) {
376+
throw new InvalidArgumentException(sprintf('Fail to parse the DSN "%s". Try running "composer require doctrine/dbal".', $this->dsn));
377+
}
378+
$this->conn = DriverManager::getConnection(['url' => $this->dsn]);
379+
} else {
380+
$this->conn = new \PDO($this->dsn, $this->username, $this->password, $this->connectionOptions);
381+
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
382+
}
375383
}
376384
if (null === $this->driver) {
377385
if ($this->conn instanceof \PDO) {

src/Symfony/Component/Lock/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('Fail 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;

src/Symfony/Component/Lock/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
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy